Description
Bug Report
Hi everyone, thanks for all the work on mypy, I love this tool! 👏
I run into an issue with this code:
def foo(values: list[dict]) -> dict | None:
return max(values, default=None, key=lambda entry: entry.get("value", 0))
See playground: https://mypy-play.net/?mypy=latest&python=3.12&gist=3daadb92c263a8447595aa7640a04599
Expected Behavior:
mypy should not report any errors
Actual Behavior
mypy reports an error:
Item "None" of "dict[Any, Any] | None" has no attribute "get"
Environment
- Mypy version used:
mypy 1.10.1 (compiled: yes)
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini
(and other config files): none - Python version used:
Python 3.12.2
Additional Details
I found this issue from 2019, which seems to be the same issue: #6460
Here people reported that it was an issue with typeshed, which was fixed in typeshed with this PR: python/typeshed#2833
I checked current trunk for typeshed and cannot spot any issue with the stubs for max()
, the type correctly seems to indicate that the key function has nothing to do with the type of the default parameter: https://github.com/python/typeshed/blob/d482d4e83c9d247e79c87d954a9ccb11f85bfc37/stdlib/builtins.pyi#L1515-L1516
So while it seems this issue was once fixed it the past, it looks like it might have reappeared through a different cause.