Title: TypeVar incompatible with constrained union · Issue #9424 · python/mypy · GitHub
Open Graph Title: TypeVar incompatible with constrained union · Issue #9424 · python/mypy
X Title: TypeVar incompatible with constrained union · Issue #9424 · python/mypy
Description: from typing import Optional, TypeVar U = TypeVar('U', bound=Optional[int]) def f(u: U) -> U: if u is None: return None assert isinstance(u, int) # removing this line causes it to pass. return u Gives main.py:10: error: Incompatible retur...
Open Graph Description: from typing import Optional, TypeVar U = TypeVar('U', bound=Optional[int]) def f(u: U) -> U: if u is None: return None assert isinstance(u, int) # removing this line causes it to pass. return u Giv...
X Description: from typing import Optional, TypeVar U = TypeVar('U', bound=Optional[int]) def f(u: U) -> U: if u is None: return None assert isinstance(u, int) # removing this line causes it to pass. r...
Opengraph URL: https://github.com/python/mypy/issues/9424
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"TypeVar incompatible with constrained union","articleBody":"```python\r\nfrom typing import Optional, TypeVar\r\n\r\nU = TypeVar('U', bound=Optional[int])\r\n\r\n\r\ndef f(u: U) -> U:\r\n if u is None:\r\n return None\r\n assert isinstance(u, int) # removing this line causes it to pass.\r\n return u\r\n```\r\nGives\r\n```\r\nmain.py:10: error: Incompatible return value type (got \"int\", expected \"U\")\r\n```\r\n\r\nI cannot remove the assertion since it stands in for other code that is effectively constraining `u`.\r\n\r\nThis can be expressed tediously using `overload`:\r\n```python\r\nfrom typing import Optional, TypeVar, overload\r\n\r\n\r\n@overload\r\ndef f(u: None) -> None: ...\r\n\r\n@overload\r\ndef f(u: int) -> int: ...\r\n\r\ndef f(u: Optional[int]) -> Optional[int]:\r\n if u is None:\r\n return None\r\n assert isinstance(u, int)\r\n return u\r\n```\r\nWould it be possible to get the `TypeVar` approach to pass? It is far more succinct, and this quickly blows up in complexity if there are more argument-return type constraints to be enforced.\r\n\r\nThis pattern is analogous to the common class factory pattern:\r\n```python\r\nclass C: pass\r\nclass B(C): pass\r\nclass A(B): pass\r\n\r\nT = TypeVar('T', bound=C)\r\ndef factory(cls: Type[T]) -> T:\r\n return cls()\r\n```\r\nSince A \u003c B \u003c C, this enforces\r\n```\r\nfactory(Type[C]) -> C\r\nfactory(Type[B]) -> B\r\nfactory(Type[A]) -> A\r\n```\r\n\r\nThis issue argues that since int \u003c Optional[int] and None \u003c Optional[int], we should ideally be able to similarly specify that\r\n```\r\nf(int) -> int\r\nf(None) -> None\r\n```\r\nusing the same notation.","author":{"url":"https://github.com/NeilGirdhar","@type":"Person","name":"NeilGirdhar"},"datePublished":"2020-09-06T08:47:47Z","url":"https://github.com/python/mypy/issues/9424"}
| 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:29fcf109-bec2-92b0-c983-5bf9450c904a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D038:2463C6:4B4118:6504CB:6B002EC2 |
| html-safe-nonce | 95cba5f69c2fe6527b2d46ed7660cea5059fa14e19bc7e6c880f537230f3e1ac |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMDM4OjI0NjNDNjo0QjQxMTg6NjUwNENCOjZCMDAyRUMyIiwidmlzaXRvcl9pZCI6IjY1NDY2OTEwNDMzMTQ2NDI2MjYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 50030580157f78d04ec700f3dabce9c0acf6d91dccbfee9ac916d55bc3ef4e2b |
| hovercard-subject-tag | issue:694297154 |
| 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/9424/issue_layout |
| twitter:image | https://opengraph.githubassets.com/972af909140b5f7160fe4289db70aee9dd5c5bb57e791fd2d39fb1690eb31c62/python/mypy/issues/9424 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/972af909140b5f7160fe4289db70aee9dd5c5bb57e791fd2d39fb1690eb31c62/python/mypy/issues/9424 |
| og:image:alt | from typing import Optional, TypeVar U = TypeVar('U', bound=Optional[int]) def f(u: U) -> U: if u is None: return None assert isinstance(u, int) # removing this line causes it to pass. return u Giv... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | NeilGirdhar |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width