Beispiel #1
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
Beispiel #2
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) -> None: ...'
     assert stub.render() == expected
Beispiel #3
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
Beispiel #4
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
Beispiel #5
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
Beispiel #6
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)
Beispiel #7
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
Beispiel #8
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
Beispiel #9
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
Beispiel #10
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
Beispiel #11
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
Beispiel #12
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
Beispiel #13
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(