Skip to content

Widen dict_keys/dict_items isdisjoint parameter type#15976

Open
muhamedfazalps wants to merge 1 commit into
python:mainfrom
muhamedfazalps:fix/isdisjoint-type-width
Open

Widen dict_keys/dict_items isdisjoint parameter type#15976
muhamedfazalps wants to merge 1 commit into
python:mainfrom
muhamedfazalps:fix/isdisjoint-type-width

Conversation

@muhamedfazalps

Copy link
Copy Markdown

Summary

Widen the parameter type of dict_keys.isdisjoint and dict_items.isdisjoint from Iterable[_KT_co]/Iterable[tuple[_KT_co, _VT_co]] to Iterable[object].

Fixes #15888

Motivation

The current strict type causes false positives when using TypedDict with closed=True where the key type is a union of string literals. When iterating over dict keys and passing them to isdisjoint, the strict type rejects valid operations:

class TD(TypedDict, closed=True):
    x: int

def check(td: TD) -> None:
    td.keys().isdisjoint(["other"])  # false positive: invalid-argument-type
    td.items().isdisjoint([("other", 1)])  # false positive: invalid-argument-type

Changes

  • stdlib/_collections_abc.pyi: Changed dict_keys.isdisjoint parameter from Iterable[_KT_co] to Iterable[object]
  • stdlib/_collections_abc.pyi: Changed dict_items.isdisjoint parameter from Iterable[tuple[_KT_co, _VT_co]] to Iterable[object]

This matches the pattern already used by set.isdisjoint and frozenset.isdisjoint, which accept Iterable[object].


Discovered during open source research. https://buymeacoffee.com/muhamedfazalps

Change dict_keys.isdisjoint and dict_items.isdisjoint parameter types
from Iterable[_KT_co]/Iterable[tuple[_KT_co, _VT_co]] to
Iterable[object].

This matches the pattern used by set.isdisjoint and frozenset.isdisjoint,
and fixes false positives when using TypedDict with closed=True where
the key type is a union of string literals.

Fixes python#15888
@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

type annotation for {dict_keys,dict_items}.is_disjoint argument is too narrow

1 participant