Title: wrongly reported typing error in `min` with `key` lambda function and default value when result is Optional · Issue #17221 · python/mypy · GitHub
Open Graph Title: wrongly reported typing error in `min` with `key` lambda function and default value when result is Optional · Issue #17221 · python/mypy
X Title: wrongly reported typing error in `min` with `key` lambda function and default value when result is Optional · Issue #17221 · python/mypy
Description: Bug Report When using key function in min builtin function, default None value and returning it from a function which has an Optional type, mypy reports a problem where I believe there is none. To Reproduce from dataclasses import datacl...
Open Graph Description: Bug Report When using key function in min builtin function, default None value and returning it from a function which has an Optional type, mypy reports a problem where I believe there is none. To ...
X Description: Bug Report When using key function in min builtin function, default None value and returning it from a function which has an Optional type, mypy reports a problem where I believe there is none. To ...
Opengraph URL: https://github.com/python/mypy/issues/17221
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"wrongly reported typing error in `min` with `key` lambda function and default value when result is Optional","articleBody":"**Bug Report**\r\n\r\nWhen using `key` function in `min` builtin function, default None value and returning it from a function which has an `Optional` type, mypy reports a problem where I believe there is none.\r\n\r\n**To Reproduce**\r\n\r\n```python\r\nfrom dataclasses import dataclass\r\nfrom typing import List\r\n\r\n@dataclass\r\nclass X:\r\n x: int\r\n\r\ndef get_min(vals: List[X]) -\u003e X | None:\r\n return min(vals, key = lambda tsync: tsync.x, default=None)\r\n\r\nresult = get_min([X(1), X(2), X(3)])\r\n```\r\n\r\n**Expected Behavior**\r\n\r\nNo errors reported.\r\n\r\n**Actual Behavior**\r\n\r\n```\r\n$ mypy b.py --pretty\r\nb.py:11: error: Item \"None\" of \"X | None\" has no attribute \"x\" [union-attr]\r\n return min(vals, key = lambda tsync: tsync.x, default=None)\r\n ^~~~~~~\r\nFound 1 error in 1 file (checked 1 source file)\r\n```\r\n\r\nThe error goes away if I remove the `default=None` or when the `min` is not in a function with `X | None` return type:\r\n```python\r\nfrom dataclasses import dataclass\r\nfrom typing import List\r\n\r\n@dataclass\r\nclass X:\r\n x: int\r\n\r\ndef get_min_1(vals: List[X]) -\u003e X | None:\r\n return min(vals, key = lambda tsync: tsync.x, default=None)\r\n\r\ndef get_min_2(vals: List[X]) -\u003e X | None:\r\n return min(vals, key = lambda tsync: tsync.x) # \u003c- this is fine\r\n\r\nvals = [X(1), X(2), X(3)]\r\n\r\nresult1 = get_min_1(vals)\r\nresult2 = get_min_2(vals)\r\nresult3 = min(vals, key = lambda tsync: tsync.x, default=None) # \u003c- this is fine\r\n```\r\n\r\nHere, mypy complains only about `get_min_1`:\r\n```\r\n$ mypy a.py --pretty\r\na.py:11: error: Item \"None\" of \"X | None\" has no attribute \"x\" [union-attr]\r\n return min(vals, key = lambda tsync: tsync.x, default=None)\r\n ^~~~~~~\r\nFound 1 error in 1 file (checked 1 source file)\r\n```\r\n\r\n**Your Environment**\r\n\r\n\u003c!-- Include as many relevant details about the environment you experienced the bug in --\u003e\r\n\r\n- Mypy version used: 1.8.0\r\n- Mypy command-line flags: none required for reproducting the bug\r\n- Mypy configuration options from `mypy.ini` (and other config files): (no config)\r\n- Python version used: 3.11\r\n","author":{"url":"https://github.com/jan-spurny","@type":"Person","name":"jan-spurny"},"datePublished":"2024-05-07T09:14:18.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/17221/mypy/issues/17221"}
| 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:6c26bef3-4892-aee7-64e8-7af6aa675349 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A884:3081D4:2D8A067:3BC5A6B:6A5CECC9 |
| html-safe-nonce | b6364ad4e237c179e340d860215ddee6adc6b849d0a9abfc86d3f1a3a5a1e0ee |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBODg0OjMwODFENDoyRDhBMDY3OjNCQzVBNkI6NkE1Q0VDQzkiLCJ2aXNpdG9yX2lkIjoiNzU2MjIyNTE4NTU4NTk0OTg5NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | aa313334e67e66db7569d8235d6e1c4a10eaf34e6e6a11cec9edff7e265ca206 |
| hovercard-subject-tag | issue:2282713877 |
| 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/17221/issue_layout |
| twitter:image | https://opengraph.githubassets.com/374419164c8b2feccd9d8f86734f6ac8314dc3aad013902468b7d37a7fde7715/python/mypy/issues/17221 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/374419164c8b2feccd9d8f86734f6ac8314dc3aad013902468b7d37a7fde7715/python/mypy/issues/17221 |
| og:image:alt | Bug Report When using key function in min builtin function, default None value and returning it from a function which has an Optional type, mypy reports a problem where I believe there is none. To ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jan-spurny |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| 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 | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width