Пример #1
0
 def test_staticmethod(self):
     stub = FunctionStub('test', inspect.signature(Dummy.a_static_method), FunctionKind.STATIC)
     expected = "\n".join([
         '@staticmethod',
         'def test%s: ...' % (render_signature(stub.signature),),
     ])
     assert stub.render() == expected
Пример #2
0
 def test_strip_modules(self):
     """We should strip modules from annotations in the signature"""
     to_strip = [Dummy.__module__]
     f = strip_modules_helper
     stub = FunctionStub(f.__name__, inspect.signature(f), FunctionKind.MODULE, to_strip)
     expected = 'def strip_modules_helper(d1: Dummy, d2: Dummy) -> None: ...'
     assert stub.render() == expected
Пример #3
0
 def test_property(self):
     stub = FunctionStub('test', inspect.signature(Dummy.a_property.fget), FunctionKind.PROPERTY)
     expected = "\n".join([
         '@property',
         'def test%s: ...' % (render_signature(stub.signature),),
     ])
     assert stub.render() == expected
Пример #4
0
 def test_classmethod(self):
     stub = FunctionStub('test', inspect.signature(Dummy.a_class_method), FunctionKind.CLASS)
     expected = "\n".join([
         '@classmethod',
         'def test%s: ...' % (render_signature(stub.signature),),
     ])
     assert stub.render() == expected
Пример #5
0
 def test_optional_union_parameter_annotation(self):
     """Optional[Union[X, Y]] should always be rendered as such, not Union[X, Y, None]"""
     stub = FunctionStub('test',
                         inspect.signature(has_optional_union_param),
                         FunctionKind.MODULE)
     expected = 'def test(x: Optional[Union[int, float]]) -> None: ...'
     assert stub.render() == expected
Пример #6
0
 def test_forward_ref_annotation(self):
     """Forward refs should be rendered as strings, not _ForwardRef(...)."""
     stub = FunctionStub('has_forward_ref',
                         inspect.signature(has_forward_ref),
                         FunctionKind.MODULE)
     expected = "def has_forward_ref() -> Optional['TestFunctionStub']: ..."
     assert stub.render() == expected
Пример #7
0
 def test_cached_property(self):
     stub = FunctionStub('test',
                         inspect.signature(Dummy.a_cached_property.func), FunctionKind.DJANGO_CACHED_PROPERTY)
     expected = "\n".join([
         '@cached_property',
         'def test%s: ...' % (render_signature(stub.signature),),
     ])
     assert stub.render() == expected
Пример #8
0
 def test_nonetype_annotation(self):
     """NoneType should always be rendered as None"""
     sig = Signature.from_callable(UpdateSignatureHelper.has_annos)
     sig = update_signature_args(sig, {'a': Dict[str, NoneType]}, has_self=False,
                                 existing_annotation_strategy=ExistingAnnotationStrategy.IGNORE)
     stub = FunctionStub('test', sig, FunctionKind.MODULE)
     expected = 'def test(a: Dict[str, None], b) -> int: ...'
     assert stub.render() == expected
Пример #9
0
 def test_async_function(self):
     stub = FunctionStub('test',
                         inspect.signature(simple_add),
                         FunctionKind.MODULE,
                         is_async=True)
     expected = 'async def test%s: ...' % (render_signature(
         stub.signature), )
     assert stub.render() == expected
Пример #10
0
    def test_split_parameters_across_multiple_lines(self):
        """When single-line length exceeds 120 characters, parameters should be split into multiple lines."""
        stub = FunctionStub('has_length_exceeds_120_chars',
                            inspect.signature(has_length_exceeds_120_chars),
                            FunctionKind.MODULE)
        expected = dedent('''\
        def has_length_exceeds_120_chars(
            very_long_name_parameter_1: float,
            very_long_name_parameter_2: float
        ) -> Optional[float]: ...''')
        assert stub.render() == expected

        expected = '\n'.join([
            '    def has_length_exceeds_120_chars(',
            '        very_long_name_parameter_1: float,',
            '        very_long_name_parameter_2: float',
            '    ) -> Optional[float]: ...'])
        assert stub.render(prefix='    ') == expected
Пример #11
0
 def test_build_index(self):
     idxb = StubIndexBuilder('tests')
     idxb.log(CallTrace(untyped_helper, {'x': int, 'y': str}, str))
     sig = Signature.from_callable(untyped_helper)
     sig = sig.replace(parameters=[
         Parameter('x', Parameter.POSITIONAL_OR_KEYWORD, annotation=int),
         Parameter('y', Parameter.POSITIONAL_OR_KEYWORD, annotation=str),
     ],
                       return_annotation=str)
     mod_stub = ModuleStub(function_stubs=[
         FunctionStub('untyped_helper', sig, FunctionKind.MODULE)
     ])
     expected = {'tests.test_stubs': mod_stub}
     assert idxb.get_stubs() == expected
Пример #12
0
 def test_newtype_parameter_annotation(self):
     stub = FunctionStub('test', inspect.signature(has_newtype_param),
                         FunctionKind.MODULE)
     expected = 'def test(user_id: UserId) -> None: ...'
     assert stub.render() == expected
Пример #13
0
 def test_optional_return_annotation(self):
     """Optional should always be included in return annotations"""
     stub = FunctionStub('test', inspect.signature(has_optional_return),
                         FunctionKind.MODULE)
     expected = 'def test() -> Optional[int]: ...'
     assert stub.render() == expected
Пример #14
0
 def test_default_none_parameter_annotation(self):
     stub = FunctionStub('test', inspect.signature(default_none_parameter),
                         FunctionKind.MODULE)
     expected = 'def test(x: Optional[int] = ...) -> None: ...'
     assert stub.render() == expected
Пример #15
0
 def test_forward_ref_annotation_within_generator(self):
     stub = FunctionStub('foo',
                         inspect.signature(has_forward_ref_within_generator),
                         FunctionKind.MODULE)
     expected = "def foo() -> Generator['TestFunctionStub', None, int]: ..."
     assert stub.render() == expected
Пример #16
0
 def test_optional_parameter_annotation(self):
     """Optional should always be included in parameter annotations, even if the default value is None"""
     stub = FunctionStub('test', inspect.signature(has_optional_param),
                         FunctionKind.MODULE)
     expected = 'def test(x: Optional[int] = ...) -> None: ...'
     assert stub.render() == expected
Пример #17
0
def _func_stub_from_callable(func: Callable, strip_modules: List[str] = None):
    kind = FunctionKind.from_callable(func)
    sig = Signature.from_callable(func)
    return FunctionStub(func.__name__, sig, kind, strip_modules)
Пример #18
0
 def test_with_prefix(self):
     stub = FunctionStub('test', inspect.signature(simple_add),
                         FunctionKind.MODULE)
     expected = '  def test%s: ...' % (render_signature(stub.signature), )
     assert stub.render(prefix='  ') == expected
Пример #19
0
 def test_default_none_parameter_imports(self):
     stub = FunctionStub('test', inspect.signature(default_none_parameter), FunctionKind.MODULE)
     expected = {'typing': {'Optional'}}
     assert get_imports_for_signature(stub.signature) == expected
Пример #20
0
 def test_simple(self):
     for kind in [FunctionKind.MODULE, FunctionKind.INSTANCE]:
         stub = FunctionStub('test', inspect.signature(simple_add), kind)
         expected = 'def test%s: ...' % (render_signature(stub.signature), )
         assert stub.render() == expected
Пример #21
0
 'tests.util': ModuleStub(
     function_stubs=(),
     class_stubs=[
         ClassStub(
             name='Dummy',
             function_stubs=[
                 FunctionStub(
                     name='an_instance_method',
                     signature=Signature(
                         parameters=[
                             Parameter(name='self',
                                       kind=Parameter.POSITIONAL_OR_KEYWORD,
                                       annotation=Parameter.empty),
                             Parameter(name='foo',
                                       kind=Parameter.POSITIONAL_OR_KEYWORD,
                                       annotation=make_forward_ref('FooTypedDict')),
                             Parameter(name='bar',
                                       kind=Parameter.POSITIONAL_OR_KEYWORD,
                                       annotation=int),
                         ],
                         return_annotation=make_forward_ref('DummyAnInstanceMethodTypedDict'),
                     ),
                     kind=FunctionKind.INSTANCE,
                     strip_modules=['mypy_extensions'],
                     is_async=False,
                 ),
             ],
         ),
     ],
     imports_stub=ImportBlockStub(typed_dict_import_map),
     typed_dict_class_stubs=[
         ClassStub(