Skip to content

Harmonise return type of builtins.__import__ and importlib.import_module #6302

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 3 commits into from
Nov 15, 2021

Conversation

AlexWaygood
Copy link
Member

This PR is an attempt at solving #6295 by adding __getattr__ to types.ModuleType and changing the return type of builtins.__import__ so that it now returns types.ModuleType rather than Any.

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

isort (https://github.com/pycqa/isort.git)
+ isort/_future/__init__.py:9: error: unused "type: ignore" comment
+ isort/_future/__init__.py:10: error: unused "type: ignore" comment
+ isort/output.py:504: error: unused "type: ignore" comment
+ isort/output.py:516: error: unused "type: ignore" comment
+ isort/output.py:523: error: unused "type: ignore" comment

pip (https://github.com/pypa/pip.git)
- src/pip/_internal/locations/__init__.py:68: error: Module has no attribute "platlibdir"
+ src/pip/_internal/network/session.py:106: error: unused "type: ignore" comment

kornia (https://github.com/kornia/kornia.git)
- kornia/testing/__init__.py:16: error: Module has no attribute "util"  [attr-defined]

ibis (https://github.com/ibis-project/ibis.git)
- ibis/backends/tests/conftest.py:45: error: Module has no attribute "TestConf"

python-chess (https://github.com/niklasf/python-chess.git)
+ chess/engine.py:122: error: unused "type: ignore" comment
+ chess/engine.py:123: error: unused "type: ignore" comment
+ chess/engine.py:1209: error: unused "type: ignore" comment

anyio (https://github.com/agronholm/anyio.git)
+ src/anyio/_core/_eventloop.py:56: error: unused "type: ignore" comment

sphinx (https://github.com/sphinx-doc/sphinx.git)
+ sphinx/util/typing.py:228: error: unused "type: ignore" comment
+ sphinx/util/typing.py:428: error: unused "type: ignore" comment
+ sphinx/util/inspect.py:424: error: unused "type: ignore" comment
+ sphinx/util/inspect.py:427: error: unused "type: ignore" comment
+ sphinx/util/inspect.py:430: error: unused "type: ignore" comment
+ sphinx/util/docutils.py:52: error: unused "type: ignore" comment
+ sphinx/util/docutils.py:53: error: unused "type: ignore" comment
+ sphinx/util/docutils.py:67: error: unused "type: ignore" comment
+ sphinx/util/docutils.py:81: error: unused "type: ignore" comment
+ sphinx/util/docutils.py:95: error: unused "type: ignore" comment
+ sphinx/util/docutils.py:110: error: unused "type: ignore" comment
+ sphinx/domains/python.py:1452: error: unused "type: ignore" comment
+ sphinx/writers/manpage.py:110: error: unused "type: ignore" comment
+ sphinx/testing/util.py:126: error: unused "type: ignore" comment
+ sphinx/testing/util.py:127: error: unused "type: ignore" comment
+ sphinx/util/compat.py:29: error: unused "type: ignore" comment

aiohttp (https://github.com/aio-libs/aiohttp.git)
+ aiohttp/connector.py:1306: error: unused "type: ignore" comment
+ aiohttp/web_runner.py:181: error: unused "type: ignore" comment
+ aiohttp/pytest_plugin.py:244: error: unused "type: ignore" comment

streamlit (https://github.com/streamlit/streamlit.git)
+ lib/streamlit/script_runner.py:432: error: unused "type: ignore" comment
+ lib/streamlit/script_runner.py:439: error: unused "type: ignore" comment

werkzeug (https://github.com/pallets/werkzeug.git)
+ src/werkzeug/_reloader.py:20: error: unused "type: ignore" comment
- tests/test_wrappers.py:833: note: Use "-> None" if function does not return a value
- tests/test_wrappers.py:856: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)
+ tests/test_wrappers.py:833: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)

core (https://github.com/home-assistant/core.git)
+ homeassistant/components/http/forwarded.py:91: error: unused "type: ignore" comment
+ homeassistant/auth/mfa_modules/__init__.py:136: error: unused "type: ignore" comment
+ homeassistant/auth/mfa_modules/__init__.py:171: error: unused "type: ignore" comment
+ homeassistant/auth/providers/__init__.py:145: error: unused "type: ignore" comment
+ homeassistant/auth/providers/__init__.py:178: error: unused "type: ignore" comment
+ homeassistant/config.py:648: error: unused "type: ignore" comment
+ homeassistant/config.py:651: error: unused "type: ignore" comment
+ homeassistant/config.py:662: error: unused "type: ignore" comment
+ homeassistant/config.py:672: error: unused "type: ignore" comment
+ homeassistant/config.py:673: error: unused "type: ignore" comment
+ homeassistant/config.py:753: error: unused "type: ignore" comment
+ homeassistant/config.py:895: error: unused "type: ignore" comment
+ homeassistant/setup.py:241: error: unused "type: ignore" comment
+ homeassistant/setup.py:246: error: unused "type: ignore" comment
+ homeassistant/helpers/check_config.py:161: error: unused "type: ignore" comment
+ homeassistant/config_entries.py:304: error: unused "type: ignore" comment
+ homeassistant/config_entries.py:443: error: unused "type: ignore" comment
+ homeassistant/config_entries.py:482: error: unused "type: ignore" comment
+ homeassistant/config_entries.py:519: error: unused "type: ignore" comment
+ homeassistant/helpers/condition.py:929: error: unused "type: ignore" comment
+ homeassistant/helpers/condition.py:977: error: unused "type: ignore" comment
+ homeassistant/helpers/script.py:260: error: unused "type: ignore" comment
+ homeassistant/helpers/script.py:262: error: unused "type: ignore" comment
+ homeassistant/helpers/script.py:269: error: unused "type: ignore" comment
+ homeassistant/scripts/__init__.py:67: error: unused "type: ignore" comment
+ homeassistant/helpers/reload.py:81: error: unused "type: ignore" comment
+ homeassistant/helpers/reload.py:82: error: unused "type: ignore" comment
+ homeassistant/components/notify/legacy.py:61: error: unused "type: ignore" comment
+ homeassistant/components/notify/legacy.py:66: error: unused "type: ignore" comment
+ homeassistant/components/device_tracker/legacy.py:241: error: unused "type: ignore" comment
+ homeassistant/components/device_tracker/legacy.py:246: error: unused "type: ignore" comment
+ homeassistant/components/device_tracker/legacy.py:251: error: unused "type: ignore" comment
+ homeassistant/components/device_tracker/legacy.py:256: error: unused "type: ignore" comment

edgedb (https://github.com/edgedb/edgedb.git)
+ edb/server/pgconnparams.py:75: error: unused "type: ignore" comment

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Nov 15, 2021

Diff from mypy_primer, showing the effect of this PR on open source code:

[...]

This seems like it just makes a lot of people's lives easier, if they were just switching off the type-checker whenever they were doing a dynamic import.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

Looks quite promising. Waiting for @JelleZijlstra's input.

@srittau srittau merged commit aa7e277 into python:master Nov 15, 2021
@AlexWaygood AlexWaygood deleted the harmonise-import-return branch November 15, 2021 15:22
@AlexWaygood
Copy link
Member Author

Thanks, both! 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants