Title: Error when preserving generic type behaviour in subclasses (when not supported at runtime) · Issue #8523 · python/mypy · GitHub
Open Graph Title: Error when preserving generic type behaviour in subclasses (when not supported at runtime) · Issue #8523 · python/mypy
X Title: Error when preserving generic type behaviour in subclasses (when not supported at runtime) · Issue #8523 · python/mypy
Description: This is a bug report. mypy = 0.770 python = 3.8.2 I did not try running mypy from git master The docs for "Using classes that are generic in stubs but not at runtime" (here) clearly indicate how to subclass classes that are defined to be...
Open Graph Description: This is a bug report. mypy = 0.770 python = 3.8.2 I did not try running mypy from git master The docs for "Using classes that are generic in stubs but not at runtime" (here) clearly indicate how to...
X Description: This is a bug report. mypy = 0.770 python = 3.8.2 I did not try running mypy from git master The docs for "Using classes that are generic in stubs but not at runtime" (here) clearly indic...
Opengraph URL: https://github.com/python/mypy/issues/8523
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Error when preserving generic type behaviour in subclasses (when not supported at runtime)","articleBody":"This is a bug report.\r\n* mypy = 0.770\r\n* python = 3.8.2\r\n* I did not try running mypy from git master\r\n----\r\nThe docs for \"Using classes that are generic in stubs but not at runtime\" ([here](https://mypy.readthedocs.io/en/stable/common_issues.html#using-classes-that-are-generic-in-stubs-but-not-at-runtime)) clearly indicate how to subclass classes that are defined to be generic in their stubs but don't actually support generics in their runtime code.\r\n\r\nThe shown method (conditionally defining a typed subclass) works great when you want to lock the type down in your subclass. However, **if you want to maintain the subclass as a generic it does not work** (mypy reports an error).\r\n\r\nHere is failing case (with `mypy --strict`), where the only variation from the canonical [example](https://mypy.readthedocs.io/en/stable/common_issues.html#using-classes-that-are-generic-in-stubs-but-not-at-runtime) is using a generic `TypeVar` instead of a `str` as the type for the Queue:\r\n\r\n```python\r\nfrom queue import Queue\r\nfrom typing import TYPE_CHECKING, TypeVar\r\n\r\n_T = TypeVar(\"_T\")\r\nif TYPE_CHECKING:\r\n MyQueueBase = Queue[_T] # change _T to str here and it works perfectly (per the docs)\r\nelse:\r\n MyQueueBase = Queue\r\nclass MyQueue(MyQueueBase): pass # mypy error is on this line (9)\r\n```\r\n\r\nmypy reports an error as follows (when the base is `Queue[_T]`):\r\n\r\n```\r\n$ python --version\r\nPython 3.8.2\r\n$ mypy --version\r\nmypy 0.770\r\n$ mypy --strict ./generic_subclass_example.py \r\ngeneric_subclass_example.py:9: error: Missing type parameters for generic type \"MyQueueBase\"\r\nFound 1 error in 1 file (checked 1 source file)\r\n```\r\n\r\nNote that without `--strict` there is no error (not sure which of the flags causes it).\r\n","author":{"url":"https://github.com/rwarren","@type":"Person","name":"rwarren"},"datePublished":"2020-03-11T15:07:54Z","url":"https://github.com/python/mypy/issues/8523"}
| 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:bd8f5348-edf1-d625-0b4c-035091ced05c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DF42:1A77D:ACCCEE:E5BF5A:6B004875 |
| html-safe-nonce | 0053c53288fc7c933c462fdd332b0a0e095f000403e097a798a3731c5a6b5086 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjQyOjFBNzdEOkFDQ0NFRTpFNUJGNUE6NkIwMDQ4NzUiLCJ2aXNpdG9yX2lkIjoiMzUxMDQ1MjY4NjA4NjQ5MDIyOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 7178afe29019eac67dea6781c2603e0790b90a550eb1cae9627a470c734270e5 |
| hovercard-subject-tag | issue:579334595 |
| 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/8523/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7d93e14bd4adfa7d5e2cbd2413c26903bf091823128694ed1dd70d2b360df372/python/mypy/issues/8523 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7d93e14bd4adfa7d5e2cbd2413c26903bf091823128694ed1dd70d2b360df372/python/mypy/issues/8523 |
| og:image:alt | This is a bug report. mypy = 0.770 python = 3.8.2 I did not try running mypy from git master The docs for "Using classes that are generic in stubs but not at runtime" (here) clearly indicate how to... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rwarren |
| 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