Skip to content

Commit f32d9f0

Browse files
stdlib: add __slots__ (python#14611)
1 parent 28abff1 commit f32d9f0

38 files changed

+247
-12
lines changed

stdlib/@tests/stubtest_allowlists/darwin-py310.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ xml.etree.cElementTree.XMLPullParser.flush
4141
xml.parsers.expat.XMLParserType.GetReparseDeferralEnabled
4242
xml.parsers.expat.XMLParserType.SetReparseDeferralEnabled
4343
xml.sax.expatreader.ExpatParser.flush
44+
zipfile.ZipInfo.__slots__
4445

4546

4647
# =============================================================

stdlib/@tests/stubtest_allowlists/darwin-py39.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ xml.etree.cElementTree.XMLPullParser.flush
5050
xml.parsers.expat.XMLParserType.GetReparseDeferralEnabled
5151
xml.parsers.expat.XMLParserType.SetReparseDeferralEnabled
5252
xml.sax.expatreader.ExpatParser.flush
53+
zipfile.ZipInfo.__slots__
5354

5455

5556
# =============================================================

stdlib/@tests/stubtest_allowlists/win32-py310.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ xml.etree.cElementTree.XMLPullParser.flush
4242
xml.parsers.expat.XMLParserType.GetReparseDeferralEnabled
4343
xml.parsers.expat.XMLParserType.SetReparseDeferralEnabled
4444
xml.sax.expatreader.ExpatParser.flush
45-
45+
zipfile.ZipInfo.__slots__
4646

4747
# =============================================================
4848
# Allowlist entries that cannot or should not be fixed; <= 3.12

stdlib/@tests/stubtest_allowlists/win32-py39.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ xml.etree.cElementTree.XMLPullParser.flush
5151
xml.parsers.expat.XMLParserType.GetReparseDeferralEnabled
5252
xml.parsers.expat.XMLParserType.SetReparseDeferralEnabled
5353
xml.sax.expatreader.ExpatParser.flush
54-
54+
zipfile.ZipInfo.__slots__
5555

5656
# =============================================================
5757
# Allowlist entries that cannot or should not be fixed; <= 3.12

stdlib/_collections_abc.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,6 @@ class dict_items(ItemsView[_KT_co, _VT_co]): # undocumented
103103
if sys.version_info >= (3, 12):
104104
@runtime_checkable
105105
class Buffer(Protocol):
106+
__slots__ = ()
106107
@abstractmethod
107108
def __buffer__(self, flags: int, /) -> memoryview: ...

stdlib/_threading_local.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ __all__ = ["local"]
77
_LocalDict: TypeAlias = dict[Any, Any]
88

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

1819
class local:
20+
__slots__ = ("_local__impl", "__dict__")
1921
def __new__(cls, /, *args: Any, **kw: Any) -> Self: ...
2022
def __getattribute__(self, name: str) -> Any: ...
2123
def __setattr__(self, name: str, value: Any) -> None: ...

stdlib/asyncio/events.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class _TaskFactory(Protocol):
7373
def __call__(self, loop: AbstractEventLoop, factory: _CoroutineLike[_T], /) -> Future[_T]: ...
7474

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

8788
class TimerHandle(Handle):
89+
__slots__ = ["_scheduled", "_when"]
8890
def __init__(
8991
self,
9092
when: float,

stdlib/asyncio/protocols.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@ from typing import Any
66
__all__ = ("BaseProtocol", "Protocol", "DatagramProtocol", "SubprocessProtocol", "BufferedProtocol")
77

88
class BaseProtocol:
9+
__slots__ = ()
910
def connection_made(self, transport: transports.BaseTransport) -> None: ...
1011
def connection_lost(self, exc: Exception | None) -> None: ...
1112
def pause_writing(self) -> None: ...
1213
def resume_writing(self) -> None: ...
1314

1415
class Protocol(BaseProtocol):
16+
# Need annotation or mypy will complain about 'Cannot determine type of "__slots__" in base class'
17+
__slots__: tuple[()] = ()
1518
def data_received(self, data: bytes) -> None: ...
1619
def eof_received(self) -> bool | None: ...
1720

1821
class BufferedProtocol(BaseProtocol):
22+
__slots__ = ()
1923
def get_buffer(self, sizehint: int) -> ReadableBuffer: ...
2024
def buffer_updated(self, nbytes: int) -> None: ...
2125
def eof_received(self) -> bool | None: ...
2226

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

3237
class SubprocessProtocol(BaseProtocol):
38+
__slots__: tuple[()] = ()
3339
def pipe_data_received(self, fd: int, data: bytes) -> None: ...
3440
def pipe_connection_lost(self, fd: int, exc: Exception | None) -> None: ...
3541
def process_exited(self) -> None: ...

stdlib/asyncio/transports.pyi

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ from typing import Any
88
__all__ = ("BaseTransport", "ReadTransport", "WriteTransport", "Transport", "DatagramTransport", "SubprocessTransport")
99

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

1819
class ReadTransport(BaseTransport):
20+
__slots__ = ()
1921
def is_reading(self) -> bool: ...
2022
def pause_reading(self) -> None: ...
2123
def resume_reading(self) -> None: ...
2224

2325
class WriteTransport(BaseTransport):
26+
__slots__ = ()
2427
def set_write_buffer_limits(self, high: int | None = None, low: int | None = None) -> None: ...
2528
def get_write_buffer_size(self) -> int: ...
2629
def get_write_buffer_limits(self) -> tuple[int, int]: ...
@@ -32,13 +35,16 @@ class WriteTransport(BaseTransport):
3235
def can_write_eof(self) -> bool: ...
3336
def abort(self) -> None: ...
3437

35-
class Transport(ReadTransport, WriteTransport): ...
38+
class Transport(ReadTransport, WriteTransport):
39+
__slots__ = ()
3640

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

4146
class SubprocessTransport(BaseTransport):
47+
__slots__ = ()
4248
def get_pid(self) -> int: ...
4349
def get_returncode(self) -> int | None: ...
4450
def get_pipe_transport(self, fd: int) -> BaseTransport | None: ...
@@ -47,4 +53,5 @@ class SubprocessTransport(BaseTransport):
4753
def kill(self) -> None: ...
4854

4955
class _FlowControlMixin(Transport):
56+
__slots__ = ("_loop", "_protocol_paused", "_high_water", "_low_water")
5057
def __init__(self, extra: Mapping[str, Any] | None = None, loop: AbstractEventLoop | None = None) -> None: ...

stdlib/asyncio/trsock.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ _WriteBuffer: TypeAlias = bytearray | memoryview
1414
_CMSG: TypeAlias = tuple[int, int, bytes]
1515

1616
class TransportSocket:
17+
__slots__ = ("_sock",)
1718
def __init__(self, sock: socket.socket) -> None: ...
1819
@property
1920
def family(self) -> int: ...

0 commit comments

Comments
 (0)