Title: min()/max() with default value cause issues with key function type · Issue #17536 · python/mypy · GitHub
Open Graph Title: min()/max() with default value cause issues with key function type · Issue #17536 · python/mypy
X Title: min()/max() with default value cause issues with key function type · Issue #17536 · python/mypy
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 pl...
Open Graph 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=lam...
X 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=...
Opengraph URL: https://github.com/python/mypy/issues/17536
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"min()/max() with default value cause issues with key function type","articleBody":"## Bug Report\r\n\r\nHi everyone, thanks for all the work on mypy, I love this tool! 👏 \r\n\r\nI run into an issue with this code:\r\n\r\n```python\r\ndef foo(values: list[dict]) -\u003e dict | None:\r\n return max(values, default=None, key=lambda entry: entry.get(\"value\", 0))\r\n```\r\nSee playground: https://mypy-play.net/?mypy=latest\u0026python=3.12\u0026gist=3daadb92c263a8447595aa7640a04599\r\n\r\n### Expected Behavior:\r\nmypy should not report any errors\r\n\r\n### Actual Behavior\r\nmypy reports an error:\r\n```\r\nItem \"None\" of \"dict[Any, Any] | None\" has no attribute \"get\"\r\n```\r\n\r\n\r\n### Environment\r\n- Mypy version used: `mypy 1.10.1 (compiled: yes)`\r\n- Mypy command-line flags: **none**\r\n- Mypy configuration options from `mypy.ini` (and other config files): **none**\r\n- Python version used: `Python 3.12.2`\r\n\r\n### Additional Details\r\n\r\nI found this issue from 2019, which seems to be the same issue: https://github.com/python/mypy/issues/6460\r\n\r\nHere people reported that it was an issue with typeshed, which was fixed in typeshed with this PR: https://github.com/python/typeshed/pull/2833\r\n\r\nI 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\r\n\r\nSo while it seems this issue was once fixed it the past, it looks like it might have reappeared through a different cause.\r\n","author":{"url":"https://github.com/pfaion","@type":"Person","name":"pfaion"},"datePublished":"2024-07-18T08:29:52.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/17536/mypy/issues/17536"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d93a0519-e546-f5e1-7f8a-0f1e6eab5345 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CD34:75C97:1C0E8E6:26DFC98:6A5F360A |
| html-safe-nonce | b07e70d394555e27edeb9fd59e61b3b720a5145e2482c43c456a266391ad433d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRDM0Ojc1Qzk3OjFDMEU4RTY6MjZERkM5ODo2QTVGMzYwQSIsInZpc2l0b3JfaWQiOiIzNjA3MTg5ODI3OTE2NjA5MDM0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 06eeba6350f9c3f8d03d0b9802cf3cb695c038b2ef386866222bb16dae25a046 |
| hovercard-subject-tag | issue:2415726847 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/17536/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fb598105a32f3d4d97bd8f8aacd3bd280e14b078bee17178f3575d1261f0a3a7/python/mypy/issues/17536 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fb598105a32f3d4d97bd8f8aacd3bd280e14b078bee17178f3575d1261f0a3a7/python/mypy/issues/17536 |
| og:image:alt | 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=lam... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pfaion |
| hostname | github.com |
| expected-hostname | github.com |
| None | c80a2cb0221395c7c3987d7891fcde45641e27ea0bd3917b247d8fe154cf710f |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 4cf24abec96dc3a7b39f364f737aca1e50991904 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width