Skip to content

Commit f88a09d

Browse files
Add @type_check_only to various typeshed-only procotols in stdlib (#14465)
Mark various typeshed-only protocols as `@type_check_only` in stdlib
1 parent e2d0c45 commit f88a09d

15 files changed

+42
-19
lines changed

stdlib/_operator.pyi

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import sys
22
from _typeshed import SupportsGetItem
33
from collections.abc import Callable, Container, Iterable, MutableMapping, MutableSequence, Sequence
44
from operator import attrgetter as attrgetter, itemgetter as itemgetter, methodcaller as methodcaller
5-
from typing import Any, AnyStr, Protocol, SupportsAbs, SupportsIndex, TypeVar, overload
5+
from typing import Any, AnyStr, Protocol, SupportsAbs, SupportsIndex, TypeVar, overload, type_check_only
66
from typing_extensions import ParamSpec, TypeAlias, TypeIs
77

88
_R = TypeVar("_R")
@@ -16,12 +16,15 @@ _P = ParamSpec("_P")
1616
# operators can be overloaded to return an arbitrary object. For example,
1717
# the numpy.array comparison dunders return another numpy.array.
1818

19+
@type_check_only
1920
class _SupportsDunderLT(Protocol):
2021
def __lt__(self, other: Any, /) -> Any: ...
2122

23+
@type_check_only
2224
class _SupportsDunderGT(Protocol):
2325
def __gt__(self, other: Any, /) -> Any: ...
2426

27+
@type_check_only
2528
class _SupportsDunderLE(Protocol):
2629
def __le__(self, other: Any, /) -> Any: ...
2730

@@ -30,12 +33,15 @@ class _SupportsDunderGE(Protocol):
3033

3134
_SupportsComparison: TypeAlias = _SupportsDunderLE | _SupportsDunderGE | _SupportsDunderGT | _SupportsDunderLT
3235

36+
@type_check_only
3337
class _SupportsInversion(Protocol[_T_co]):
3438
def __invert__(self) -> _T_co: ...
3539

40+
@type_check_only
3641
class _SupportsNeg(Protocol[_T_co]):
3742
def __neg__(self) -> _T_co: ...
3843

44+
@type_check_only
3945
class _SupportsPos(Protocol[_T_co]):
4046
def __pos__(self) -> _T_co: ...
4147

stdlib/argparse.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import sys
22
from _typeshed import SupportsWrite, sentinel
33
from collections.abc import Callable, Generator, Iterable, Sequence
44
from re import Pattern
5-
from typing import IO, Any, ClassVar, Final, Generic, NoReturn, Protocol, TypeVar, overload
5+
from typing import IO, Any, ClassVar, Final, Generic, NoReturn, Protocol, TypeVar, overload, type_check_only
66
from typing_extensions import Self, TypeAlias, deprecated
77

88
__all__ = [
@@ -112,6 +112,7 @@ class _ActionsContainer:
112112
def _handle_conflict_error(self, action: Action, conflicting_actions: Iterable[tuple[str, Action]]) -> NoReturn: ...
113113
def _handle_conflict_resolve(self, action: Action, conflicting_actions: Iterable[tuple[str, Action]]) -> None: ...
114114

115+
@type_check_only
115116
class _FormatterClass(Protocol):
116117
def __call__(self, *, prog: str) -> HelpFormatter: ...
117118

stdlib/builtins.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,11 @@ class complex:
452452
@classmethod
453453
def from_number(cls, number: complex | SupportsComplex | SupportsFloat | SupportsIndex, /) -> Self: ...
454454

455+
@type_check_only
455456
class _FormatMapMapping(Protocol):
456457
def __getitem__(self, key: str, /) -> Any: ...
457458

459+
@type_check_only
458460
class _TranslateTable(Protocol):
459461
def __getitem__(self, key: int, /) -> str | int | None: ...
460462

@@ -1355,7 +1357,7 @@ def chr(i: int | SupportsIndex, /) -> str: ...
13551357

13561358
if sys.version_info >= (3, 10):
13571359
def aiter(async_iterable: SupportsAiter[_SupportsAnextT_co], /) -> _SupportsAnextT_co: ...
1358-
1360+
@type_check_only
13591361
class _SupportsSynchronousAnext(Protocol[_AwaitableT_co]):
13601362
def __anext__(self) -> _AwaitableT_co: ...
13611363

stdlib/cgi.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from builtins import list as _list, type as _type
33
from collections.abc import Iterable, Iterator, Mapping
44
from email.message import Message
55
from types import TracebackType
6-
from typing import IO, Any, Protocol
6+
from typing import IO, Any, Protocol, type_check_only
77
from typing_extensions import Self
88

99
__all__ = [
@@ -31,7 +31,7 @@ def parse(
3131
def parse_multipart(
3232
fp: IO[Any], pdict: SupportsGetItem[str, bytes], encoding: str = "utf-8", errors: str = "replace", separator: str = "&"
3333
) -> dict[str, list[Any]]: ...
34-
34+
@type_check_only
3535
class _Environ(Protocol):
3636
def __getitem__(self, k: str, /) -> str: ...
3737
def keys(self) -> Iterable[str]: ...

stdlib/compileall.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import sys
22
from _typeshed import StrPath
33
from py_compile import PycInvalidationMode
4-
from typing import Any, Protocol
4+
from typing import Any, Protocol, type_check_only
55

66
__all__ = ["compile_dir", "compile_file", "compile_path"]
77

8+
@type_check_only
89
class _SupportsSearch(Protocol):
910
def search(self, string: str, /) -> Any: ...
1011

stdlib/contextlib.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ from _typeshed import FileDescriptorOrPath, Unused
44
from abc import ABC, abstractmethod
55
from collections.abc import AsyncGenerator, AsyncIterator, Awaitable, Callable, Generator, Iterator
66
from types import TracebackType
7-
from typing import IO, Any, Generic, Protocol, TypeVar, overload, runtime_checkable
7+
from typing import IO, Any, Generic, Protocol, TypeVar, overload, runtime_checkable, type_check_only
88
from typing_extensions import ParamSpec, Self, TypeAlias
99

1010
__all__ = [
@@ -112,7 +112,7 @@ else:
112112
) -> bool | None: ...
113113

114114
def asynccontextmanager(func: Callable[_P, AsyncIterator[_T_co]]) -> Callable[_P, _AsyncGeneratorContextManager[_T_co]]: ...
115-
115+
@type_check_only
116116
class _SupportsClose(Protocol):
117117
def close(self) -> object: ...
118118

@@ -123,6 +123,7 @@ class closing(AbstractContextManager[_SupportsCloseT, None]):
123123
def __exit__(self, *exc_info: Unused) -> None: ...
124124

125125
if sys.version_info >= (3, 10):
126+
@type_check_only
126127
class _SupportsAclose(Protocol):
127128
def aclose(self) -> Awaitable[object]: ...
128129

stdlib/copy.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import sys
2-
from typing import Any, Protocol, TypeVar
2+
from typing import Any, Protocol, TypeVar, type_check_only
33
from typing_extensions import Self
44

55
__all__ = ["Error", "copy", "deepcopy"]
66

77
_T = TypeVar("_T")
88
_SR = TypeVar("_SR", bound=_SupportsReplace)
99

10+
@type_check_only
1011
class _SupportsReplace(Protocol):
1112
# In reality doesn't support args, but there's no other great way to express this.
1213
def __replace__(self, *args: Any, **kwargs: Any) -> Self: ...

stdlib/fileinput.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import sys
22
from _typeshed import AnyStr_co, StrOrBytesPath
33
from collections.abc import Callable, Iterable
44
from types import GenericAlias, TracebackType
5-
from typing import IO, Any, AnyStr, Generic, Literal, Protocol, overload
5+
from typing import IO, Any, AnyStr, Generic, Literal, Protocol, overload, type_check_only
66
from typing_extensions import Self, TypeAlias
77

88
__all__ = [
@@ -25,6 +25,7 @@ if sys.version_info >= (3, 11):
2525
else:
2626
_TextMode: TypeAlias = Literal["r", "rU", "U"]
2727

28+
@type_check_only
2829
class _HasReadlineAndFileno(Protocol[AnyStr_co]):
2930
def readline(self) -> AnyStr_co: ...
3031
def fileno(self) -> int: ...

stdlib/fractions.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import sys
22
from collections.abc import Callable
33
from decimal import Decimal
44
from numbers import Rational, Real
5-
from typing import Any, Literal, Protocol, SupportsIndex, overload
5+
from typing import Any, Literal, Protocol, SupportsIndex, overload, type_check_only
66
from typing_extensions import Self, TypeAlias
77

88
_ComparableNum: TypeAlias = int | float | Decimal | Real
99

1010
__all__ = ["Fraction"]
1111

12+
@type_check_only
1213
class _ConvertibleToIntegerRatio(Protocol):
1314
def as_integer_ratio(self) -> tuple[int | Rational, int | Rational]: ...
1415

stdlib/gettext.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import io
22
import sys
33
from _typeshed import StrPath
44
from collections.abc import Callable, Container, Iterable, Sequence
5-
from typing import Any, Final, Literal, Protocol, TypeVar, overload
5+
from typing import Any, Final, Literal, Protocol, TypeVar, overload, type_check_only
66

77
__all__ = [
88
"NullTranslations",
@@ -26,6 +26,7 @@ __all__ = [
2626
if sys.version_info < (3, 11):
2727
__all__ += ["bind_textdomain_codeset", "ldgettext", "ldngettext", "lgettext", "lngettext"]
2828

29+
@type_check_only
2930
class _TranslationsReader(Protocol):
3031
def read(self) -> bytes: ...
3132
# optional:

0 commit comments

Comments
 (0)