From 298d3411a8572cda4d5b16ad002726a06e1df1a9 Mon Sep 17 00:00:00 2001 From: Avasam Date: Sun, 12 May 2024 15:13:37 -0400 Subject: [PATCH 1/3] keep using types._LoaderProtocol in pkg_resources --- stdlib/types.pyi | 6 +++++- stubs/setuptools/pkg_resources/__init__.pyi | 5 +++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/stdlib/types.pyi b/stdlib/types.pyi index 38940b4345c8..9aac148e7318 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -17,7 +17,7 @@ from collections.abc import ( from importlib.machinery import ModuleSpec # pytype crashes if types.MappingProxyType inherits from collections.abc.Mapping instead of typing.Mapping -from typing import Any, ClassVar, Literal, Mapping, TypeVar, final, overload # noqa: Y022 +from typing import Any, ClassVar, Literal, Mapping, Protocol, TypeVar, final, overload # noqa: Y022 from typing_extensions import ParamSpec, Self, TypeVarTuple, deprecated __all__ = [ @@ -312,6 +312,10 @@ class SimpleNamespace: def __setattr__(self, name: str, value: Any, /) -> None: ... def __delattr__(self, name: str, /) -> None: ... +# TODO: Remove this symbol and its usages in 3rd-party stubs once mypy has included it in its vendored typeshed +class _LoaderProtocol(Protocol): + def load_module(self, fullname: str, /) -> ModuleType: ... + class ModuleType: __name__: str __file__: str | None diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index d48dd86f6a0e..8ecc43559474 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -1,12 +1,12 @@ import types import zipimport from _typeshed import Incomplete, StrPath, Unused -from _typeshed.importlib import LoaderProtocol from collections.abc import Callable, Generator, Iterable, Iterator, Sequence from io import BytesIO from itertools import chain from pkgutil import get_importer as get_importer from re import Pattern +from types import _LoaderProtocol from typing import IO, Any, ClassVar, Final, Literal, NoReturn, Protocol, TypeVar, overload, type_check_only from typing_extensions import Self, TypeAlias from zipfile import ZipInfo @@ -360,7 +360,8 @@ def evaluate_marker(text: str, extra: Incomplete | None = None) -> bool: ... class NullProvider: egg_name: str | None egg_info: str | None - loader: LoaderProtocol | None + # TODO: Use _typeshed.importlib.LoaderProtocol once mypy has included it in its vendored typeshed + loader: _LoaderProtocol | None module_path: str | None def __init__(self, module: _ModuleLike) -> None: ... From b5b59c334929fc4abc661624a3c1ad0a5e8ffaa4 Mon Sep 17 00:00:00 2001 From: Avasam Date: Sun, 12 May 2024 15:19:11 -0400 Subject: [PATCH 2/3] noqa: Y046 --- stdlib/types.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/types.pyi b/stdlib/types.pyi index 9aac148e7318..85bf8fc464a8 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -313,7 +313,7 @@ class SimpleNamespace: def __delattr__(self, name: str, /) -> None: ... # TODO: Remove this symbol and its usages in 3rd-party stubs once mypy has included it in its vendored typeshed -class _LoaderProtocol(Protocol): +class _LoaderProtocol(Protocol): # noqa: Y046 def load_module(self, fullname: str, /) -> ModuleType: ... class ModuleType: From e29e485c0c2c6e3e1f477cb7cf7738fd5f0ce673 Mon Sep 17 00:00:00 2001 From: Avasam Date: Sun, 12 May 2024 15:25:10 -0400 Subject: [PATCH 3/3] Move protocol to pkg_resources --- stdlib/types.pyi | 6 +----- stubs/setuptools/pkg_resources/__init__.pyi | 6 ++++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/stdlib/types.pyi b/stdlib/types.pyi index 85bf8fc464a8..38940b4345c8 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -17,7 +17,7 @@ from collections.abc import ( from importlib.machinery import ModuleSpec # pytype crashes if types.MappingProxyType inherits from collections.abc.Mapping instead of typing.Mapping -from typing import Any, ClassVar, Literal, Mapping, Protocol, TypeVar, final, overload # noqa: Y022 +from typing import Any, ClassVar, Literal, Mapping, TypeVar, final, overload # noqa: Y022 from typing_extensions import ParamSpec, Self, TypeVarTuple, deprecated __all__ = [ @@ -312,10 +312,6 @@ class SimpleNamespace: def __setattr__(self, name: str, value: Any, /) -> None: ... def __delattr__(self, name: str, /) -> None: ... -# TODO: Remove this symbol and its usages in 3rd-party stubs once mypy has included it in its vendored typeshed -class _LoaderProtocol(Protocol): # noqa: Y046 - def load_module(self, fullname: str, /) -> ModuleType: ... - class ModuleType: __name__: str __file__: str | None diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index 8ecc43559474..6f99b8f671af 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -6,13 +6,16 @@ from io import BytesIO from itertools import chain from pkgutil import get_importer as get_importer from re import Pattern -from types import _LoaderProtocol from typing import IO, Any, ClassVar, Final, Literal, NoReturn, Protocol, TypeVar, overload, type_check_only from typing_extensions import Self, TypeAlias from zipfile import ZipInfo from ._vendored_packaging import requirements as packaging_requirements, version as packaging_version +# TODO: Use _typeshed.importlib.LoaderProtocol once mypy has included it in its vendored typeshed +class _LoaderProtocol(Protocol): + def load_module(self, fullname: str, /) -> types.ModuleType: ... + _T = TypeVar("_T") _D = TypeVar("_D", bound=Distribution) _NestedStr: TypeAlias = str | Iterable[_NestedStr] @@ -360,7 +363,6 @@ def evaluate_marker(text: str, extra: Incomplete | None = None) -> bool: ... class NullProvider: egg_name: str | None egg_info: str | None - # TODO: Use _typeshed.importlib.LoaderProtocol once mypy has included it in its vendored typeshed loader: _LoaderProtocol | None module_path: str | None