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.
In the PR that added these annotations, @hauntsaninja said:
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_keysanddict_itemsfor 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:This is really a sub-issue of #15271, the same category of issue as #6597.