Skip to content

Commit 23e7650

Browse files
committed
Add @type_check_only to private stdlib classes not available at runtime
1 parent a2c6ac0 commit 23e7650

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+168
-48
lines changed

stdlib/_compression.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
from _typeshed import Incomplete, WriteableBuffer
44
from collections.abc import Callable
55
from io import DEFAULT_BUFFER_SIZE, BufferedIOBase, RawIOBase
6-
from typing import Any, Protocol
6+
from typing import Any, Protocol, type_check_only
77

88
BUFFER_SIZE = DEFAULT_BUFFER_SIZE
99

10+
@type_check_only
1011
class _Reader(Protocol):
1112
def read(self, n: int, /) -> bytes: ...
1213
def seekable(self) -> bool: ...

stdlib/_ctypes.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ class _SimpleCData(_CData, Generic[_T], metaclass=_PyCSimpleType):
103103
def __init__(self, value: _T = ...) -> None: ... # pyright: ignore[reportInvalidTypeVarUse]
104104
def __ctypes_from_outparam__(self, /) -> _T: ... # type: ignore[override]
105105

106+
@type_check_only
106107
class _CanCastTo(_CData): ...
108+
109+
@type_check_only
107110
class _PointerLike(_CanCastTo): ...
108111

109112
# This type is not exposed. It calls itself _ctypes.PyCPointerType.

stdlib/_gdbm.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
from _typeshed import ReadOnlyBuffer, StrOrBytesPath
33
from types import TracebackType
4-
from typing import TypeVar, overload
4+
from typing import TypeVar, overload, type_check_only
55
from typing_extensions import Self, TypeAlias
66

77
if sys.platform != "win32":
@@ -13,6 +13,7 @@ if sys.platform != "win32":
1313

1414
class error(OSError): ...
1515
# Actual typename gdbm, not exposed by the implementation
16+
@type_check_only
1617
class _gdbm:
1718
def firstkey(self) -> bytes | None: ...
1819
def nextkey(self, key: _KeyType) -> bytes | None: ...

stdlib/_io.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class BytesIO(BufferedIOBase, _BufferedIOBase, BinaryIO): # type: ignore[misc]
8888
def readlines(self, size: int | None = None, /) -> list[bytes]: ...
8989
def seek(self, pos: int, whence: int = 0, /) -> int: ...
9090

91+
@type_check_only
9192
class _BufferedReaderStream(Protocol):
9293
def read(self, n: int = ..., /) -> bytes: ...
9394
# Optional: def readall(self) -> bytes: ...

stdlib/_operator.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class _SupportsDunderGT(Protocol):
2828
class _SupportsDunderLE(Protocol):
2929
def __le__(self, other: Any, /) -> Any: ...
3030

31+
@type_check_only
3132
class _SupportsDunderGE(Protocol):
3233
def __ge__(self, other: Any, /) -> Any: ...
3334

stdlib/_pickle.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ from pickle import PickleBuffer as PickleBuffer
44
from typing import Any, Protocol, type_check_only
55
from typing_extensions import TypeAlias
66

7+
@type_check_only
78
class _ReadableFileobj(Protocol):
89
def read(self, n: int, /) -> bytes: ...
910
def readline(self) -> bytes: ...

stdlib/_ssl.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ from ssl import (
1212
SSLWantWriteError as SSLWantWriteError,
1313
SSLZeroReturnError as SSLZeroReturnError,
1414
)
15-
from typing import Any, ClassVar, Literal, TypedDict, final, overload
15+
from typing import Any, ClassVar, Literal, TypedDict, final, overload, type_check_only
1616
from typing_extensions import NotRequired, Self, TypeAlias
1717

1818
_PasswordType: TypeAlias = Callable[[], str | bytes | bytearray] | str | bytes | bytearray
1919
_PCTRTT: TypeAlias = tuple[tuple[str, str], ...]
2020
_PCTRTTT: TypeAlias = tuple[_PCTRTT, ...]
2121
_PeerCertRetDictType: TypeAlias = dict[str, str | _PCTRTTT | _PCTRTT]
2222

23+
@type_check_only
2324
class _Cipher(TypedDict):
2425
aead: bool
2526
alg_bits: int
@@ -33,6 +34,7 @@ class _Cipher(TypedDict):
3334
strength_bits: int
3435
symmetric: str
3536

37+
@type_check_only
3638
class _CertInfo(TypedDict):
3739
subject: tuple[tuple[tuple[str, str], ...], ...]
3840
issuer: tuple[tuple[tuple[str, str], ...], ...]

stdlib/ast.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ from _ast import (
1010
)
1111
from _typeshed import ReadableBuffer, Unused
1212
from collections.abc import Iterable, Iterator, Sequence
13-
from typing import Any, ClassVar, Generic, Literal, TypedDict, TypeVar as _TypeVar, overload
13+
from typing import Any, ClassVar, Generic, Literal, TypedDict, TypeVar as _TypeVar, overload, type_check_only
1414
from typing_extensions import Self, Unpack, deprecated
1515

1616
if sys.version_info >= (3, 13):
@@ -20,6 +20,7 @@ if sys.version_info >= (3, 13):
2020
_EndPositionT = typing_extensions.TypeVar("_EndPositionT", int, int | None, default=int | None)
2121

2222
# Corresponds to the names in the `_attributes` class variable which is non-empty in certain AST nodes
23+
@type_check_only
2324
class _Attributes(TypedDict, Generic[_EndPositionT], total=False):
2425
lineno: int
2526
col_offset: int

stdlib/asyncio/events.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ from collections.abc import Callable, Sequence
1212
from concurrent.futures import Executor
1313
from contextvars import Context
1414
from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
15-
from typing import IO, Any, Literal, Protocol, TypeVar, overload
15+
from typing import IO, Any, Literal, Protocol, TypeVar, overload, type_check_only
1616
from typing_extensions import Self, TypeAlias, TypeVarTuple, Unpack, deprecated
1717

1818
from . import _AwaitableLike, _CoroutineLike
@@ -68,6 +68,7 @@ _ExceptionHandler: TypeAlias = Callable[[AbstractEventLoop, _Context], object]
6868
_ProtocolFactory: TypeAlias = Callable[[], BaseProtocol]
6969
_SSLContext: TypeAlias = bool | None | ssl.SSLContext
7070

71+
@type_check_only
7172
class _TaskFactory(Protocol):
7273
def __call__(self, loop: AbstractEventLoop, factory: _CoroutineLike[_T], /) -> Future[_T]: ...
7374

@@ -599,6 +600,7 @@ class AbstractEventLoop:
599600
@abstractmethod
600601
async def shutdown_default_executor(self) -> None: ...
601602

603+
@type_check_only
602604
class _AbstractEventLoopPolicy:
603605
@abstractmethod
604606
def get_event_loop(self) -> AbstractEventLoop: ...

stdlib/asyncio/format_helpers.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import sys
33
import traceback
44
from collections.abc import Iterable
55
from types import FrameType, FunctionType
6-
from typing import Any, overload
6+
from typing import Any, overload, type_check_only
77
from typing_extensions import TypeAlias
88

9+
@type_check_only
910
class _HasWrapper:
1011
__wrapper__: _HasWrapper | FunctionType
1112

0 commit comments

Comments
 (0)