Title: Error inferring typevar on generic classes · Issue #18706 · python/mypy · GitHub
Open Graph Title: Error inferring typevar on generic classes · Issue #18706 · python/mypy
X Title: Error inferring typevar on generic classes · Issue #18706 · python/mypy
Description: Bug Report The inferred type of methods of a generic class are not bounded to typevar value on descendant classes. To Reproduce from typing import Iterator, Generic, TypeVar T = TypeVar("T", str, int) class Parent(Generic[T]): @classmeth...
Open Graph Description: Bug Report The inferred type of methods of a generic class are not bounded to typevar value on descendant classes. To Reproduce from typing import Iterator, Generic, TypeVar T = TypeVar("T", str, i...
X Description: Bug Report The inferred type of methods of a generic class are not bounded to typevar value on descendant classes. To Reproduce from typing import Iterator, Generic, TypeVar T = TypeVar("T&quo...
Opengraph URL: https://github.com/python/mypy/issues/18706
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Error inferring typevar on generic classes","articleBody":"**Bug Report**\n\nThe inferred type of methods of a generic class are not bounded to typevar value on descendant classes.\n\n\n**To Reproduce**\n\n```python\nfrom typing import Iterator, Generic, TypeVar\n\nT = TypeVar(\"T\", str, int)\n\nclass Parent(Generic[T]):\n\n @classmethod\n def method_1(cls, value: T) -> T:\n return 2*value\n \n def method_2(self, value: T) -> T:\n return 2*value\n \nclass ChildInt(Parent[int]):\n pass\n\ninstance = ChildInt()\n\nreveal_type(instance.method_1) # Revealed type is \"def (value: builtins.int) -> builtins.int\"\nreveal_type(ChildInt.method_1) # Revealed type is \"def (value: builtins.int) -> builtins.int\"\n\nreveal_type(instance.method_2) # Revealed type is \"def (value: builtins.int) -> builtins.int\"\nreveal_type(ChildInt.method_2) # Revealed type is \"def (self: __main__.Parent[T`1], value: T`1) -> T`1\"\n```\n\nhttps://mypy-play.net/?mypy=latest&python=3.12&gist=7ce2739ccc2b43f37187d54dcdd5f923\n\n**Expected Behavior**\n\n```python\nreveal_type(ChildInt.method_2) # Revealed type is \"def (self: __main__.ChildInt, value: builtins.int) -> builtins.int\"\n```\n\nThis soulution could be acceptable, as well: \n\n```python\nreveal_type(ChildInt.method_2) # Revealed type is \"def (self: __main__.Parent[builtins.int], value: builtins.int) -> builtins.int\"\n```\n\n**Actual Behavior**\n\n```python\nreveal_type(ChildInt.method_2) # Revealed type is \"def (self: __main__.Parent[T`1], value: T`1) -> T`1\"\n```\n\n\n**Your Environment**\n\n\u003c!-- Include as many relevant details about the environment you experienced the bug in -->\n\n- Mypy version used: 1.15\n- Mypy command-line flags: no flags\n- Mypy configuration options from `mypy.ini` (and other config files): no config\n- Python version used: 3.13\n\n**Releated issues**\n\n#15658\n","author":{"url":"https://github.com/alfred82santa","@type":"Person","name":"alfred82santa"},"datePublished":"2025-02-19T08:24:49Z","url":"https://github.com/python/mypy/issues/18706"}
| 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:4cb29ce7-f430-9ef9-c682-00c2f7a0b3ce |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9A34:1EF2:54EF49:75D75E:6B00154F |
| html-safe-nonce | 1ae9bc8ba58ccb1bf14f3f6de14355fb1e2069e3574406bd19adc8596c4820d1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QTM0OjFFRjI6NTRFRjQ5Ojc1RDc1RTo2QjAwMTU0RiIsInZpc2l0b3JfaWQiOiIzMTI4MjIxNTcwNjk1NzEzNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | cafb13a3184f890d9e2049acb475ca6aadeb14fc799b953db8e4b01572bbfc86 |
| hovercard-subject-tag | issue:2862580582 |
| 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/18706/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ce050fd1782aede2ff5f757e21087ae31836ff998ebb4dcaa576914f3466c0ff/python/mypy/issues/18706 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ce050fd1782aede2ff5f757e21087ae31836ff998ebb4dcaa576914f3466c0ff/python/mypy/issues/18706 |
| og:image:alt | Bug Report The inferred type of methods of a generic class are not bounded to typevar value on descendant classes. To Reproduce from typing import Iterator, Generic, TypeVar T = TypeVar("T", str, i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | alfred82santa |
| hostname | github.com |
| expected-hostname | github.com |
| None | a2dd2291b1b13cc9c06f27ec92fa3dbaf34116fc24c9347b8539db61884c59f4 |
| 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 | 70121d4e855ee0bb7b75cf15d061d91f8863c0f8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width