Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed stubgen parsing generics from C extensions
Browse files Browse the repository at this point in the history
AWhetter committed Jul 12, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 08cd1d6 commit cf31345
Showing 2 changed files with 85 additions and 1 deletion.
11 changes: 10 additions & 1 deletion mypy/stubgenc.py
Original file line number Diff line number Diff line change
@@ -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,
)
elif module and typ.startswith(module.__name__ + '.'):
stripped_type = typ[len(module.__name__) + 1:]
elif '.' in typ:
arg_module = typ[:typ.rindex('.')]
75 changes: 75 additions & 0 deletions mypy/test/teststubgen.py
Original file line number Diff line number Diff line change
@@ -778,6 +778,81 @@ def test(arg0: str) -> None:
assert_equal(output, ['def test(arg0: str) -> Action: ...'])
assert_equal(imports, [])

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:

0 comments on commit cf31345

Please sign in to comment.