Open sobolevn opened 2 years ago
We should probably change _KT
to _KT | None
in both overloads. Technically the first argument can be anything that overlaps with _KT
, but in practice, allowing None
is probably good enough.
The stricter typing for get()
will catch potential type problems. While your example will work at runtime, it indicates a likely bug as None
is not a valid key. I like the new behavior, although I could be convinced otherwise. Maybe we could also just special case None
.
I hit this while working on my bidict library, which implements bidirectional mapping data structures which wrap two (regular one-directional) mappings, and which is type hinted and checked with mypy.
it indicates a likely bug
I disagree that this indicates a "likely" bug. It could be a bug. But it's just as likely that there is no bug. And the current type annotations are flagging perfectly correct and idiomatic code:
Sometimes you have no idea what type x
is. One of the main reasons that d.get(x[, default])
and d.pop(x, default)
exist -- with the optional default
argument -- is to support coding in a "check the result after calling" style, rather than the "look before you leap" style, which is often less Pythonic.
Yet these APIs' type hints are treating these APIs the same as Mapping.__getitem__
and MutableMapping.__delitem__
respectively, even though they are not meant to be used the same way.
Would you accept a PR that changes the Mapping.get(x[, default])
and MutableMapping.pop(x, default)
type hints to support the "check result after calling" style they're intended to support? Note this is more than just special-casing None
.
Thank you for your consideration.
I don't think that this is the way forward. Type checkers are supposed to check that the types are correct after all. If you don't know types or are not interested in the extra type checking the annotations provide, you can always use the Any
escape hatch.
We could probably make it work well enough in practice by special-casing None
s.
I got a new regression from the latest release on a real project: https://github.com/wemake-services/wemake-python-styleguide/blob/master/wemake_python_styleguide/logic/arguments/function_args.py#L125-L126
Error:
Simplier repro:
So, what do you think: is this a valid error? Because it will work at runtime with no problem. And since it has
Any
part in it, sometimes it can even bestr
. So, in my app it was working as expected in all cases: ifx
isstr
and exists inm
- then fine. If not - then just returnNone
.I am openning it here, because it looks like a typeshed issue, rather than a mypy issue.