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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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