Skip to content

Commit aa7e277

Browse files
authored
Harmonise return type of builtins.__import__ and importlib.import_module (#6302)
builtins.__import__ now returns ModuleType instead of Any. In addition, add __getattr__() to ModuleType to ease using imported modules.
1 parent 10c9d8c commit aa7e277

File tree

4 files changed

+8
-1
lines changed

4 files changed

+8
-1
lines changed

stdlib/builtins.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,13 +1443,14 @@ class zip(Iterator[_T_co], Generic[_T_co]):
14431443
def __iter__(self) -> Iterator[_T_co]: ...
14441444
def __next__(self) -> _T_co: ...
14451445

1446+
# Return type of `__import__` should be kept the same as return type of `importlib.import_module`
14461447
def __import__(
14471448
name: str,
14481449
globals: Mapping[str, object] | None = ...,
14491450
locals: Mapping[str, object] | None = ...,
14501451
fromlist: Sequence[str] = ...,
14511452
level: int = ...,
1452-
) -> Any: ...
1453+
) -> types.ModuleType: ...
14531454

14541455
# Actually the type of Ellipsis is <type 'ellipsis'>, but since it's
14551456
# not exposed anywhere under that name, we make it private here.

stdlib/importlib/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import types
22
from importlib.abc import Loader
33
from typing import Any, Mapping, Sequence
44

5+
# `__import__` and `import_module` return type should be kept the same as `builtins.__import__`
56
def __import__(
67
name: str,
78
globals: Mapping[str, Any] | None = ...,

stdlib/types.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ class ModuleType:
178178
__path__: MutableSequence[str]
179179
__spec__: ModuleSpec | None
180180
def __init__(self, name: str, doc: str | None = ...) -> None: ...
181+
# __getattr__ doesn't exist at runtime,
182+
# but having it here in typeshed makes dynamic imports
183+
# using `builtins.__import__` or `importlib.import_module` less painful
184+
def __getattr__(self, name: str) -> Any: ...
181185

182186
@final
183187
class GeneratorType(Generator[_T_co, _T_contra, _V_co]):

tests/stubtest_allowlists/py3_common.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ tkinter.font.Font.__getitem__ # Argument name differs (doesn't matter for __dun
199199
traceback.TracebackException.from_exception # explicitly expanding arguments going into TracebackException __init__
200200
types.GetSetDescriptorType.__get__ # this function can accept no value for the type parameter.
201201
types.MemberDescriptorType.__get__ # this function can accept no value for the type parameter.
202+
types.ModuleType.__getattr__ # this doesn't exist at runtime
202203
types.SimpleNamespace.__init__ # class doesn't accept positional arguments but has default C signature
203204
typing.IO.__iter__ # Added because IO streams are iterable. See https://github.com/python/typeshed/commit/97bc450acd60c1bcdafef3ce8fbe3b95a9c0cac3
204205
typing.IO.__next__ # Added because IO streams are iterable. See https://github.com/python/typeshed/commit/97bc450acd60c1bcdafef3ce8fbe3b95a9c0cac3

0 commit comments

Comments
 (0)