Skip to content

Fixed stubgen parsing generics from C extensions #8939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion mypy/stubgenc.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,16 @@ def strip_or_import(typ: str, module: ModuleType, imports: List[str]) -> str:
imports: list of import statements (may be modified during the call)
"""
stripped_type = typ
if module and typ.startswith(module.__name__ + '.'):
if any(c in typ for c in '[,'):
for subtyp in re.split(r'[\[,\]]', typ):
strip_or_import(subtyp.strip(), module, imports)
if module:
stripped_type = re.sub(
r'(^|[\[, ]+)' + re.escape(module.__name__ + '.'),
r'\1',
typ,
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have some unit tests for the new functionality, since this logic is non-trivial. You can add them to mypy/test/teststubgen.py.

(Maybe also modify this so that the module name is passed instead of a module object.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I couldn't find the tests for stubgen. I'll get those added. Thanks for the review!

elif module and typ.startswith(module.__name__ + '.'):
stripped_type = typ[len(module.__name__) + 1:]
elif '.' in typ:
arg_module = typ[:typ.rindex('.')]
Expand Down
75 changes: 75 additions & 0 deletions mypy/test/teststubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,81 @@ def get_attribute(self) -> None:
generate_c_property_stub('attribute', TestClass.attribute, output, readonly=True)
assert_equal(output, ['@property', 'def attribute(self) -> str: ...'])

def test_generate_c_type_with_single_arg_generic(self) -> None:
class TestClass:
def test(self, arg0: str) -> None:
"""
test(self: TestClass, arg0: List[int])
"""
pass
output = [] # type: List[str]
imports = [] # type: List[str]
mod = ModuleType(TestClass.__module__, '')
generate_c_function_stub(mod, 'test', TestClass.test, output, imports,
self_var='self', class_name='TestClass')
assert_equal(output, ['def test(self, arg0: List[int]) -> Any: ...'])
assert_equal(imports, [])

def test_generate_c_type_with_double_arg_generic(self) -> None:
class TestClass:
def test(self, arg0: str) -> None:
"""
test(self: TestClass, arg0: Dict[str, int])
"""
pass
output = [] # type: List[str]
imports = [] # type: List[str]
mod = ModuleType(TestClass.__module__, '')
generate_c_function_stub(mod, 'test', TestClass.test, output, imports,
self_var='self', class_name='TestClass')
assert_equal(output, ['def test(self, arg0: Dict[str,int]) -> Any: ...'])
assert_equal(imports, [])

def test_generate_c_type_with_nested_generic(self) -> None:
class TestClass:
def test(self, arg0: str) -> None:
"""
test(self: TestClass, arg0: Dict[str, List[int]])
"""
pass
output = [] # type: List[str]
imports = [] # type: List[str]
mod = ModuleType(TestClass.__module__, '')
generate_c_function_stub(mod, 'test', TestClass.test, output, imports,
self_var='self', class_name='TestClass')
assert_equal(output, ['def test(self, arg0: Dict[str,List[int]]) -> Any: ...'])
assert_equal(imports, [])

def test_generate_c_type_with_generic_using_other_module_first(self) -> None:
class TestClass:
def test(self, arg0: str) -> None:
"""
test(self: TestClass, arg0: Dict[argparse.Action, int])
"""
pass
output = [] # type: List[str]
imports = [] # type: List[str]
mod = ModuleType(TestClass.__module__, '')
generate_c_function_stub(mod, 'test', TestClass.test, output, imports,
self_var='self', class_name='TestClass')
assert_equal(output, ['def test(self, arg0: Dict[argparse.Action,int]) -> Any: ...'])
assert_equal(imports, ['import argparse'])

def test_generate_c_type_with_generic_using_other_module_last(self) -> None:
class TestClass:
def test(self, arg0: str) -> None:
"""
test(self: TestClass, arg0: Dict[str, argparse.Action])
"""
pass
output = [] # type: List[str]
imports = [] # type: List[str]
mod = ModuleType(TestClass.__module__, '')
generate_c_function_stub(mod, 'test', TestClass.test, output, imports,
self_var='self', class_name='TestClass')
assert_equal(output, ['def test(self, arg0: Dict[str,argparse.Action]) -> Any: ...'])
assert_equal(imports, ['import argparse'])

def test_generate_c_type_with_overload_pybind11(self) -> None:
class TestClass:
def __init__(self, arg0: str) -> None:
Expand Down