Title: TypeForm: Recognize type variables in a consistent way · Issue #20124 · python/mypy · GitHub
Open Graph Title: TypeForm: Recognize type variables in a consistent way · Issue #20124 · python/mypy
X Title: TypeForm: Recognize type variables in a consistent way · Issue #20124 · python/mypy
Description: Prerequisite: This issue applies after PR #19596 is merged. Currently mypy sometimes allows a type variable to be assigned to a TypeForm variable and sometimes it does not. This is a problem because the behavior is inconsistent. Specific...
Open Graph Description: Prerequisite: This issue applies after PR #19596 is merged. Currently mypy sometimes allows a type variable to be assigned to a TypeForm variable and sometimes it does not. This is a problem becaus...
X Description: Prerequisite: This issue applies after PR #19596 is merged. Currently mypy sometimes allows a type variable to be assigned to a TypeForm variable and sometimes it does not. This is a problem becaus...
Opengraph URL: https://github.com/python/mypy/issues/20124
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"TypeForm: Recognize type variables in a consistent way","articleBody":"\u003e **Prerequisite:** This issue applies after PR https://github.com/python/mypy/pull/19596 is merged.\n\nCurrently mypy sometimes allows a type variable to be assigned to a TypeForm variable and sometimes it does not. This is a problem because the behavior is inconsistent.\n\nSpecifically, mypy allows a type variable to be assigned to a TypeForm variable iff it is in a quoted type expression:\n\n```python\n[case testTypeVarTypeFormsAreOnlyRecognizedInStringAnnotation]\n...\nE = TypeVar('E')\nclass Box(Generic[E]):\n def foo(self, e: E) -\u003e None:\n list_of_typx: List[TypeForm] = [E] # E: \"E\" is a type variable and only valid in type context\n typx1: TypeForm = E # E: \"E\" is a type variable and only valid in type context\n typx2: TypeForm = 'E'\n```\n\nWhen a type variable is NOT quoted, mypy will not allow it to be assigned:\n\n```python\n[case testEveryKindOfTypeExpressionIsAssignableToATypeFormVariable]\nSomeTypeVar = TypeVar('SomeTypeVar')\ntypx: TypeForm\n...\n# NOTE: Unbound TypeVar isn't currently accepted as a TypeForm. Is that OK?\ntypx = SomeTypeVar # E: Incompatible types in assignment (expression has type \"TypeVar\", variable has type \"TypeForm[Any]\")\n```\n\nI'd argue that TypeForm should always accept a type variable because it is a valid type expression and TypeForms are intended to accept all kinds of type expressions:\n\n```\ndef cast[T](type_expr: TypeForm[T], value_expr: object) -\u003e T: ...\n\ncast(SomeTypeVar, some_expression) # should not be an error\n```\n\nIn summary:\n* [ ] mypy should allow a type variable to be assigned to a TypeForm variable, regardless of whether it is quoted or not. Update above-referenced tests to reflect this new behavior.","author":{"url":"https://github.com/davidfstr","@type":"Person","name":"davidfstr"},"datePublished":"2025-10-26T18:02:17.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/20124/mypy/issues/20124"}
| 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:f027bbe7-20bb-b74c-cb22-ada791e51cf4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8C40:189645:1082333:15E2580:6A4C5845 |
| html-safe-nonce | c97de226163f5f64df45db7916bfbeba88653a1004b09df4b6a240d2a4152fba |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QzQwOjE4OTY0NToxMDgyMzMzOjE1RTI1ODA6NkE0QzU4NDUiLCJ2aXNpdG9yX2lkIjoiNjQ1Mzc0Njk3NjA1OTg0MDU4MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c9c9669ba8610131ce3167a9f383d899def99aaaa93e998fabd4c830b6dabbee |
| hovercard-subject-tag | issue:3554262295 |
| 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/20124/issue_layout |
| twitter:image | https://opengraph.githubassets.com/32a6ef7eef8b39cc8608fff71734e0e30893fa4fe902ef28eacb1ef4d025e505/python/mypy/issues/20124 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/32a6ef7eef8b39cc8608fff71734e0e30893fa4fe902ef28eacb1ef4d025e505/python/mypy/issues/20124 |
| og:image:alt | Prerequisite: This issue applies after PR #19596 is merged. Currently mypy sometimes allows a type variable to be assigned to a TypeForm variable and sometimes it does not. This is a problem becaus... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | davidfstr |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3d11bb817438277de2a940854450e83a7d32b6aeb5014e9e6b00a6423900251c |
| 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 | 14099438da5379150f15a2892474c7c7e6c0e55e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width