Skip to content

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

@carljm

Description

@carljm

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 #15271, the same category of issue as #6597.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions