Skip to content

Commit 1ba0c98

Browse files
authored
Fix false positive with calling .register() on KeysView subclass (#9348)
* Revert "`Collection` is `Sized` (#8977)" This reverts commit 5bbba5d. * Revert "typing: remove metaclass from Sized (#9058)" This reverts commit a3ce512. * Add regression test for issue 9296.
1 parent a74df38 commit 1ba0c98

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

stdlib/typing.pyi

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ class SupportsRound(Protocol[_T_co]):
325325
def __round__(self, __ndigits: int) -> _T_co: ...
326326

327327
@runtime_checkable
328-
class Sized(Protocol):
328+
class Sized(Protocol, metaclass=ABCMeta):
329329
@abstractmethod
330330
def __len__(self) -> int: ...
331331

@@ -452,7 +452,10 @@ class Container(Protocol[_T_co]):
452452
def __contains__(self, __x: object) -> bool: ...
453453

454454
@runtime_checkable
455-
class Collection(Sized, Iterable[_T_co], Container[_T_co], Protocol[_T_co]): ...
455+
class Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]):
456+
# Implement Sized (but don't have it as a base class).
457+
@abstractmethod
458+
def __len__(self) -> int: ...
456459

457460
class Sequence(Collection[_T_co], Reversible[_T_co], Generic[_T_co]):
458461
@overload
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from __future__ import annotations
2+
3+
import typing as t
4+
5+
KT = t.TypeVar("KT")
6+
7+
8+
class MyKeysView(t.KeysView[KT]):
9+
pass
10+
11+
12+
d: dict[t.Any, t.Any] = {}
13+
dict_keys = type(d.keys())
14+
15+
# This should not cause an error like `Member "register" is unknown`:
16+
MyKeysView.register(dict_keys)

0 commit comments

Comments
 (0)