diff --git a/stdlib/@tests/stubtest_allowlists/py314.txt b/stdlib/@tests/stubtest_allowlists/py314.txt index dd93165c5..824326339 100644 --- a/stdlib/@tests/stubtest_allowlists/py314.txt +++ b/stdlib/@tests/stubtest_allowlists/py314.txt @@ -21,13 +21,6 @@ fractions.Fraction.__rpow__ gzip.GzipFile.readinto gzip.GzipFile.readinto1 gzip.compress -importlib.abc.ResourceReader -importlib.abc.Traversable -importlib.abc.TraversableResources -importlib.machinery.__all__ -importlib.machinery.AppleFrameworkLoader -importlib.util.__all__ -importlib.util.Loader multiprocessing.forkserver.main multiprocessing.managers.BaseListProxy.clear multiprocessing.managers.BaseListProxy.copy @@ -93,6 +86,7 @@ typing.NewType.__mro_entries__ builtins.ellipsis # type is not exposed anywhere importlib._abc.Loader.exec_module # See Lib/importlib/_abc.py. Might be defined for backwards compatibility +importlib.util.Loader.exec_module # positional-only complaints caused by differences between typing aliases and the "real" classes in the stdlib _collections_abc.Coroutine.send diff --git a/stdlib/@tests/test_cases/check_importlib.py b/stdlib/@tests/test_cases/check_importlib.py index 72aec6bd3..3f226b5db 100644 --- a/stdlib/@tests/test_cases/check_importlib.py +++ b/stdlib/@tests/test_cases/check_importlib.py @@ -1,6 +1,5 @@ from __future__ import annotations -import importlib.abc import importlib.util import pathlib import sys @@ -10,9 +9,14 @@ from importlib.machinery import ModuleSpec from types import ModuleType from typing_extensions import Self +if sys.version_info >= (3, 11): + from importlib.resources.abc import Traversable +else: + from importlib.abc import Traversable + # Assert that some Path classes are Traversable. -def traverse(t: importlib.abc.Traversable) -> None: +def traverse(t: Traversable) -> None: pass diff --git a/stdlib/importlib/abc.pyi b/stdlib/importlib/abc.pyi index 3016a3a43..cf0fd0807 100644 --- a/stdlib/importlib/abc.pyi +++ b/stdlib/importlib/abc.pyi @@ -113,63 +113,71 @@ class FileLoader(_bootstrap_external.FileLoader, ResourceLoader, ExecutionLoader def get_filename(self, name: str | None = None) -> str: ... def load_module(self, name: str | None = None) -> types.ModuleType: ... -class ResourceReader(metaclass=ABCMeta): - @abstractmethod - def open_resource(self, resource: str) -> IO[bytes]: ... - @abstractmethod - def resource_path(self, resource: str) -> str: ... - if sys.version_info >= (3, 10): +if sys.version_info < (3, 11): + class ResourceReader(metaclass=ABCMeta): @abstractmethod + def open_resource(self, resource: str) -> IO[bytes]: ... + @abstractmethod + def resource_path(self, resource: str) -> str: ... + if sys.version_info >= (3, 10): + @abstractmethod + def is_resource(self, path: str) -> bool: ... + else: + @abstractmethod + def is_resource(self, name: str) -> bool: ... + + @abstractmethod + def contents(self) -> Iterator[str]: ... + + @runtime_checkable + class Traversable(Protocol): + @abstractmethod + def is_dir(self) -> bool: ... + @abstractmethod + def is_file(self) -> bool: ... + @abstractmethod + def iterdir(self) -> Iterator[Traversable]: ... + if sys.version_info >= (3, 11): + @abstractmethod + def joinpath(self, *descendants: str) -> Traversable: ... + else: + @abstractmethod + def joinpath(self, child: str, /) -> Traversable: ... + + # The documentation and runtime protocol allows *args, **kwargs arguments, + # but this would mean that all implementers would have to support them, + # which is not the case. + @overload + @abstractmethod + def open(self, mode: Literal["r"] = "r", *, encoding: str | None = None, errors: str | None = None) -> IO[str]: ... + @overload + @abstractmethod + def open(self, mode: Literal["rb"]) -> IO[bytes]: ... + @property + @abstractmethod + def name(self) -> str: ... + if sys.version_info >= (3, 10): + def __truediv__(self, child: str, /) -> Traversable: ... + else: + @abstractmethod + def __truediv__(self, child: str, /) -> Traversable: ... + + @abstractmethod + def read_bytes(self) -> bytes: ... + @abstractmethod + def read_text(self, encoding: str | None = None) -> str: ... + + class TraversableResources(ResourceReader): + @abstractmethod + def files(self) -> Traversable: ... + def open_resource(self, resource: str) -> BufferedReader: ... + def resource_path(self, resource: Any) -> str: ... def is_resource(self, path: str) -> bool: ... - else: - @abstractmethod - def is_resource(self, name: str) -> bool: ... + def contents(self) -> Iterator[str]: ... - @abstractmethod - def contents(self) -> Iterator[str]: ... - -@runtime_checkable -class Traversable(Protocol): - @abstractmethod - def is_dir(self) -> bool: ... - @abstractmethod - def is_file(self) -> bool: ... - @abstractmethod - def iterdir(self) -> Iterator[Traversable]: ... - if sys.version_info >= (3, 11): - @abstractmethod - def joinpath(self, *descendants: str) -> Traversable: ... - else: - @abstractmethod - def joinpath(self, child: str, /) -> Traversable: ... - - # The documentation and runtime protocol allows *args, **kwargs arguments, - # but this would mean that all implementers would have to support them, - # which is not the case. - @overload - @abstractmethod - def open(self, mode: Literal["r"] = "r", *, encoding: str | None = None, errors: str | None = None) -> IO[str]: ... - @overload - @abstractmethod - def open(self, mode: Literal["rb"]) -> IO[bytes]: ... - @property - @abstractmethod - def name(self) -> str: ... - if sys.version_info >= (3, 10): - def __truediv__(self, child: str, /) -> Traversable: ... - else: - @abstractmethod - def __truediv__(self, child: str, /) -> Traversable: ... - - @abstractmethod - def read_bytes(self) -> bytes: ... - @abstractmethod - def read_text(self, encoding: str | None = None) -> str: ... - -class TraversableResources(ResourceReader): - @abstractmethod - def files(self) -> Traversable: ... - def open_resource(self, resource: str) -> BufferedReader: ... - def resource_path(self, resource: Any) -> str: ... - def is_resource(self, path: str) -> bool: ... - def contents(self) -> Iterator[str]: ... +elif sys.version_info < (3, 14): + from importlib.resources.abc import ( + ResourceReader as ResourceReader, + Traversable as Traversable, + TraversableResources as TraversableResources, + ) diff --git a/stdlib/importlib/machinery.pyi b/stdlib/importlib/machinery.pyi index bb1a6f93d..767046b70 100644 --- a/stdlib/importlib/machinery.pyi +++ b/stdlib/importlib/machinery.pyi @@ -16,5 +16,28 @@ from importlib._bootstrap_external import ( if sys.version_info >= (3, 11): from importlib._bootstrap_external import NamespaceLoader as NamespaceLoader +if sys.version_info >= (3, 14): + from importlib._bootstrap_external import AppleFrameworkLoader as AppleFrameworkLoader def all_suffixes() -> list[str]: ... + +if sys.version_info >= (3, 14): + __all__ = [ + "AppleFrameworkLoader", + "BYTECODE_SUFFIXES", + "BuiltinImporter", + "DEBUG_BYTECODE_SUFFIXES", + "EXTENSION_SUFFIXES", + "ExtensionFileLoader", + "FileFinder", + "FrozenImporter", + "ModuleSpec", + "NamespaceLoader", + "OPTIMIZED_BYTECODE_SUFFIXES", + "PathFinder", + "SOURCE_SUFFIXES", + "SourceFileLoader", + "SourcelessFileLoader", + "WindowsRegistryFinder", + "all_suffixes", + ] diff --git a/stdlib/importlib/resources/__init__.pyi b/stdlib/importlib/resources/__init__.pyi index 2cf6366b6..e672a619b 100644 --- a/stdlib/importlib/resources/__init__.pyi +++ b/stdlib/importlib/resources/__init__.pyi @@ -2,12 +2,16 @@ import os import sys from collections.abc import Iterator from contextlib import AbstractContextManager -from importlib.abc import Traversable from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, Literal, TextIO from typing_extensions import TypeAlias +if sys.version_info >= (3, 11): + from importlib.resources.abc import Traversable +else: + from importlib.abc import Traversable + if sys.version_info >= (3, 11): from importlib.resources._common import Package as Package else: @@ -72,5 +76,7 @@ if sys.version_info >= (3, 11): else: def files(package: Package) -> Traversable: ... -if sys.version_info >= (3, 10): +if sys.version_info >= (3, 11): + from importlib.resources.abc import ResourceReader as ResourceReader +elif sys.version_info >= (3, 10): from importlib.abc import ResourceReader as ResourceReader diff --git a/stdlib/importlib/resources/_common.pyi b/stdlib/importlib/resources/_common.pyi index d6a943654..3dd961bb6 100644 --- a/stdlib/importlib/resources/_common.pyi +++ b/stdlib/importlib/resources/_common.pyi @@ -5,7 +5,7 @@ if sys.version_info >= (3, 11): import types from collections.abc import Callable from contextlib import AbstractContextManager - from importlib.abc import ResourceReader, Traversable + from importlib.resources.abc import ResourceReader, Traversable from pathlib import Path from typing import Literal, overload from typing_extensions import TypeAlias, deprecated diff --git a/stdlib/importlib/resources/abc.pyi b/stdlib/importlib/resources/abc.pyi index ad80605f7..fe0fe64db 100644 --- a/stdlib/importlib/resources/abc.pyi +++ b/stdlib/importlib/resources/abc.pyi @@ -1,14 +1,69 @@ import sys +from abc import ABCMeta, abstractmethod +from collections.abc import Iterator +from io import BufferedReader +from typing import IO, Any, Literal, Protocol, overload, runtime_checkable if sys.version_info >= (3, 11): - # These are all actually defined in this file on 3.11+, - # and re-exported from importlib.abc, - # but it's much less code duplication for typeshed if we pretend that they're still defined - # in importlib.abc on 3.11+, and re-exported from this file - from importlib.abc import ( - ResourceReader as ResourceReader, - Traversable as Traversable, - TraversableResources as TraversableResources, - ) + class ResourceReader(metaclass=ABCMeta): + @abstractmethod + def open_resource(self, resource: str) -> IO[bytes]: ... + @abstractmethod + def resource_path(self, resource: str) -> str: ... + if sys.version_info >= (3, 10): + @abstractmethod + def is_resource(self, path: str) -> bool: ... + else: + @abstractmethod + def is_resource(self, name: str) -> bool: ... + + @abstractmethod + def contents(self) -> Iterator[str]: ... + + @runtime_checkable + class Traversable(Protocol): + @abstractmethod + def is_dir(self) -> bool: ... + @abstractmethod + def is_file(self) -> bool: ... + @abstractmethod + def iterdir(self) -> Iterator[Traversable]: ... + if sys.version_info >= (3, 11): + @abstractmethod + def joinpath(self, *descendants: str) -> Traversable: ... + else: + @abstractmethod + def joinpath(self, child: str, /) -> Traversable: ... + + # The documentation and runtime protocol allows *args, **kwargs arguments, + # but this would mean that all implementers would have to support them, + # which is not the case. + @overload + @abstractmethod + def open(self, mode: Literal["r"] = "r", *, encoding: str | None = None, errors: str | None = None) -> IO[str]: ... + @overload + @abstractmethod + def open(self, mode: Literal["rb"]) -> IO[bytes]: ... + @property + @abstractmethod + def name(self) -> str: ... + if sys.version_info >= (3, 10): + def __truediv__(self, child: str, /) -> Traversable: ... + else: + @abstractmethod + def __truediv__(self, child: str, /) -> Traversable: ... + + @abstractmethod + def read_bytes(self) -> bytes: ... + @abstractmethod + def read_text(self, encoding: str | None = None) -> str: ... + + class TraversableResources(ResourceReader): + @abstractmethod + def files(self) -> Traversable: ... + def open_resource(self, resource: str) -> BufferedReader: ... + def resource_path(self, resource: Any) -> str: ... + def is_resource(self, path: str) -> bool: ... + def contents(self) -> Iterator[str]: ... __all__ = ["ResourceReader", "Traversable", "TraversableResources"] diff --git a/stdlib/importlib/util.pyi b/stdlib/importlib/util.pyi index cc1c98ae4..370a08623 100644 --- a/stdlib/importlib/util.pyi +++ b/stdlib/importlib/util.pyi @@ -1,4 +1,3 @@ -import importlib.abc import importlib.machinery import sys import types @@ -12,6 +11,7 @@ from importlib._bootstrap_external import ( source_from_cache as source_from_cache, spec_from_file_location as spec_from_file_location, ) +from importlib.abc import Loader from typing_extensions import ParamSpec _P = ParamSpec("_P") @@ -24,10 +24,26 @@ if sys.version_info < (3, 12): def resolve_name(name: str, package: str | None) -> str: ... def find_spec(name: str, package: str | None = None) -> importlib.machinery.ModuleSpec | None: ... -class LazyLoader(importlib.abc.Loader): - def __init__(self, loader: importlib.abc.Loader) -> None: ... +class LazyLoader(Loader): + def __init__(self, loader: Loader) -> None: ... @classmethod - def factory(cls, loader: importlib.abc.Loader) -> Callable[..., LazyLoader]: ... + def factory(cls, loader: Loader) -> Callable[..., LazyLoader]: ... def exec_module(self, module: types.ModuleType) -> None: ... def source_hash(source_bytes: ReadableBuffer) -> bytes: ... + +if sys.version_info >= (3, 14): + __all__ = [ + "LazyLoader", + "Loader", + "MAGIC_NUMBER", + "cache_from_source", + "decode_source", + "find_spec", + "module_from_spec", + "resolve_name", + "source_from_cache", + "source_hash", + "spec_from_file_location", + "spec_from_loader", + ] diff --git a/stdlib/zipimport.pyi b/stdlib/zipimport.pyi index 3e94c681b..4aab318e7 100644 --- a/stdlib/zipimport.pyi +++ b/stdlib/zipimport.pyi @@ -1,10 +1,14 @@ import sys from _typeshed import StrOrBytesPath -from importlib.abc import ResourceReader from importlib.machinery import ModuleSpec from types import CodeType, ModuleType from typing_extensions import deprecated +if sys.version_info >= (3, 10): + from importlib.readers import ZipReader +else: + from importlib.abc import ResourceReader + if sys.version_info >= (3, 10): from _frozen_importlib_external import _LoaderBasics else: @@ -29,7 +33,13 @@ class zipimporter(_LoaderBasics): def get_code(self, fullname: str) -> CodeType: ... def get_data(self, pathname: str) -> bytes: ... def get_filename(self, fullname: str) -> str: ... - def get_resource_reader(self, fullname: str) -> ResourceReader | None: ... # undocumented + if sys.version_info >= (3, 14): + def get_resource_reader(self, fullname: str) -> ZipReader: ... # undocumented + elif sys.version_info >= (3, 10): + def get_resource_reader(self, fullname: str) -> ZipReader | None: ... # undocumented + else: + def get_resource_reader(self, fullname: str) -> ResourceReader | None: ... # undocumented + def get_source(self, fullname: str) -> str | None: ... def is_package(self, fullname: str) -> bool: ... @deprecated("Deprecated since 3.10; use exec_module() instead") diff --git a/stubs/networkx/networkx/generators/atlas.pyi b/stubs/networkx/networkx/generators/atlas.pyi index 5f55b29e5..d90e9a738 100644 --- a/stubs/networkx/networkx/generators/atlas.pyi +++ b/stubs/networkx/networkx/generators/atlas.pyi @@ -1,8 +1,13 @@ -from importlib.abc import Traversable +import sys from typing import Final from networkx.utils.backends import _dispatchable +if sys.version_info >= (3, 11): + from importlib.resources.abc import Traversable +else: + from importlib.abc import Traversable + __all__ = ["graph_atlas", "graph_atlas_g"] NUM_GRAPHS: Final = 1253