Skip to content

Commit 0d7064c

Browse files
Make Mapping/MutableMapping params positional-only (#5772)
These are positional-only on dict, so it makes sense to mark them as positional-only in these base classes too. Fixes #5771
1 parent 7781027 commit 0d7064c

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

stdlib/collections/__init__.pyi

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class UserDict(MutableMapping[_KT, _VT]):
4141
def __delitem__(self, key: _KT) -> None: ...
4242
def __iter__(self) -> Iterator[_KT]: ...
4343
def __contains__(self, key: object) -> bool: ...
44-
def copy(self: _S) -> _S: ...
44+
def copy(self: Self) -> Self: ...
4545
@classmethod
46-
def fromkeys(cls: Type[_S], iterable: Iterable[_KT], value: _VT | None = ...) -> _S: ...
46+
def fromkeys(cls: Type[Self], iterable: Iterable[_KT], value: _VT | None = ...) -> Self: ...
4747

4848
class UserList(MutableSequence[_T]):
4949
data: list[_T]
@@ -208,7 +208,7 @@ class Counter(Dict[_T, int], Generic[_T]):
208208
def __init__(self, __mapping: Mapping[_T, int]) -> None: ...
209209
@overload
210210
def __init__(self, __iterable: Iterable[_T]) -> None: ...
211-
def copy(self: _S) -> _S: ...
211+
def copy(self: Self) -> Self: ...
212212
def elements(self) -> Iterator[_T]: ...
213213
def most_common(self, n: int | None = ...) -> list[tuple[_T, int]]: ...
214214
@classmethod
@@ -256,7 +256,7 @@ class _OrderedDictValuesView(dict_values[_KT_co, _VT_co], Reversible[_VT_co], Ge
256256
class OrderedDict(Dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]):
257257
def popitem(self, last: bool = ...) -> tuple[_KT, _VT]: ...
258258
def move_to_end(self, key: _KT, last: bool = ...) -> None: ...
259-
def copy(self: _S) -> _S: ...
259+
def copy(self: Self) -> Self: ...
260260
def __reversed__(self) -> Iterator[_KT]: ...
261261
def keys(self) -> _OrderedDictKeysView[_KT, _VT]: ...
262262
def items(self) -> _OrderedDictItemsView[_KT, _VT]: ...
@@ -296,3 +296,8 @@ class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
296296
def __iter__(self) -> Iterator[_KT]: ...
297297
def __len__(self) -> int: ...
298298
def __missing__(self, key: _KT) -> _VT: ... # undocumented
299+
def setdefault(self, key: _KT, default: _VT = ...) -> _VT: ...
300+
@overload
301+
def pop(self, key: _KT) -> _VT: ...
302+
@overload
303+
def pop(self, key: _KT, default: _VT | _T = ...) -> _VT | _T: ...

stdlib/typing.pyi

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -431,29 +431,29 @@ class Mapping(Collection[_KT], Generic[_KT, _VT_co]):
431431
# TODO: We wish the key type could also be covariant, but that doesn't work,
432432
# see discussion in https://github.com/python/typing/pull/273.
433433
@abstractmethod
434-
def __getitem__(self, k: _KT) -> _VT_co: ...
434+
def __getitem__(self, __k: _KT) -> _VT_co: ...
435435
# Mixin methods
436436
@overload
437437
def get(self, key: _KT) -> _VT_co | None: ...
438438
@overload
439-
def get(self, key: _KT, default: _VT_co | _T) -> _VT_co | _T: ...
439+
def get(self, __key: _KT, __default: _VT_co | _T) -> _VT_co | _T: ...
440440
def items(self) -> ItemsView[_KT, _VT_co]: ...
441441
def keys(self) -> KeysView[_KT]: ...
442442
def values(self) -> ValuesView[_VT_co]: ...
443-
def __contains__(self, o: object) -> bool: ...
443+
def __contains__(self, __o: object) -> bool: ...
444444

445445
class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):
446446
@abstractmethod
447-
def __setitem__(self, k: _KT, v: _VT) -> None: ...
447+
def __setitem__(self, __k: _KT, __v: _VT) -> None: ...
448448
@abstractmethod
449-
def __delitem__(self, v: _KT) -> None: ...
449+
def __delitem__(self, __v: _KT) -> None: ...
450450
def clear(self) -> None: ...
451451
@overload
452-
def pop(self, key: _KT) -> _VT: ...
452+
def pop(self, __key: _KT) -> _VT: ...
453453
@overload
454-
def pop(self, key: _KT, default: _VT | _T = ...) -> _VT | _T: ...
454+
def pop(self, __key: _KT, __default: _VT | _T = ...) -> _VT | _T: ...
455455
def popitem(self) -> tuple[_KT, _VT]: ...
456-
def setdefault(self, key: _KT, default: _VT = ...) -> _VT: ...
456+
def setdefault(self, __key: _KT, __default: _VT = ...) -> _VT: ...
457457
# 'update' used to take a Union, but using overloading is better.
458458
# The second overloaded type here is a bit too general, because
459459
# Mapping[Tuple[_KT, _VT], W] is a subclass of Iterable[Tuple[_KT, _VT]],

stdlib/weakref.pyi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class WeakValueDictionary(MutableMapping[_KT, _VT]):
4242
def items(self) -> Iterator[tuple[_KT, _VT]]: ... # type: ignore
4343
def itervaluerefs(self) -> Iterator[KeyedRef[_KT, _VT]]: ...
4444
def valuerefs(self) -> list[KeyedRef[_KT, _VT]]: ...
45+
def setdefault(self, key: _KT, default: _VT = ...) -> _VT: ...
46+
@overload
47+
def pop(self, key: _KT) -> _VT: ...
48+
@overload
49+
def pop(self, key: _KT, default: _VT | _T = ...) -> _VT | _T: ...
4550

4651
class KeyedRef(ref[_T], Generic[_KT, _T]):
4752
key: _KT
@@ -67,6 +72,11 @@ class WeakKeyDictionary(MutableMapping[_KT, _VT]):
6772
def values(self) -> Iterator[_VT]: ... # type: ignore
6873
def items(self) -> Iterator[tuple[_KT, _VT]]: ... # type: ignore
6974
def keyrefs(self) -> list[ref[_KT]]: ...
75+
def setdefault(self, key: _KT, default: _VT = ...) -> _VT: ...
76+
@overload
77+
def pop(self, key: _KT) -> _VT: ...
78+
@overload
79+
def pop(self, key: _KT, default: _VT | _T = ...) -> _VT | _T: ...
7080

7181
class finalize:
7282
def __init__(self, __obj: object, __func: Callable[..., Any], *args: Any, **kwargs: Any) -> None: ...

tests/stubtest_allowlists/py310.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ _collections_abc.Generator.throw
1313
_collections_abc.ItemsView.__reversed__
1414
_collections_abc.KeysView.__reversed__
1515
_collections_abc.ValuesView.__reversed__
16+
17+
# These are not positional-only at runtime, but we treat them
18+
# as positional-only to match dict.
19+
_collections_abc.Mapping.__getitem__
20+
_collections_abc.Mapping.__contains__
21+
_collections_abc.MutableMapping.__delitem__
22+
_collections_abc.MutableMapping.__setitem__
23+
_collections_abc.MutableMapping.pop
24+
_collections_abc.MutableMapping.setdefault
25+
1626
_dummy_thread
1727
ast.Bytes.__new__
1828
ast.Ellipsis.__new__

0 commit comments

Comments
 (0)