Title: Mypy does not check bounds of variable type aliases · Issue #13086 · python/mypy · GitHub
Open Graph Title: Mypy does not check bounds of variable type aliases · Issue #13086 · python/mypy
X Title: Mypy does not check bounds of variable type aliases · Issue #13086 · python/mypy
Description: Bug Report When having defined a TypeAlias dependent on a TypeVar, mypy does not allays that instances of these are actually allowed. That is, that they respect the bounds or restrictions of the used TypeVar. This is in contrast to the b...
Open Graph Description: Bug Report When having defined a TypeAlias dependent on a TypeVar, mypy does not allays that instances of these are actually allowed. That is, that they respect the bounds or restrictions of the us...
X Description: Bug Report When having defined a TypeAlias dependent on a TypeVar, mypy does not allays that instances of these are actually allowed. That is, that they respect the bounds or restrictions of the us...
Opengraph URL: https://github.com/python/mypy/issues/13086
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Mypy does not check bounds of variable type aliases","articleBody":"**Bug Report**\r\n\r\nWhen having defined a `TypeAlias` dependent on a `TypeVar`, `mypy` does not allays that instances of these are actually allowed. That is, that they respect the bounds or restrictions of the used `TypeVar`. This is in contrast to the behavior for generic types, which are checked.\r\n\r\n**To Reproduce**\r\n\r\n```py\r\nfrom typing import Generic, TypeVar, TypeAlias\r\n\r\nT = TypeVar(\"T\", bound=bytes | str)\r\nU = TypeVar(\"U\", bytes, str)\r\n\r\nBoundedTuple: TypeAlias = tuple[T]\r\nRestrictedTuple: TypeAlias = tuple[U]\r\n\r\n\r\ni_am_illegal1: BoundedTuple[int] # Passes, even though this type shouldn't be possible!\r\ni_am_illegal2: RestrictedTuple[int] # Passes, even though this type shouldn't be possible!\r\n\r\n\r\nclass Bounded(Generic[T]):\r\n pass\r\n \r\nclass Restricted(Generic[U]):\r\n pass\r\n\r\n\r\ni_am_illegal3: Bounded[int] # Behaves as expected, resulting in `error: Type argument \"int\" of \"Bounded\" must be a subtype of \"Union[bytes, str]\"`\r\ni_am_illegal4: Restricted[int] # Behaves as expected, resulting in `error: Value of type variable \"U\" of \"Restricted\" cannot be \"int\"`\r\n\r\n```\r\n\r\nhttps://mypy-play.net/?mypy=latest&python=3.10&flags=strict&gist=f5763650eba54f25b51553dbdd58179f\r\n\r\n**Expected Behavior**\r\n\r\nI expect four errors instead of two from the example code above.\r\n\r\n**Actual Behavior**\r\n\r\nAs can be seen, I was allowed to define types that shouldn't be allowed, given the restrictions of the type variables in use.\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: 0.961\r\n- Mypy command-line flags: --strict\r\n- Python version used: 3.10\r\n","author":{"url":"https://github.com/harahu","@type":"Person","name":"harahu"},"datePublished":"2022-07-07T14:42:20Z","url":"https://github.com/python/mypy/issues/13086"}
| 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:d0c6bfe0-f9f2-97f8-0ca9-901f5108812e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A062:108997:3B0D38:50E338:6B006DD2 |
| html-safe-nonce | 09480e99e5ad9cc530544d2599823449d3b799a6cffe03de01a7a0f57e8be112 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMDYyOjEwODk5NzozQjBEMzg6NTBFMzM4OjZCMDA2REQyIiwidmlzaXRvcl9pZCI6IjM2MDIyMDUxMzYxMjgwNzcyNjYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8ef140528dd2034a41eb1b456444c929171cde4957a92e22d5057771e83b240d |
| hovercard-subject-tag | issue:1297567921 |
| 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/13086/issue_layout |
| twitter:image | https://opengraph.githubassets.com/dc3c12a99f4bc21124f75fd9c22067075fe2ed7451c9bc3919aa5837c4403df2/python/mypy/issues/13086 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/dc3c12a99f4bc21124f75fd9c22067075fe2ed7451c9bc3919aa5837c4403df2/python/mypy/issues/13086 |
| og:image:alt | Bug Report When having defined a TypeAlias dependent on a TypeVar, mypy does not allays that instances of these are actually allowed. That is, that they respect the bounds or restrictions of the us... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | harahu |
| hostname | github.com |
| expected-hostname | github.com |
| None | 6527e91c60c0b29d16606ff32ac5df8ecdc37a2a62205ea057a14d7762c870fa |
| 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 | 46f1cd2ae3cd8ae1a81019b5d619742e76f13bbd |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width