Skip to content

Commit

Permalink
Import Optional if default param is None to match renderer (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanbudd committed Feb 22, 2020
1 parent 699e290 commit 9b8beec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions monkeytype/stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ def get_imports_for_signature(sig: inspect.Signature) -> ImportMap:
imports = ImportMap()
for param in sig.parameters.values():
param_imports = get_imports_for_annotation(param.annotation)
if not _is_optional(param.annotation) and param.default is None:
imports['typing'].add('Optional')
imports.merge(param_imports)
return_imports = get_imports_for_annotation(sig.return_annotation)
imports.merge(return_imports)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
StubIndexBuilder,
build_module_stubs,
get_imports_for_annotation,
get_imports_for_signature,
render_signature,
shrink_traced_types,
update_signature_args,
Expand Down Expand Up @@ -1118,3 +1119,10 @@ def test_container_types(self, anno, expected):

def test_nested_class(self):
assert get_imports_for_annotation(Parent.Child) == {Parent.__module__: {'Parent'}}


class TestGetImportsForSignature:
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

0 comments on commit 9b8beec

Please sign in to comment.