Is there a good reason why Mapping.__contains__
expects object
, but Mapping.get
expects a strict type?
#8219
Labels
stubs: false negative
Type checkers do not report an error, but should
I ran into a case where a user intended to check whether an enum value
x
was present in a dictionaryDict[int, int]
:x.value in d
.But instead they wrote
x in d
, which always fails, since theEnum
itself is never a key in the dictionary. The type checker didn't complain since the typeshed stub forMapping.__contains__
accepts any type compatible withobject
:However, if the user had used
d.get(x)
, the type checker would have complained, because the typeshed stub forMapping.get
is stricter:Simple repro:
It looks like the stub for
__contains__
has expectedobject
ever sincetyping.pyi
was added to typeshed (in 2015). If the idea is that users may want to check for existence of keys of arbitrary types, a similar argument would hold ford.get(x)
.Is there a good reason why the stub complains about
d.get(x)
but notx in d
? Otherwise, it'd be good to make__contains__
stricter.The text was updated successfully, but these errors were encountered: