Skip to content

stubgen can import Iterable and Iterator from typing #9088

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 2 commits into from
Aug 2, 2020
Merged
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
15 changes: 14 additions & 1 deletion mypy/stubgenc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
)


# Members of the typing module to consider for importing by default.
_DEFAULT_TYPING_IMPORTS = (
'Any'
Copy link

Choose a reason for hiding this comment

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

@AWhetter There is a missing comma here, without it you get 'AnyDict' as the first entry.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've addressed this here: #9900

'Dict',
'Iterable',
'Iterator',
'List',
'Optional',
'Tuple',
'Union',
)


def generate_stub_for_c_module(module_name: str,
target: str,
sigs: Optional[Dict[str, str]] = None,
Expand Down Expand Up @@ -82,7 +95,7 @@ def generate_stub_for_c_module(module_name: str,
def add_typing_import(output: List[str]) -> List[str]:
"""Add typing imports for collections/types that occur in the generated stub."""
names = []
for name in ['Any', 'Union', 'Tuple', 'Optional', 'List', 'Dict']:
for name in _DEFAULT_TYPING_IMPORTS:
if any(re.search(r'\b%s\b' % name, line) for line in output):
names.append(name)
if names:
Expand Down