Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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_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_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_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_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 _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)
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_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
for entry in entries:
path = entry.qualname.split('.')
name = path.pop()
class_path = path
# TODO: Handle nested classes
klass = None
if len(class_path) > 0:
klass = '.'.join(class_path)
if entry.module not in mod_stubs:
mod_stubs[entry.module] = ModuleStub()
mod_stub = mod_stubs[entry.module]
imports = get_imports_for_signature(entry.signature)
# Import TypedDict, if needed.
if entry.typed_dict_class_stubs:
imports['mypy_extensions'].add('TypedDict')
func_stub = FunctionStub(name, entry.signature, entry.kind, list(imports.keys()), entry.is_async)
# Don't need to import anything from the same module
imports.pop(entry.module, None)
mod_stub.imports_stub.imports.merge(imports)
if klass is not None:
if klass not in mod_stub.class_stubs:
mod_stub.class_stubs[klass] = ClassStub(klass)
class_stub = mod_stub.class_stubs[klass]
class_stub.function_stubs[func_stub.name] = func_stub
else:
mod_stub.function_stubs[func_stub.name] = func_stub
mod_stub.typed_dict_class_stubs.extend(entry.typed_dict_class_stubs)
return mod_stubs