Skip to content

Commit 622df68

Browse files
Add @type_check_only to stub-only private classes in stdlib (#14512)
1 parent dde70ae commit 622df68

Some content is hidden

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

61 files changed

+188
-51
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/_msi.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import sys
2+
from typing import type_check_only
23

34
if sys.platform == "win32":
45
class MSIError(Exception): ...
56
# Actual typename View, not exposed by the implementation
7+
@type_check_only
68
class _View:
79
def Execute(self, params: _Record | None = ...) -> None: ...
810
def GetColumnInfo(self, kind: int) -> _Record: ...
@@ -14,6 +16,7 @@ if sys.platform == "win32":
1416
__init__: None # type: ignore[assignment]
1517

1618
# Actual typename SummaryInformation, not exposed by the implementation
19+
@type_check_only
1720
class _SummaryInformation:
1821
def GetProperty(self, field: int) -> int | bytes | None: ...
1922
def GetPropertyCount(self) -> int: ...
@@ -24,6 +27,7 @@ if sys.platform == "win32":
2427
__init__: None # type: ignore[assignment]
2528

2629
# Actual typename Database, not exposed by the implementation
30+
@type_check_only
2731
class _Database:
2832
def OpenView(self, sql: str) -> _View: ...
2933
def Commit(self) -> None: ...
@@ -34,6 +38,7 @@ if sys.platform == "win32":
3438
__init__: None # type: ignore[assignment]
3539

3640
# Actual typename Record, not exposed by the implementation
41+
@type_check_only
3742
class _Record:
3843
def GetFieldCount(self) -> int: ...
3944
def GetInteger(self, field: int) -> int: ...

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: 10 additions & 3 deletions
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
@@ -1698,8 +1699,14 @@ if sys.version_info >= (3, 12):
16981699
self, *, name: str = ..., default_value: expr | None = ..., **kwargs: Unpack[_Attributes[int]]
16991700
) -> Self: ...
17001701

1701-
class _ABC(type):
1702-
def __init__(cls, *args: Unused) -> None: ...
1702+
if sys.version_info >= (3, 14):
1703+
@type_check_only
1704+
class _ABC(type):
1705+
def __init__(cls, *args: Unused) -> None: ...
1706+
1707+
else:
1708+
class _ABC(type):
1709+
def __init__(cls, *args: Unused) -> None: ...
17031710

17041711
if sys.version_info < (3, 14):
17051712
@deprecated("Replaced by ast.Constant; removed in Python 3.14")

stdlib/asyncio/events.pyi

Lines changed: 5 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,9 @@ class AbstractEventLoop:
599600
@abstractmethod
600601
async def shutdown_default_executor(self) -> None: ...
601602

603+
# This class does not exist at runtime, but stubtest complains if it's marked as
604+
# @type_check_only because it has an alias that does exist at runtime. See mypy#19568.
605+
# @type_check_only
602606
class _AbstractEventLoopPolicy:
603607
@abstractmethod
604608
def get_event_loop(self) -> AbstractEventLoop: ...

0 commit comments

Comments
 (0)