Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib/@tests/stubtest_allowlists/darwin-py310.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ xml.etree.cElementTree.XMLPullParser.flush
xml.parsers.expat.XMLParserType.GetReparseDeferralEnabled
xml.parsers.expat.XMLParserType.SetReparseDeferralEnabled
xml.sax.expatreader.ExpatParser.flush
zipfile.ZipInfo.__slots__


# =============================================================
Expand Down
1 change: 1 addition & 0 deletions stdlib/@tests/stubtest_allowlists/darwin-py39.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ xml.etree.cElementTree.XMLPullParser.flush
xml.parsers.expat.XMLParserType.GetReparseDeferralEnabled
xml.parsers.expat.XMLParserType.SetReparseDeferralEnabled
xml.sax.expatreader.ExpatParser.flush
zipfile.ZipInfo.__slots__


# =============================================================
Expand Down
2 changes: 1 addition & 1 deletion stdlib/@tests/stubtest_allowlists/win32-py310.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ xml.etree.cElementTree.XMLPullParser.flush
xml.parsers.expat.XMLParserType.GetReparseDeferralEnabled
xml.parsers.expat.XMLParserType.SetReparseDeferralEnabled
xml.sax.expatreader.ExpatParser.flush

zipfile.ZipInfo.__slots__

# =============================================================
# Allowlist entries that cannot or should not be fixed; <= 3.12
Expand Down
2 changes: 1 addition & 1 deletion stdlib/@tests/stubtest_allowlists/win32-py39.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ xml.etree.cElementTree.XMLPullParser.flush
xml.parsers.expat.XMLParserType.GetReparseDeferralEnabled
xml.parsers.expat.XMLParserType.SetReparseDeferralEnabled
xml.sax.expatreader.ExpatParser.flush

zipfile.ZipInfo.__slots__

# =============================================================
# Allowlist entries that cannot or should not be fixed; <= 3.12
Expand Down
1 change: 1 addition & 0 deletions stdlib/_collections_abc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,6 @@ class dict_items(ItemsView[_KT_co, _VT_co]): # undocumented
if sys.version_info >= (3, 12):
@runtime_checkable
class Buffer(Protocol):
__slots__ = ()
@abstractmethod
def __buffer__(self, flags: int, /) -> memoryview: ...
2 changes: 2 additions & 0 deletions stdlib/_threading_local.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ __all__ = ["local"]
_LocalDict: TypeAlias = dict[Any, Any]

class _localimpl:
__slots__ = ("key", "dicts", "localargs", "locallock", "__weakref__")
key: str
dicts: dict[int, tuple[ReferenceType[Any], _LocalDict]]
# Keep localargs in sync with the *args, **kwargs annotation on local.__new__
Expand All @@ -16,6 +17,7 @@ class _localimpl:
def create_dict(self) -> _LocalDict: ...

class local:
__slots__ = ("_local__impl", "__dict__")
def __new__(cls, /, *args: Any, **kw: Any) -> Self: ...
def __getattribute__(self, name: str) -> Any: ...
def __setattr__(self, name: str, value: Any) -> None: ...
Expand Down
2 changes: 2 additions & 0 deletions stdlib/asyncio/events.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class _TaskFactory(Protocol):
def __call__(self, loop: AbstractEventLoop, factory: _CoroutineLike[_T], /) -> Future[_T]: ...

class Handle:
__slots__ = ("_callback", "_args", "_cancelled", "_loop", "_source_traceback", "_repr", "__weakref__", "_context")
_cancelled: bool
_args: Sequence[Any]
def __init__(
Expand All @@ -85,6 +86,7 @@ class Handle:
def get_context(self) -> Context: ...

class TimerHandle(Handle):
__slots__ = ["_scheduled", "_when"]
def __init__(
self,
when: float,
Expand Down
6 changes: 6 additions & 0 deletions stdlib/asyncio/protocols.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@ from typing import Any
__all__ = ("BaseProtocol", "Protocol", "DatagramProtocol", "SubprocessProtocol", "BufferedProtocol")

class BaseProtocol:
__slots__ = ()
def connection_made(self, transport: transports.BaseTransport) -> None: ...
def connection_lost(self, exc: Exception | None) -> None: ...
def pause_writing(self) -> None: ...
def resume_writing(self) -> None: ...

class Protocol(BaseProtocol):
# Need annotation or mypy will complain about 'Cannot determine type of "__slots__" in base class'
__slots__: tuple[()] = ()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be

Suggested change
__slots__: tuple[()] = ()
__slots__: ClassVar[tuple[()]] = ()

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe Final.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe Final.

That would imply that you can't override it in subclasses, which seems questionable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right. Well, if type checkers used standard inheritance rules you couldn't override this anyway since no other type is a subtype of tuple[()].

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this makes mypy unhappy, it now also wants you to use a ClassVar annotation in all child classes. I'm inclined to go back to the bare tuple[()] annotation unless it causes concrete problems with some type checker.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that's fine

def data_received(self, data: bytes) -> None: ...
def eof_received(self) -> bool | None: ...

class BufferedProtocol(BaseProtocol):
__slots__ = ()
def get_buffer(self, sizehint: int) -> ReadableBuffer: ...
def buffer_updated(self, nbytes: int) -> None: ...
def eof_received(self) -> bool | None: ...

class DatagramProtocol(BaseProtocol):
__slots__ = ()
def connection_made(self, transport: transports.DatagramTransport) -> None: ... # type: ignore[override]
# addr can be a tuple[int, int] for some unusual protocols like socket.AF_NETLINK.
# Use tuple[str | Any, int] to not cause typechecking issues on most usual cases.
Expand All @@ -30,6 +35,7 @@ class DatagramProtocol(BaseProtocol):
def error_received(self, exc: Exception) -> None: ...

class SubprocessProtocol(BaseProtocol):
__slots__: tuple[()] = ()
def pipe_data_received(self, fd: int, data: bytes) -> None: ...
def pipe_connection_lost(self, fd: int, exc: Exception | None) -> None: ...
def process_exited(self) -> None: ...
9 changes: 8 additions & 1 deletion stdlib/asyncio/transports.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ from typing import Any
__all__ = ("BaseTransport", "ReadTransport", "WriteTransport", "Transport", "DatagramTransport", "SubprocessTransport")

class BaseTransport:
__slots__ = ("_extra",)
def __init__(self, extra: Mapping[str, Any] | None = None) -> None: ...
def get_extra_info(self, name: str, default: Any = None) -> Any: ...
def is_closing(self) -> bool: ...
Expand All @@ -16,11 +17,13 @@ class BaseTransport:
def get_protocol(self) -> BaseProtocol: ...

class ReadTransport(BaseTransport):
__slots__ = ()
def is_reading(self) -> bool: ...
def pause_reading(self) -> None: ...
def resume_reading(self) -> None: ...

class WriteTransport(BaseTransport):
__slots__ = ()
def set_write_buffer_limits(self, high: int | None = None, low: int | None = None) -> None: ...
def get_write_buffer_size(self) -> int: ...
def get_write_buffer_limits(self) -> tuple[int, int]: ...
Expand All @@ -32,13 +35,16 @@ class WriteTransport(BaseTransport):
def can_write_eof(self) -> bool: ...
def abort(self) -> None: ...

class Transport(ReadTransport, WriteTransport): ...
class Transport(ReadTransport, WriteTransport):
__slots__ = ()

class DatagramTransport(BaseTransport):
__slots__ = ()
def sendto(self, data: bytes | bytearray | memoryview, addr: _Address | None = None) -> None: ...
def abort(self) -> None: ...

class SubprocessTransport(BaseTransport):
__slots__ = ()
def get_pid(self) -> int: ...
def get_returncode(self) -> int | None: ...
def get_pipe_transport(self, fd: int) -> BaseTransport | None: ...
Expand All @@ -47,4 +53,5 @@ class SubprocessTransport(BaseTransport):
def kill(self) -> None: ...

class _FlowControlMixin(Transport):
__slots__ = ("_loop", "_protocol_paused", "_high_water", "_low_water")
def __init__(self, extra: Mapping[str, Any] | None = None, loop: AbstractEventLoop | None = None) -> None: ...
1 change: 1 addition & 0 deletions stdlib/asyncio/trsock.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ _WriteBuffer: TypeAlias = bytearray | memoryview
_CMSG: TypeAlias = tuple[int, int, bytes]

class TransportSocket:
__slots__ = ("_sock",)
def __init__(self, sock: socket.socket) -> None: ...
@property
def family(self) -> int: ...
Expand Down
2 changes: 2 additions & 0 deletions stdlib/contextlib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ _CM_EF = TypeVar("_CM_EF", bound=AbstractContextManager[Any, Any] | _ExitFunc)
# allowlist for use as a Protocol.
@runtime_checkable
class AbstractContextManager(ABC, Protocol[_T_co, _ExitT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
__slots__ = ()
def __enter__(self) -> _T_co: ...
@abstractmethod
def __exit__(
Expand All @@ -58,6 +59,7 @@ class AbstractContextManager(ABC, Protocol[_T_co, _ExitT_co]): # type: ignore[m
# allowlist for use as a Protocol.
@runtime_checkable
class AbstractAsyncContextManager(ABC, Protocol[_T_co, _ExitT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
__slots__ = ()
async def __aenter__(self) -> _T_co: ...
@abstractmethod
async def __aexit__(
Expand Down
8 changes: 6 additions & 2 deletions stdlib/ctypes/_endian.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ from ctypes import Structure, Union

# At runtime, the native endianness is an alias for Structure,
# while the other is a subclass with a metaclass added in.
class BigEndianStructure(Structure): ...
class BigEndianStructure(Structure):
__slots__ = ()

class LittleEndianStructure(Structure): ...

# Same thing for these: one is an alias of Union at runtime
if sys.version_info >= (3, 11):
class BigEndianUnion(Union): ...
class BigEndianUnion(Union):
__slots__ = ()

class LittleEndianUnion(Union): ...
32 changes: 32 additions & 0 deletions stdlib/dataclasses.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,37 @@ class _DefaultFactory(Protocol[_T_co]):
def __call__(self) -> _T_co: ...

class Field(Generic[_T]):
if sys.version_info >= (3, 14):
__slots__ = (
"name",
"type",
"default",
"default_factory",
"repr",
"hash",
"init",
"compare",
"metadata",
"kw_only",
"doc",
"_field_type",
)
elif sys.version_info >= (3, 10):
__slots__ = (
"name",
"type",
"default",
"default_factory",
"repr",
"hash",
"init",
"compare",
"metadata",
"kw_only",
"_field_type",
)
else:
__slots__ = ("name", "type", "default", "default_factory", "repr", "hash", "init", "compare", "metadata", "_field_type")
name: str
type: Type[_T] | str | Any
default: _T | Literal[_MISSING_TYPE.MISSING]
Expand Down Expand Up @@ -355,6 +386,7 @@ def is_dataclass(obj: object) -> TypeIs[DataclassInstance | type[DataclassInstan
class FrozenInstanceError(AttributeError): ...

class InitVar(Generic[_T]):
__slots__ = ("type",)
type: Type[_T]
def __init__(self, type: Type[_T]) -> None: ...
@overload
Expand Down
1 change: 1 addition & 0 deletions stdlib/fractions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class _ConvertibleToIntegerRatio(Protocol):
def as_integer_ratio(self) -> tuple[int | Rational, int | Rational]: ...

class Fraction(Rational):
__slots__ = ("_numerator", "_denominator")
@overload
def __new__(cls, numerator: int | Rational = 0, denominator: int | Rational | None = None) -> Self: ...
@overload
Expand Down
1 change: 1 addition & 0 deletions stdlib/hmac.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def new(key: bytes | bytearray, msg: ReadableBuffer | None, digestmod: _DigestMo
def new(key: bytes | bytearray, *, digestmod: _DigestMod) -> HMAC: ...

class HMAC:
__slots__ = ("_hmac", "_inner", "_outer", "block_size", "digest_size")
digest_size: int
block_size: int
@property
Expand Down
1 change: 1 addition & 0 deletions stdlib/importlib/metadata/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class EntryPoint(_EntryPointBase):

if sys.version_info >= (3, 12):
class EntryPoints(tuple[EntryPoint, ...]):
__slots__ = ()
def __getitem__(self, name: str) -> EntryPoint: ... # type: ignore[override]
def select(
self,
Expand Down
3 changes: 3 additions & 0 deletions stdlib/inspect.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ class _void: ...
class _empty: ...

class Signature:
__slots__ = ("_return_annotation", "_parameters")
def __init__(
self, parameters: Sequence[Parameter] | None = None, *, return_annotation: Any = ..., __validate_parameters__: bool = True
) -> None: ...
Expand Down Expand Up @@ -416,6 +417,7 @@ if sys.version_info >= (3, 12):
def getasyncgenlocals(agen: AsyncGeneratorType[Any, Any]) -> dict[str, Any]: ...

class Parameter:
__slots__ = ("_name", "_kind", "_default", "_annotation")
def __init__(self, name: str, kind: _ParameterKind, *, default: Any = ..., annotation: Any = ...) -> None: ...
empty = _empty

Expand Down Expand Up @@ -447,6 +449,7 @@ class Parameter:
def __hash__(self) -> int: ...

class BoundArguments:
__slots__ = ("arguments", "_signature", "__weakref__")
arguments: OrderedDict[str, Any]
@property
def args(self) -> tuple[Any, ...]: ...
Expand Down
6 changes: 6 additions & 0 deletions stdlib/ipaddress.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def ip_interface(
) -> IPv4Interface | IPv6Interface: ...

class _IPAddressBase:
__slots__ = ()
@property
def compressed(self) -> str: ...
@property
Expand All @@ -33,6 +34,7 @@ class _IPAddressBase:
def version(self) -> int: ...

class _BaseAddress(_IPAddressBase):
__slots__ = ()
def __add__(self, other: int) -> Self: ...
def __hash__(self) -> int: ...
def __int__(self) -> int: ...
Expand Down Expand Up @@ -105,6 +107,7 @@ class _BaseNetwork(_IPAddressBase, Generic[_A]):
def hostmask(self) -> _A: ...

class _BaseV4:
__slots__ = ()
if sys.version_info >= (3, 14):
version: Final = 4
max_prefixlen: Final = 32
Expand All @@ -115,6 +118,7 @@ class _BaseV4:
def max_prefixlen(self) -> Literal[32]: ...

class IPv4Address(_BaseV4, _BaseAddress):
__slots__ = ("_ip", "__weakref__")
def __init__(self, address: object) -> None: ...
@property
def is_global(self) -> bool: ...
Expand Down Expand Up @@ -156,6 +160,7 @@ class IPv4Interface(IPv4Address):
def with_prefixlen(self) -> str: ...

class _BaseV6:
__slots__ = ()
if sys.version_info >= (3, 14):
version: Final = 6
max_prefixlen: Final = 128
Expand All @@ -166,6 +171,7 @@ class _BaseV6:
def max_prefixlen(self) -> Literal[128]: ...

class IPv6Address(_BaseV6, _BaseAddress):
__slots__ = ("_ip", "_scope_id", "__weakref__")
def __init__(self, address: object) -> None: ...
@property
def is_global(self) -> bool: ...
Expand Down
1 change: 1 addition & 0 deletions stdlib/multiprocessing/managers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Namespace:
_Namespace: TypeAlias = Namespace

class Token:
__slots__ = ("typeid", "address", "id")
typeid: str | bytes | None
address: _Address | None
id: str | bytes | int | None
Expand Down
5 changes: 5 additions & 0 deletions stdlib/numbers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ class _IntegralLike(_RealLike, Protocol):
#################

class Number(metaclass=ABCMeta):
__slots__ = ()
@abstractmethod
def __hash__(self) -> int: ...

# See comment at the top of the file
# for why some of these return types are purposefully vague
class Complex(Number, _ComplexLike):
__slots__ = ()
@abstractmethod
def __complex__(self) -> complex: ...
def __bool__(self) -> bool: ...
Expand Down Expand Up @@ -109,6 +111,7 @@ class Complex(Number, _ComplexLike):
# See comment at the top of the file
# for why some of these return types are purposefully vague
class Real(Complex, _RealLike):
__slots__ = ()
@abstractmethod
def __float__(self) -> float: ...
@abstractmethod
Expand Down Expand Up @@ -153,6 +156,7 @@ class Real(Complex, _RealLike):
# See comment at the top of the file
# for why some of these return types are purposefully vague
class Rational(Real):
__slots__ = ()
@property
@abstractmethod
def numerator(self) -> _IntegralLike: ...
Expand All @@ -164,6 +168,7 @@ class Rational(Real):
# See comment at the top of the file
# for why some of these return types are purposefully vague
class Integral(Rational, _IntegralLike):
__slots__ = ()
@abstractmethod
def __int__(self) -> int: ...
def __index__(self) -> int: ...
Expand Down
1 change: 1 addition & 0 deletions stdlib/os/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,7 @@ In the future, this property will contain the last metadata change time."""
# on the allowlist for use as a Protocol starting in 3.14.
@runtime_checkable
class PathLike(ABC, Protocol[AnyStr_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
__slots__ = ()
@abstractmethod
def __fspath__(self) -> AnyStr_co: ...

Expand Down
Loading
Loading