Title: Should have a way to annotate an Optional carryover method. · Issue #3295 · python/mypy · GitHub
Open Graph Title: Should have a way to annotate an Optional carryover method. · Issue #3295 · python/mypy
X Title: Should have a way to annotate an Optional carryover method. · Issue #3295 · python/mypy
Description: Edited with a better example. Let's suppose you have a function that can take an Optional and returns something based on that or None if it's None. from typing import overload, Optional def bar(foo: Optional[int]) -> Optional[str]: if fo...
Open Graph Description: Edited with a better example. Let's suppose you have a function that can take an Optional and returns something based on that or None if it's None. from typing import overload, Optional def bar(foo...
X Description: Edited with a better example. Let's suppose you have a function that can take an Optional and returns something based on that or None if it's None. from typing import overload, Optional def...
Opengraph URL: https://github.com/python/mypy/issues/3295
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Should have a way to annotate an Optional carryover method.","articleBody":"**Edited with a better example.**\r\n\r\nLet's suppose you have a function that can take an Optional and returns something based on that or None if it's None.\r\n\r\n```\r\nfrom typing import overload, Optional\r\n\r\ndef bar(foo: Optional[int]) -> Optional[str]:\r\n if foo is None:\r\n return None\r\n else:\r\n return str(foo)\r\n\r\nx = None # type: Optional[int]\r\n\r\nreveal_type(bar(None)) # Line 18\r\nreveal_type(bar(x))\r\nreveal_type(bar('foo'))\r\n```\r\n\r\nThere's one problem with this being annotated this way. If you pass a Non-Optional you get returned an Optional[str] even though it's not possible. So I thought the new `@overload` stuff might help here but it doesn't:\r\n\r\n```\r\nfrom typing import overload, Optional\r\n\r\n@overload\r\ndef bar(foo: int) -> str:\r\n ...\r\n\r\n@overload\r\ndef bar(foo: Optional[int]) -> Optional[str]:\r\n ...\r\n\r\ndef bar(foo: Optional[int]) -> Optional[str]:\r\n if foo is None:\r\n return None\r\n else:\r\n return str(foo)\r\n\r\nx = None # type: Optional[int]\r\n\r\nreveal_type(bar(None))\r\nreveal_type(bar(x))\r\nreveal_type(bar('foo'))\r\n```\r\n\r\nSadly that gives me an error and the wrong types:\r\n```\r\ntt.py:4: error: Overloaded function signatures 1 and 2 overlap with incompatible return types\r\ntt.py:19: error: Revealed type is 'Union[builtins.int, builtins.None]'\r\ntt.py:20: error: Revealed type is 'Any'\r\ntt.py:21: error: Revealed type is 'Any'\r\n```\r\n\r\nI also tried None as the argument to the 2nd overload. That got me closer:\r\n\r\n\r\n```\r\ntt.py:19: error: Revealed type is 'Union[builtins.int, builtins.None]'\r\ntt.py:20: error: Revealed type is 'Any'\r\ntt.py:21: error: Revealed type is 'builtins.str'\r\n```\r\n\r\nI would love to be able to specify this as it would make a lot of our code easier to type. Maybe there's a way to do it with a TypeVar but I couldn't figure it out.\r\n\r\n","author":{"url":"https://github.com/euresti","@type":"Person","name":"euresti"},"datePublished":"2017-05-01T19:17:28Z","url":"https://github.com/python/mypy/issues/3295"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:b31abd3a-cc37-5a65-c166-b6eaf030a5ef |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 987C:F5AA9:1B981C5:254EEE7:6B0003E4 |
| html-safe-nonce | 63e656b436e025b50989d45db8ca0cc880f6adffc09b70397c84f06893a39354 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5ODdDOkY1QUE5OjFCOTgxQzU6MjU0RUVFNzo2QjAwMDNFNCIsInZpc2l0b3JfaWQiOiI4MTU4MzI2Njk0NDk1NjUwNzg4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | ea5cb58d72fc2bac25f86d7a14770fca7b70b9bccc5eac615501d73c1079cf04 |
| hovercard-subject-tag | issue:225502822 |
| 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/3295/issue_layout |
| twitter:image | https://opengraph.githubassets.com/40fab1d81527e6cfd9f2c3bf8cccd57313cee979820a1139c89c2c5e435dabe9/python/mypy/issues/3295 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/40fab1d81527e6cfd9f2c3bf8cccd57313cee979820a1139c89c2c5e435dabe9/python/mypy/issues/3295 |
| og:image:alt | Edited with a better example. Let's suppose you have a function that can take an Optional and returns something based on that or None if it's None. from typing import overload, Optional def bar(foo... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | euresti |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width