Skip to content

Commit

Permalink
rewrite _io imports to io in stubs (fixes #1) (PR #32)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamrajhans authored and carljm committed Dec 21, 2017
1 parent 7d2fafb commit d36c344
Show file tree
Hide file tree
Showing 2 changed files with 25 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 @@ -266,6 +266,8 @@ def render(self) -> str:
imports = []
for module in sorted(self.imports.keys()):
names = sorted(self.imports[module])
if module == '_io':
module = module[1:]
if len(names) == 1:
imports.append("from %s import %s" % (module, names[0]))
else:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ def test_single_import(self):
])
assert stub.render() == expected

def test_io_import_single(self):
"""Single _io imports should convert to io"""
imports = ImportMap()
imports['_io'] = {'BytesIO'}
stub = ImportBlockStub(imports)
expected = "\n".join([
'from io import BytesIO',
])
assert stub.render() == expected

def test_multiple_imports(self):
"""Multiple imports from a single module should each be on their own line"""
imports = ImportMap()
Expand All @@ -88,6 +98,19 @@ def test_multiple_imports(self):
])
assert stub.render() == expected

def test_multiple_io_imports(self):
"""Multiple imports from single _io module should be convert to io import"""
imports = ImportMap()
imports['_io'] = {'BytesIO', 'FileIO'}
stub = ImportBlockStub(imports)
expected = "\n".join([
'from io import (',
' BytesIO,',
' FileIO,',
')',
])
assert stub.render() == expected


def simple_add(a: int, b: int) -> int:
return a + b
Expand Down

0 comments on commit d36c344

Please sign in to comment.