Skip to content

Update importlib to reflect recent changes #6557

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 10 commits into from
Dec 10, 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
1 change: 1 addition & 0 deletions stdlib/VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ imghdr: 2.7-
imp: 2.7-
importlib: 2.7-
importlib.metadata: 3.8-
importlib.metadata._meta: 3.10-
importlib.resources: 3.7-
inspect: 2.7-
io: 2.7-
Expand Down
9 changes: 8 additions & 1 deletion stdlib/importlib/abc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ from _typeshed import (
from abc import ABCMeta, abstractmethod
from importlib.machinery import ModuleSpec
from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper
from typing import IO, Any, BinaryIO, Iterator, Mapping, Protocol, Sequence, Union, overload
from typing import IO, Any, BinaryIO, Iterator, Mapping, NoReturn, Protocol, Sequence, Union, overload
from typing_extensions import Literal, runtime_checkable

_Path = Union[bytes, str]
Expand Down Expand Up @@ -173,3 +173,10 @@ if sys.version_info >= (3, 9):
def read_bytes(self) -> bytes: ...
@abstractmethod
def read_text(self, encoding: str | None = ...) -> str: ...
class TraversableResources(ResourceReader):
@abstractmethod
def files(self) -> Traversable: ...
def open_resource(self, resource: StrPath) -> BufferedReader: ... # type: ignore[override]
def resource_path(self, resource: Any) -> NoReturn: ...
def is_resource(self, path: StrPath) -> bool: ...
def contents(self) -> Iterator[str]: ...
11 changes: 10 additions & 1 deletion stdlib/importlib/machinery.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import importlib.abc
import sys
import types
from typing import Any, Callable, Sequence
from typing import Any, Callable, Iterable, Sequence

if sys.version_info >= (3, 8):
from importlib.metadata import DistributionFinder, PathDistribution

class ModuleSpec:
def __init__(
Expand Down Expand Up @@ -97,6 +100,12 @@ class PathFinder:
else:
@classmethod
def invalidate_caches(cls) -> None: ...
if sys.version_info >= (3, 10):
@staticmethod
def find_distributions(context: DistributionFinder.Context = ...) -> Iterable[PathDistribution]: ...
elif sys.version_info >= (3, 8):
@classmethod
def find_distributions(cls, context: DistributionFinder.Context = ...) -> Iterable[PathDistribution]: ...
@classmethod
def find_spec(
cls, fullname: str, path: Sequence[bytes | str] | None = ..., target: types.ModuleType | None = ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ from email.message import Message
from importlib.abc import MetaPathFinder
from os import PathLike
from pathlib import Path
from typing import Any, Iterable, NamedTuple, Tuple, overload
from typing import Any, ClassVar, Iterable, NamedTuple, Pattern, Tuple, overload

if sys.version_info >= (3, 10):
from importlib.metadata._meta import PackageMetadata as PackageMetadata
def packages_distributions() -> Mapping[str, list[str]]: ...

if sys.version_info >= (3, 8):
Expand All @@ -19,9 +20,18 @@ if sys.version_info >= (3, 8):
value: str
group: str
class EntryPoint(_EntryPointBase):
pattern: ClassVar[Pattern[str]]
def load(self) -> Any: ... # Callable[[], Any] or an importable module
@property
def extras(self) -> list[str]: ...
if sys.version_info >= (3, 9):
@property
def module(self) -> str: ...
@property
def attr(self) -> str: ...
if sys.version_info >= (3, 10):
dist: ClassVar[Distribution | None]
def matches(self, **params: Any) -> bool: ... # undocumented
class PackagePath(pathlib.PurePosixPath):
def read_text(self, encoding: str = ...) -> str: ...
def read_binary(self) -> bytes: ...
Expand Down Expand Up @@ -61,6 +71,9 @@ if sys.version_info >= (3, 8):
def files(self) -> list[PackagePath] | None: ...
@property
def requires(self) -> list[str] | None: ...
if sys.version_info >= (3, 10):
@property
def name(self) -> str: ...
class DistributionFinder(MetaPathFinder):
class Context:
name: str | None
Expand Down
18 changes: 18 additions & 0 deletions stdlib/importlib/metadata/_meta.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import Any, Iterator, Protocol, TypeVar

_T = TypeVar("_T")

class PackageMetadata(Protocol):
def __len__(self) -> int: ...
def __contains__(self, item: str) -> bool: ...
def __getitem__(self, key: str) -> str: ...
def __iter__(self) -> Iterator[str]: ...
def get_all(self, name: str, failobj: _T = ...) -> list[Any] | _T: ...
@property
def json(self) -> dict[str, str | list[str]]: ...

class SimplePath(Protocol):
def joinpath(self) -> SimplePath: ...
def __div__(self) -> SimplePath: ...
def parent(self) -> SimplePath: ...
def read_text(self) -> str: ...
3 changes: 3 additions & 0 deletions stdlib/importlib/resources.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ if sys.version_info >= (3, 9):
from importlib.abc import Traversable
def files(package: Package) -> Traversable: ...
def as_file(path: Traversable) -> AbstractContextManager[Path]: ...

if sys.version_info >= (3, 10):
from importlib.abc import ResourceReader as ResourceReader
4 changes: 4 additions & 0 deletions stdlib/importlib/util.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import importlib.abc
import importlib.machinery
import sys
import types
from _typeshed import StrOrBytesPath
from typing import Any, Callable
Expand Down Expand Up @@ -36,3 +37,6 @@ class LazyLoader(importlib.abc.Loader):
def factory(cls, loader: importlib.abc.Loader) -> Callable[..., LazyLoader]: ...
def create_module(self, spec: importlib.machinery.ModuleSpec) -> types.ModuleType | None: ...
def exec_module(self, module: types.ModuleType) -> None: ...

if sys.version_info >= (3, 7):
def source_hash(source_bytes: bytes) -> int: ...
14 changes: 3 additions & 11 deletions tests/stubtest_allowlists/py310.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ gettext.translation
hmac.new # Stub is a white lie; see comments in the stub
http.server.SimpleHTTPRequestHandler.__init__ # *args is expanded
importlib.abc.Traversable.__init__ # Inherits __init__ from typing.Protocol
importlib.metadata.PackageMetadata.__init__ # Inherits __init__ from typing.Protocol
importlib.metadata._meta.PackageMetadata.__init__ # Inherits __init__ from typing.Protocol
importlib.metadata._meta.SimplePath.__init__ # Inherits __init__ from typing.Protocol
ipaddress.IPv4Interface.hostmask
ipaddress.IPv6Interface.hostmask
ipaddress._BaseNetwork.broadcast_address
Expand Down Expand Up @@ -150,17 +153,6 @@ distutils.dist.DistributionMetadata.set_keywords
distutils.dist.DistributionMetadata.set_platforms
distutils.util.get_host_platform
email.headerregistry.MessageIDHeader.max_count
importlib.abc.TraversableResources
importlib.machinery.PathFinder.find_distributions
importlib.metadata.Distribution.name
importlib.metadata.EntryPoint.attr
importlib.metadata.EntryPoint.dist
importlib.metadata.EntryPoint.matches
importlib.metadata.EntryPoint.module
importlib.metadata.EntryPoint.pattern
importlib.metadata.PackageMetadata
importlib.resources.ResourceReader
importlib.util.source_hash
lib2to3.pgen2.tokenize.COLONEQUAL
multiprocessing.managers.SharedMemoryServer.create
multiprocessing.managers.SharedMemoryServer.list_segments
Expand Down
1 change: 1 addition & 0 deletions tests/stubtest_allowlists/py36.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum.Enum._generate_next_value_
fractions.Fraction.__new__ # overload is too complicated for stubtest to resolve
hmac.HMAC.__init__
importlib.metadata # Added in 3.8
importlib.metadata._meta # Added in 3.10
Copy link
Collaborator

@srittau srittau Dec 10, 2021

Choose a reason for hiding this comment

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

I think we can remove these allowlist overrides, once mypy 0.920 gets released, as it includes python/mypy#11069.

importlib.resources # Added in 3.7
io.StringIO.readline
ipaddress._BaseNetwork.__init__
Expand Down
2 changes: 1 addition & 1 deletion tests/stubtest_allowlists/py37.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fractions.Fraction.__new__ # overload is too complicated for stubtest to resolv
hmac.HMAC.__init__
http.server.SimpleHTTPRequestHandler.__init__ # *args is expanded
importlib.metadata # Added in 3.8
importlib.metadata._meta # Added in 3.10
ipaddress._BaseNetwork.__init__
json.loads
multiprocessing.shared_memory
Expand Down Expand Up @@ -110,7 +111,6 @@ dummy_threading.setprofile
dummy_threading.settrace
dummy_threading.stack_size
html.parser.HTMLParser.unescape
importlib.util.source_hash
lib2to3.pgen2.tokenize.COLONEQUAL
platform.popen
plistlib.Data.asBase64
Expand Down
4 changes: 1 addition & 3 deletions tests/stubtest_allowlists/py38.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ gettext.install # codeset default value is ['unspecified'] so can't be specifie
gettext.translation # codeset default value is ['unspecified'] so can't be specified
hmac.new # Stub is a white lie; see comments in the stub
http.server.SimpleHTTPRequestHandler.__init__ # *args is expanded
importlib.metadata._meta # Added in 3.10
ipaddress.IPv4Interface.hostmask
ipaddress.IPv6Interface.hostmask
ipaddress._BaseNetwork.broadcast_address
Expand Down Expand Up @@ -134,9 +135,6 @@ dummy_threading.settrace
dummy_threading.stack_size
email.headerregistry.MessageIDHeader.max_count
html.parser.HTMLParser.unescape
importlib.machinery.PathFinder.find_distributions
importlib.metadata.EntryPoint.pattern
importlib.util.source_hash
lib2to3.pgen2.tokenize.COLONEQUAL
multiprocessing.managers.SharedMemoryServer.create
multiprocessing.managers.SharedMemoryServer.list_segments
Expand Down
7 changes: 1 addition & 6 deletions tests/stubtest_allowlists/py39.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ gettext.translation
hmac.new # Stub is a white lie; see comments in the stub
http.server.SimpleHTTPRequestHandler.__init__ # *args is expanded
importlib.abc.Traversable.__init__ # Inherits __init__ from typing.Protocol
importlib.metadata._meta # Added in 3.10
ipaddress.IPv4Interface.hostmask
ipaddress.IPv6Interface.hostmask
ipaddress._BaseNetwork.broadcast_address
Expand Down Expand Up @@ -135,12 +136,6 @@ email.headerregistry.MessageIDHeader.max_count
hmac.HMAC.digest_cons
hmac.HMAC.inner
hmac.HMAC.outer
importlib.abc.TraversableResources
importlib.machinery.PathFinder.find_distributions
importlib.metadata.EntryPoint.attr
importlib.metadata.EntryPoint.module
importlib.metadata.EntryPoint.pattern
importlib.util.source_hash
lib2to3.pgen2.tokenize.COLONEQUAL
multiprocessing.managers.SharedMemoryServer.create
multiprocessing.managers.SharedMemoryServer.list_segments
Expand Down