Title: Type inference for `max()` "tainted" by `default` · Issue #16267 · python/mypy · GitHub
Open Graph Title: Type inference for `max()` "tainted" by `default` · Issue #16267 · python/mypy
X Title: Type inference for `max()` "tainted" by `default` · Issue #16267 · python/mypy
Description: Consider the following code: from __future__ import annotations from collections.abc import Iterable from packaging.version import Version def best_str(strs: Iterable[str]) -> str | None: return max(strs, key=keyfunc, default=None) def k...
Open Graph Description: Consider the following code: from __future__ import annotations from collections.abc import Iterable from packaging.version import Version def best_str(strs: Iterable[str]) -> str | None: return ma...
X Description: Consider the following code: from __future__ import annotations from collections.abc import Iterable from packaging.version import Version def best_str(strs: Iterable[str]) -> str | None: return...
Opengraph URL: https://github.com/python/mypy/issues/16267
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type inference for `max()` \"tainted\" by `default`","articleBody":"Consider the following code:\r\n\r\n```python\r\nfrom __future__ import annotations\r\nfrom collections.abc import Iterable\r\nfrom packaging.version import Version\r\n\r\ndef best_str(strs: Iterable[str]) -\u003e str | None:\r\n return max(strs, key=keyfunc, default=None)\r\n\r\n\r\ndef keyfunc(s: str) -\u003e str:\r\n return s[::-1]\r\n```\r\n\r\nI believe this should type-check without issue; in particular, by my reading of [`max()`'s type annotations in typeshed](https://github.com/python/typeshed/blob/4a854366e03dee700109f8e758a08b2457ea2f51/stdlib/builtins.pyi#L1543), the `key` callable should be expected to take only those types contained in the iterable. However, mypy disagrees:\r\n\r\n```text\r\nmax-arg2.py:6: error: Argument \"key\" to \"max\" has incompatible type \"Callable[[str], str]\"; expected \"Callable[[str | None], SupportsDunderLT[Any] | SupportsDunderGT[Any]]\" [arg-type]\r\n```\r\n\r\nNote that mypy expects the `key` callable to accept `str | None` even though `strs` is an `Iterable[str]`. Moreover, if the `default` argument is omitted and the return type of `best_str()` is changed to `str`, then mypy accepts the code.\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: 1.6.0\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: 3.11.6","author":{"url":"https://github.com/jwodder","@type":"Person","name":"jwodder"},"datePublished":"2023-10-15T01:50:45.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/16267/mypy/issues/16267"}
| 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:134c3054-f452-5932-a367-f6cb0589473a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C64C:1846A4:54548B6:70067C9:6A5DC04D |
| html-safe-nonce | 04b0abdd444d415f40f636492dc10fa5a286d3875dfaa6255e7099a6ec9057a2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNjRDOjE4NDZBNDo1NDU0OEI2OjcwMDY3Qzk6NkE1REMwNEQiLCJ2aXNpdG9yX2lkIjoiNDY0OTgxNzA2MTk4ODM1MjA3NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 70e1e8232b97ac5c56f16df84c3adee8754de6fe30efada1fcf7ee17cdd87e09 |
| hovercard-subject-tag | issue:1943636996 |
| 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/16267/issue_layout |
| twitter:image | https://opengraph.githubassets.com/97ae3d6da6b23ec68924c9445230f71c9a2bf206a8ea57d6acaf141431f7265a/python/mypy/issues/16267 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/97ae3d6da6b23ec68924c9445230f71c9a2bf206a8ea57d6acaf141431f7265a/python/mypy/issues/16267 |
| og:image:alt | Consider the following code: from __future__ import annotations from collections.abc import Iterable from packaging.version import Version def best_str(strs: Iterable[str]) -> str | None: return ma... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jwodder |
| 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