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
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
3 changes: 2 additions & 1 deletion stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1443,13 +1443,14 @@ class zip(Iterator[_T_co], Generic[_T_co]):
def __iter__(self) -> Iterator[_T_co]: ...
def __next__(self) -> _T_co: ...

# Return type of `__import__` should be kept the same as return type of `importlib.import_module`
def __import__(
name: str,
globals: Mapping[str, object] | None = ...,
locals: Mapping[str, object] | None = ...,
fromlist: Sequence[str] = ...,
level: int = ...,
) -> Any: ...
) -> types.ModuleType: ...

# Actually the type of Ellipsis is <type 'ellipsis'>, but since it's
# not exposed anywhere under that name, we make it private here.
Expand Down
1 change: 1 addition & 0 deletions stdlib/importlib/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import types
from importlib.abc import Loader
from typing import Any, Mapping, Sequence

# `__import__` and `import_module` return type should be kept the same as `builtins.__import__`
def __import__(
name: str,
globals: Mapping[str, Any] | None = ...,
Expand Down
4 changes: 4 additions & 0 deletions stdlib/types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ class ModuleType:
__path__: MutableSequence[str]
__spec__: ModuleSpec | None
def __init__(self, name: str, doc: str | None = ...) -> None: ...
# __getattr__ doesn't exist at runtime,
# but having it here in typeshed makes dynamic imports
# using `builtins.__import__` or `importlib.import_module` less painful
def __getattr__(self, name: str) -> Any: ...

@final
class GeneratorType(Generator[_T_co, _T_contra, _V_co]):
Expand Down
1 change: 1 addition & 0 deletions tests/stubtest_allowlists/py3_common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ tkinter.font.Font.__getitem__ # Argument name differs (doesn't matter for __dun
traceback.TracebackException.from_exception # explicitly expanding arguments going into TracebackException __init__
types.GetSetDescriptorType.__get__ # this function can accept no value for the type parameter.
types.MemberDescriptorType.__get__ # this function can accept no value for the type parameter.
types.ModuleType.__getattr__ # this doesn't exist at runtime
types.SimpleNamespace.__init__ # class doesn't accept positional arguments but has default C signature
typing.IO.__iter__ # Added because IO streams are iterable. See https://github.com/python/typeshed/commit/97bc450acd60c1bcdafef3ce8fbe3b95a9c0cac3
typing.IO.__next__ # Added because IO streams are iterable. See https://github.com/python/typeshed/commit/97bc450acd60c1bcdafef3ce8fbe3b95a9c0cac3
Expand Down