FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

allow object type in dict.get and dict.pop by randolf-scholz · Pull Request #15471 · python/typeshed · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension .py  (1) .pyi  (6) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Next Next commit
allow object type in dict.get and dict.pop
  • Loading branch information
randolf-scholz committed Aug 25, 2026
commit 152fee56ae355c7bc42fd9dea33b43fbd2ea76aa
12 changes: 11 additions & 1 deletion stdlib/@tests/test_cases/builtins/check_dict.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import os
from typing import Any, Dict, Generic, Iterable, Mapping, TypeVar, Union
from typing import Any, Dict, Generic, Iterable, Literal, Mapping, TypeVar, Union
from typing_extensions import Self, assert_type

###################################################################
Expand Down Expand Up @@ -103,6 +103,16 @@ def test_iterable_tuple_overload(x: Iterable[tuple[int, str]]) -> dict[int, str]
result = d_str.get("key", int_value) # type: ignore[arg-type]


def test_dict_signature_overlapping_type(d: dict[Literal["foo", "bar"], int], key: str) -> None:
# Note: annotations also allow using keys of a disjoint type (e.g., int),
# linters / type checkers are free to issue warnings in such cases.
# statically, a .get(arg) is superfluous if the intersection of the
# dict key type and the argument type is empty.
# So we only test a case with non-empty intersection here.
d.get(key)
d.pop(key)


# Return values also make things weird

# Pyright doesn't have a version of no-any-return,
Expand Down
16 changes: 10 additions & 6 deletions stdlib/builtins.pyi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1336,19 +1336,23 @@ class dict(MutableMapping[_KT, _VT]):
def fromkeys(cls, iterable: Iterable[_T], value: _S, /) -> dict[_T, _S]: ...

# Positional-only in dict, but not in MutableMapping
# dict.get allows arbitrary types, which matches runtime semantics.
# linters may choose to warn if the given type does not overlap with the key type
@overload # type: ignore[override]
def get(self, key: _KT, default: None = None, /) -> _VT | None: ...
def get(self, key: object, default: None = None, /) -> _VT | None: ...
@overload
def get(self, key: _KT, default: _VT, /) -> _VT: ...
def get(self, key: object, default: _VT, /) -> _VT: ...
@overload
def get(self, key: _KT, default: _T, /) -> _VT | _T: ...
def get(self, key: object, default: _T, /) -> _VT | _T: ...

# dict.pop allows arbitrary types, which matches runtime semantics.
# linters may choose to warn if the given type does not overlap with the key type
@overload
def pop(self, key: _KT, /) -> _VT: ...
def pop(self, key: object, /) -> _VT: ...
@overload
def pop(self, key: _KT, default: _VT, /) -> _VT: ...
def pop(self, key: object, default: _VT, /) -> _VT: ...
@overload
def pop(self, key: _KT, default: _T, /) -> _VT | _T: ...
def pop(self, key: object, default: _T, /) -> _VT | _T: ...

def __len__(self) -> int: ...
def __getitem__(self, key: _KT, /) -> _VT: ...
Expand Down
6 changes: 3 additions & 3 deletions stdlib/collections/__init__.pyi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,11 @@ class OrderedDict(dict[_KT, _VT]):

# Same as dict.pop, but accepts keyword arguments
@overload
def pop(self, key: _KT) -> _VT: ...
def pop(self, key: object) -> _VT: ...
@overload
def pop(self, key: _KT, default: _VT) -> _VT: ...
def pop(self, key: object, default: _VT) -> _VT: ...
@overload
def pop(self, key: _KT, default: _T) -> _VT | _T: ...
def pop(self, key: object, default: _T) -> _VT | _T: ...

def __eq__(self, value: object, /) -> bool: ...

Expand Down
2 changes: 1 addition & 1 deletion stdlib/mailbox.pyi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Mailbox(Generic[_MessageT_co]):
def clear(self) -> None: ...

@overload
def pop(self, key: str, default: None = None) -> _MessageT_co | None: ...
def pop(self, key: str) -> _MessageT_co | None: ...
@overload
def pop(self, key: str, default: _T) -> _MessageT_co | _T: ...

Expand Down
4 changes: 2 additions & 2 deletions stubs/boltons/boltons/cacheutils.pyi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class LRI(dict[_KT, _VT]):
def __delitem__(self, key: _KT) -> None: ...

@overload
def pop(self, key: _KT) -> _VT: ...
def pop(self, key: object) -> _VT: ...
@overload
def pop(self, key: _KT, default: _T) -> _T | _VT: ...
def pop(self, key: object, default: _T) -> _T | _VT: ...

def popitem(self) -> tuple[_KT, _VT]: ...
def clear(self) -> None: ...
Expand Down
2 changes: 1 addition & 1 deletion stubs/boltons/boltons/dictutils.pyi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class OneToOne(dict[_KT, _VT]):
inv: OneToOne[_VT, _KT]
def clear(self) -> None: ...
def copy(self) -> Self: ...
def pop(self, key: _KT, default: _VT | _T = ...) -> _VT | _T: ...
def pop(self, key: object, default: _VT | _T = ...) -> _VT | _T: ...
def popitem(self) -> tuple[_KT, _VT]: ...

@overload
Expand Down
6 changes: 3 additions & 3 deletions stubs/yt-dlp/yt_dlp/utils/networking.pyi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class HTTPHeaderDict(dict[str, str]):
def get(self, key: str, /, default: type[NO_DEFAULT] | _T = ...) -> str | _T | type[NO_DEFAULT]: ...

@overload
def pop(self, key: str, /) -> str: ...
def pop(self, key: object, /) -> str: ...
@overload
def pop(self, key: str, /, default: _T) -> str | _T: ...
def pop(self, key: object, /, default: _T) -> str | _T: ...
@overload
def pop(self, key: str, /, default: type[NO_DEFAULT] | _T | str = ...) -> str | _T | type[NO_DEFAULT]: ...
def pop(self, key: object, /, default: type[NO_DEFAULT] | _T | str = ...) -> str | _T | type[NO_DEFAULT]: ...

@overload
def setdefault(self, key: str, /) -> str: ...
Expand Down

Back | FazBrowse Home | New Git URL