Skip to content

Commit

Permalink
Merge pull request #191 from pradeep90/master
Browse files Browse the repository at this point in the history
Render empty tuple type correctly.
  • Loading branch information
pradeep90 committed Jun 8, 2020
2 parents 330dc6f + 6617b1e commit 686e539
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion monkeytype/stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def rewrite_type_variable(self, type_variable: Any) -> str:
return rendered[len(tilde_prefix):] if rendered.startswith(tilde_prefix) else rendered

def make_builtin_tuple(self, elements: Iterable[str]) -> str:
return ', '.join(elements)
return ', '.join(elements) if elements else '()'

def make_container_type(self, container_type: str, elements: str) -> str:
return f'{container_type}[{elements}]'
Expand Down
25 changes: 25 additions & 0 deletions tests/test_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class TestRenderAnnotation:
(List[Generator[make_forward_ref('Foo'), None, None]], 'List[Generator[\'Foo\', None, None]]'),
(T, 'T'),
(Dict[str, T], 'Dict[str, T]'),
(Tuple[()], 'Tuple[()]'),
],
)
def test_render_annotation(self, annotation, expected):
Expand Down Expand Up @@ -755,6 +756,30 @@ def test_render_typed_dict_base_and_subclass(self):
' def an_instance_method(self, foo: \'FooTypedDict__RENAME_ME__NonTotal\', bar: int) -> int: ...'])
assert build_module_stubs(entries)['tests.util'].render() == expected

def test_render_return_empty_tuple(self):
"""Regression test for #190."""
function = FunctionDefinition.from_callable_and_traced_types(
Dummy.an_instance_method,
{
'foo': int,
'bar': int,
},
Tuple[()],
yield_type=None,
existing_annotation_strategy=ExistingAnnotationStrategy.IGNORE
)
entries = [function]
expected = '\n'.join([
'from typing import Tuple',
'',
'',
'class Dummy:',
' def an_instance_method(self, foo: int, bar: int)'
' -> Tuple[()]: ...',
])
self.maxDiff = None
assert build_module_stubs(entries)['tests.util'].render() == expected


class TestBuildModuleStubs:
def test_build_module_stubs(self):
Expand Down
1 change: 1 addition & 0 deletions tests/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ class TestGetType:
('foo', str),
(Dummy, Type[Dummy]),
(1.1, float),
((), typing_Tuple[()]),
(('a', 1, True), typing_Tuple[str, int, bool]),
(set(), Set[Any]),
({'a', 'b', 'c'}, Set[str]),
Expand Down

0 comments on commit 686e539

Please sign in to comment.