python / python/typeshed

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

Open
#15,888 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

In the PR that added these annotations, @hauntsaninja said:

My instinct is to try it the stricter way and relax it if we ever get a real user complaint about it, but happy to go along with whatever others think is best

I'd like to provide such a complaint. In ty, we would like to infer precise key-type specializations (unions of string literals) of dict_keys and dict_items for closed TypedDicts, where all possible keys are known. We have real user requests for this, because it allows iterating over dict keys/items and passing the keys along to a function that expects a limited set of literals.

But doing this causes false positives with isdisjoint, because the following valid code will now error:

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

def check(td: TD) -> None:
    # Both operations are safe at runtime but will report invalid-argument-type:
    td.keys().isdisjoint(["other"])
    td.items().isdisjoint([("other", 1)])

This is really a sub-issue of https://github.com/python/typeshed/issues/15271, the same category of issue as #6597.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the annotations introduced in PR 12309 and the related discussion in issue #15271. Reproduce the two closed TypedDict examples from this issue, then adjust the is_disjoint argument typing so both valid calls are accepted without losing precise key and item types; verify the resulting type-checking behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.