Open jwodder opened 1 year ago
Thanks for the great bug report! It looks like this still repros if you use --new-type-inference
, as well.
default
is a red herring. This has nothing to do with any fancy inference logic, it is plain old overusing of outer context. I bet if you will rewrite it as temp = max(...); return temp
it will work. A while ago I added a special-casing for this for assignments, see https://github.com/python/mypy/pull/14151, I guess we may want to have matching special-casing for return
.
@ilevkivskyi confirmed assigning to a local variable first makes this work.
Consider the following code:
I believe this should type-check without issue; in particular, by my reading of
max()
's type annotations in typeshed, thekey
callable should be expected to take only those types contained in the iterable. However, mypy disagrees:Note that mypy expects the
key
callable to acceptstr | None
even thoughstrs
is anIterable[str]
. Moreover, if thedefault
argument is omitted and the return type ofbest_str()
is changed tostr
, then mypy accepts the code.Your Environment
mypy.ini
(and other config files): None