From 1ba0c9815e4950bf0a608d49ebc21818c51d2eaf Mon Sep 17 00:00:00 2001 From: Joshua Bronson Date: Sat, 10 Dec 2022 13:09:28 -0500 Subject: [PATCH] Fix false positive with calling .register() on KeysView subclass (#9348) * Revert "`Collection` is `Sized` (#8977)" This reverts commit 5bbba5d008ffe6f26ffe4fb9e7c7b3e0765a2b0a. * Revert "typing: remove metaclass from Sized (#9058)" This reverts commit a3ce51209544d64d03018b3927051375f1bebe82. * Add regression test for issue 9296. --- stdlib/typing.pyi | 7 +++++-- .../stdlib/typing/check_regression_issue_9296.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 test_cases/stdlib/typing/check_regression_issue_9296.py diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index cc27ae7db..71018003b 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -325,7 +325,7 @@ class SupportsRound(Protocol[_T_co]): def __round__(self, __ndigits: int) -> _T_co: ... @runtime_checkable -class Sized(Protocol): +class Sized(Protocol, metaclass=ABCMeta): @abstractmethod def __len__(self) -> int: ... @@ -452,7 +452,10 @@ class Container(Protocol[_T_co]): def __contains__(self, __x: object) -> bool: ... @runtime_checkable -class Collection(Sized, Iterable[_T_co], Container[_T_co], Protocol[_T_co]): ... +class Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]): + # Implement Sized (but don't have it as a base class). + @abstractmethod + def __len__(self) -> int: ... class Sequence(Collection[_T_co], Reversible[_T_co], Generic[_T_co]): @overload diff --git a/test_cases/stdlib/typing/check_regression_issue_9296.py b/test_cases/stdlib/typing/check_regression_issue_9296.py new file mode 100644 index 000000000..34c5631ae --- /dev/null +++ b/test_cases/stdlib/typing/check_regression_issue_9296.py @@ -0,0 +1,16 @@ +from __future__ import annotations + +import typing as t + +KT = t.TypeVar("KT") + + +class MyKeysView(t.KeysView[KT]): + pass + + +d: dict[t.Any, t.Any] = {} +dict_keys = type(d.keys()) + +# This should not cause an error like `Member "register" is unknown`: +MyKeysView.register(dict_keys)