It seems like the annotations for these were designed to help detect user errors. For instance, we know that .get on a dict[int, int] will always return the default as the types are incompatible, and so .get was restricted in typeshed to only accept the key type. However, this hurts, even in simple cases like using literals:
fromtypingimportMapping, Literaldefdemo_get(
d: Mapping[Literal["foo", "bar"], int],
dynamic_key: str,
) ->None:
# No overload variant of "get" of "Mapping" matches argument type "str"d.get(dynamic_key)
defdemo_sub(
left: set[Literal["foo", "bar"]],
right: set[str],
) ->None:
# Unsupported operand types for - ("set[Literal['foo', 'bar']]" and "set[str]")left-right
I believe typeshed is trying to act as a linter here, which shouldn't be its job. Type-checkers and type-informed linters are better at this and can use more complex logic.
For example, in the set difference case set[A] - set[B], the proper test would be to check whether the types have a non-empty intersection or not. The same could be said for Mapping[K, V].get(K2).
I don't think this is quite completed yet, we have dealt with builtins.set and builtins.frozenset, but #15471 (dict) and #15472 (list) are still open, and after that I'd still want to consider some update for the corresponding collections.abc members
Activity
carljm commented on Jan 16, 2026
#6597 is also related.
I fully agree that all of these APIs should be relaxed to accept object instead.
Relax isdisjoint method to accept Iterable[object]
randolf-scholz commented on Mar 2, 2026
I don't think this is quite completed yet, we have dealt with builtins.set and builtins.frozenset, but #15471 (dict) and #15472 (list) are still open, and after that I'd still want to consider some update for the corresponding collections.abc members
srittau commented on Mar 2, 2026
Sorry, this was autoclosed when merging #15470.
carljm commented on Jun 10, 2026
#15888 observes the same problem for dict_keys.isdisjoint and dict_items.isdisjoint
[ty] Patch typeshed mapping key annotations during sync (#25831)