Title: Type substitutions in Callable ignore TypeVar bounds · Issue #8922 · python/mypy · GitHub
Open Graph Title: Type substitutions in Callable ignore TypeVar bounds · Issue #8922 · python/mypy
X Title: Type substitutions in Callable ignore TypeVar bounds · Issue #8922 · python/mypy
Description: When I run mypy on the following code: from typing import Callable, TypeVar class Base: pass class Sub(Base): pass class Other: pass BT = TypeVar('BT', bound=Base) def check(obj: BT) -> None: pass check(Base()) check(Sub()) check(Other()...
Open Graph Description: When I run mypy on the following code: from typing import Callable, TypeVar class Base: pass class Sub(Base): pass class Other: pass BT = TypeVar('BT', bound=Base) def check(obj: BT) -> None: pass ...
X Description: When I run mypy on the following code: from typing import Callable, TypeVar class Base: pass class Sub(Base): pass class Other: pass BT = TypeVar('BT', bound=Base) def check(obj: BT) -> ...
Opengraph URL: https://github.com/python/mypy/issues/8922
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type substitutions in Callable ignore TypeVar bounds","articleBody":"When I run mypy on the following code:\r\n```py\r\nfrom typing import Callable, TypeVar\r\n\r\nclass Base:\r\n pass\r\n\r\nclass Sub(Base):\r\n pass\r\n\r\nclass Other:\r\n pass\r\n\r\nBT = TypeVar('BT', bound=Base)\r\n\r\ndef check(obj: BT) -> None:\r\n pass\r\n\r\ncheck(Base())\r\ncheck(Sub())\r\ncheck(Other())\r\n\r\nFactory = Callable[[], BT]\r\n\r\ndef f() -> Other:\r\n pass\r\n\r\nfAny: Factory = f\r\nfBase: Factory[Base] = f\r\nfSub: Factory[Sub] = f\r\nfOther: Factory[Other] = f\r\n```\r\n\r\nIt outputs:\r\n\r\n```\r\n19: error: Value of type variable \"BT\" of \"check\" cannot be \"Other\" [type-var]\r\n check(Other())\r\n ^\r\n27: error: Incompatible types in assignment (expression has type \"Callable[[], Other]\",\r\nvariable has type \"Callable[[], Base]\") [assignment]\r\n fBase: Factory[Base] = f\r\n ^\r\n28: error: Incompatible types in assignment (expression has type \"Callable[[], Other]\",\r\nvariable has type \"Callable[[], Sub]\") [assignment]\r\n fSub: Factory[Sub] = f\r\n ^\r\n```\r\n\r\nAll of the things mypy warns about are valid. But there are also statements it doesn't warn about that are incorrect code.\r\n\r\nThe most clear one is the type annotation of `fOther` (last line of the test case): while the function `f` does indeed return an object of type `Other`, that type gets substituted for `BT` in the definition of `Factory` and `BT` has `Base` as its bound, which is a type unrelated to `Other`.\r\n\r\nA similar but more complex case can be made for `fAny`, which doesn't provide a type argument. However, there cannot exist a type argument that would satisfy both the bound of `BT` and the signature of `f`.\r\n\r\nI have limited knowledge of the internals of mypy, but it seems feasible that the `fOther` case could be reported. If the `fAny` case can be reported as well that would be great, but I can imagine that determining that no valid type argument can exist is a lot harder than validating a given type argument.\r\n\r\nI'm using mypy 0.770 on Python 3.8.2.\r\n","author":{"url":"https://github.com/mthuurne","@type":"Person","name":"mthuurne"},"datePublished":"2020-05-29T18:32:50Z","url":"https://github.com/python/mypy/issues/8922"}
| 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:ae2702e6-8396-4580-e150-dfe7f3d11104 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E7E6:278022:DE7BAB:1228580:6AFFFA0C |
| html-safe-nonce | 98d34b3614d96545c4953dab8c21515679d87de4ceda6bd2d5597dd1eb9a9fa4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFN0U2OjI3ODAyMjpERTdCQUI6MTIyODU4MDo2QUZGRkEwQyIsInZpc2l0b3JfaWQiOiI1NTM1OTAyMDQ5NjIwNzIwMTQwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 7149006474f66fa643d8da207c9b7d27dd23ce811613cf2ea2aaeb753c8aecea |
| hovercard-subject-tag | issue:627460263 |
| 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/8922/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ac110f5d70f490c9a8f08d74aa8cde3687b19349c0719fd48cc42cdb0b0b95b0/python/mypy/issues/8922 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ac110f5d70f490c9a8f08d74aa8cde3687b19349c0719fd48cc42cdb0b0b95b0/python/mypy/issues/8922 |
| og:image:alt | When I run mypy on the following code: from typing import Callable, TypeVar class Base: pass class Sub(Base): pass class Other: pass BT = TypeVar('BT', bound=Base) def check(obj: BT) -> None: pass ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mthuurne |
| 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