Title: Unneeded type widening of inner function call when using explicit return type · Issue #12092 · python/mypy · GitHub
Open Graph Title: Unneeded type widening of inner function call when using explicit return type · Issue #12092 · python/mypy
X Title: Unneeded type widening of inner function call when using explicit return type · Issue #12092 · python/mypy
Description: Using an explicit return type unnecessarily widens the types in filter(): def return_a(strings: list[str]) -> Optional[str]: # Item "None" of "Optional[str]" has no attribute "strip" return next(filter(lambda s: s.strip() == "a", strings...
Open Graph Description: Using an explicit return type unnecessarily widens the types in filter(): def return_a(strings: list[str]) -> Optional[str]: # Item "None" of "Optional[str]" has no attribute "strip" return next(fi...
X Description: Using an explicit return type unnecessarily widens the types in filter(): def return_a(strings: list[str]) -> Optional[str]: # Item "None" of "Optional[str]" has no attribute...
Opengraph URL: https://github.com/python/mypy/issues/12092
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unneeded type widening of inner function call when using explicit return type","articleBody":"Using an explicit return type unnecessarily widens the types in `filter()`:\r\n\r\n```python\r\ndef return_a(strings: list[str]) -\u003e Optional[str]:\r\n # Item \"None\" of \"Optional[str]\" has no attribute \"strip\"\r\n return next(filter(lambda s: s.strip() == \"a\", strings), None)\r\n```\r\nand a comparable error when the filter function is explicitly typed:\r\n```python\r\ndef matches_a(s: str) -\u003e bool:\r\n return s.strip() == \"a\"\r\n\r\ndef return_a(strings: list[str]) -\u003e Optional[str]:\r\n # Argument 1 to \"filter\" has incompatible type \"Callable[[str], Any]\"; expected \"Callable[[Optional[str]], Any]\"\r\n return next(filter(matches_a, strings), None)\r\n```\r\n\r\nNo error occurs when the explicit return type is removed:\r\n```python\r\n# return_a is inferred to return str | None\r\ndef return_a(strings: list[str]):\r\n # No error\r\n return next(filter(lambda s: s.strip() == \"a\", strings), None)\r\n```\r\n\r\nMypy 0.931, Python 3.8.6.","author":{"url":"https://github.com/knbk","@type":"Person","name":"knbk"},"datePublished":"2022-01-28T10:53:21.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/12092/mypy/issues/12092"}
| 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:65478bfc-4f27-5c49-0aed-7b1e35dafc67 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C30A:210405:2A4C205:36D099A:6A5CB57C |
| html-safe-nonce | 73a7cbef919bee1b2935021923f94b3b280e1e4568355f4f4cf840e01d22abc4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMzBBOjIxMDQwNToyQTRDMjA1OjM2RDA5OUE6NkE1Q0I1N0MiLCJ2aXNpdG9yX2lkIjoiNTE2ODU0NDg4OTY2ODY3OTAzNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9d47ba6e22f47aa075c7e0e3ca81b5da7ff0d59f4c2aa448fb2faba936553415 |
| hovercard-subject-tag | issue:1117264406 |
| 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/12092/issue_layout |
| twitter:image | https://opengraph.githubassets.com/98a695643c9ba290050fe84d0a621146ce9f2adc76ff5832d4e29c75ba927876/python/mypy/issues/12092 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/98a695643c9ba290050fe84d0a621146ce9f2adc76ff5832d4e29c75ba927876/python/mypy/issues/12092 |
| og:image:alt | Using an explicit return type unnecessarily widens the types in filter(): def return_a(strings: list[str]) -> Optional[str]: # Item "None" of "Optional[str]" has no attribute "strip" return next(fi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | knbk |
| 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