From 0e50e8b1d0ac9c992b67e22ab6c9f348cd985255 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 24 May 2022 19:30:23 -0700 Subject: [PATCH 1/3] builtins: Fix unconstrained overloads in set() and frozenset() See https://github.com/microsoft/pyright/issues/3501#issuecomment-1135979479 Related to #7928 --- stdlib/builtins.pyi | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 53ba2a8a2a94..45db4edca77a 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -909,7 +909,7 @@ class list(MutableSequence[_T], Generic[_T]): class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): # __init__ should be kept roughly in line with `collections.UserDict.__init__`, which has similar semantics @overload - def __init__(self: dict[_KT, _VT]) -> None: ... + def __init__(self) -> None: ... @overload def __init__(self: dict[str, _VT], **kwargs: _VT) -> None: ... @overload @@ -962,6 +962,9 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): def __ior__(self: Self, __value: Iterable[tuple[_KT, _VT]]) -> Self: ... class set(MutableSet[_T], Generic[_T]): + @overload + def __init__(self) -> None: ... + @overload def __init__(self, __iterable: Iterable[_T] = ...) -> None: ... def add(self, __element: _T) -> None: ... def copy(self) -> set[_T]: ... @@ -998,6 +1001,9 @@ class set(MutableSet[_T], Generic[_T]): def __class_getitem__(cls, __item: Any) -> GenericAlias: ... class frozenset(AbstractSet[_T_co], Generic[_T_co]): + @overload + def __init__(self) -> None: ... + @overload def __init__(self, __iterable: Iterable[_T_co] = ...) -> None: ... def copy(self) -> frozenset[_T_co]: ... def difference(self, *s: Iterable[object]) -> frozenset[_T_co]: ... From 12138ac00446e9a36c86c51750ba79f6b9ea3d7d Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 24 May 2022 19:50:51 -0700 Subject: [PATCH 2/3] similar changes in collections --- stdlib/collections/__init__.pyi | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/stdlib/collections/__init__.pyi b/stdlib/collections/__init__.pyi index 2e88c0d8f474..647ee23071f8 100644 --- a/stdlib/collections/__init__.pyi +++ b/stdlib/collections/__init__.pyi @@ -43,7 +43,7 @@ class UserDict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): data: dict[_KT, _VT] # __init__ should be kept roughly in line with `dict.__init__`, which has the same semantics @overload - def __init__(self: UserDict[_KT, _VT], __dict: None = ...) -> None: ... + def __init__(self, __dict: None = ...) -> None: ... @overload def __init__(self: UserDict[str, _VT], __dict: None = ..., **kwargs: _VT) -> None: ... @overload @@ -82,7 +82,10 @@ class UserDict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): class UserList(MutableSequence[_T]): data: list[_T] - def __init__(self, initlist: Iterable[_T] | None = ...) -> None: ... + @overload + def __init__(self, initlist: None = ...) -> None: ... + @overload + def __init__(self, initlist: Iterable[_T]) -> None: ... def __lt__(self, other: list[_T] | UserList[_T]) -> bool: ... def __le__(self, other: list[_T] | UserList[_T]) -> bool: ... def __gt__(self, other: list[_T] | UserList[_T]) -> bool: ... @@ -214,7 +217,10 @@ class UserString(Sequence[UserString]): class deque(MutableSequence[_T], Generic[_T]): @property def maxlen(self) -> int | None: ... - def __init__(self, iterable: Iterable[_T] = ..., maxlen: int | None = ...) -> None: ... + @overload + def __init__(self, *, maxlen: int | None = ...) -> None: ... + @overload + def __init__(self, iterable: Iterable[_T], maxlen: int | None = ...) -> None: ... def append(self, __x: _T) -> None: ... def appendleft(self, __x: _T) -> None: ... def copy(self: Self) -> Self: ... @@ -248,7 +254,7 @@ class deque(MutableSequence[_T], Generic[_T]): class Counter(dict[_T, int], Generic[_T]): @overload - def __init__(self: Counter[_T], __iterable: None = ...) -> None: ... + def __init__(self, __iterable: None = ...) -> None: ... @overload def __init__(self: Counter[str], __iterable: None = ..., **kwargs: int) -> None: ... @overload @@ -340,7 +346,7 @@ class OrderedDict(dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]): class defaultdict(dict[_KT, _VT], Generic[_KT, _VT]): default_factory: Callable[[], _VT] | None @overload - def __init__(self: defaultdict[_KT, _VT]) -> None: ... + def __init__(self) -> None: ... @overload def __init__(self: defaultdict[str, _VT], **kwargs: _VT) -> None: ... @overload From 672637ec3772db40c6a30a5cd8f95e4437ce5cbf Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 24 May 2022 22:57:28 -0700 Subject: [PATCH 3/3] Apply suggestions from code review Thanks Alex! Co-authored-by: Alex Waygood --- stdlib/builtins.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 45db4edca77a..2e4456426333 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -965,7 +965,7 @@ class set(MutableSet[_T], Generic[_T]): @overload def __init__(self) -> None: ... @overload - def __init__(self, __iterable: Iterable[_T] = ...) -> None: ... + def __init__(self, __iterable: Iterable[_T]) -> None: ... def add(self, __element: _T) -> None: ... def copy(self) -> set[_T]: ... def difference(self, *s: Iterable[Any]) -> set[_T]: ... @@ -1004,7 +1004,7 @@ class frozenset(AbstractSet[_T_co], Generic[_T_co]): @overload def __init__(self) -> None: ... @overload - def __init__(self, __iterable: Iterable[_T_co] = ...) -> None: ... + def __init__(self, __iterable: Iterable[_T_co]) -> None: ... def copy(self) -> frozenset[_T_co]: ... def difference(self, *s: Iterable[object]) -> frozenset[_T_co]: ... def intersection(self, *s: Iterable[object]) -> frozenset[_T_co]: ...