Title: [Conformance/Spec] Type Checker Divergence in Variance Inference · Issue #2281 · python/typing · GitHub
Open Graph Title: [Conformance/Spec] Type Checker Divergence in Variance Inference · Issue #2281 · python/typing
X Title: [Conformance/Spec] Type Checker Divergence in Variance Inference · Issue #2281 · python/typing
Description: Update: Since v0.0.36 ty now also considers Foo covariant in T in the example below Currently, all type checkers pass the generics_variance_inference portion of the conformance test suite. However, there is some divergence with generic m...
Open Graph Description: Update: Since v0.0.36 ty now also considers Foo covariant in T in the example below Currently, all type checkers pass the generics_variance_inference portion of the conformance test suite. However,...
X Description: Update: Since v0.0.36 ty now also considers Foo covariant in T in the example below Currently, all type checkers pass the generics_variance_inference portion of the conformance test suite. However,...
Opengraph URL: https://github.com/python/typing/issues/2281
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Conformance/Spec] Type Checker Divergence in Variance Inference","articleBody":"**Update:** Since v0.0.36 `ty` now also considers `Foo` covariant in `T` in the example below\n\nCurrently, all type checkers pass the [`generics_variance_inference` portion of the conformance test suite](https://htmlpreview.github.io/?https://github.com/python/typing/blob/main/conformance/results/results.html). However, there is some divergence with generic methods that have bound self-type: \n[[mypy]](https://mypy-play.net/?gist=2b75369999f5d7278f1ff3ab56edd03c), [[pyright]](https://pyright-play.net/?code=MYGwhgzhAEBiD28DaAVAugLgFDV9AJgKYBm0A5oQC4AUEhIxAlNALQB80KG0AdHzniKkw%2BfEgDKaWvWLcEySQBpo8SgAtCAJ24gAlhEoS0ygPTN2cREe58eWLEOiVCBgPrB4ANzCbdYAHbAhNQ%2BZHJWuv6UaOYc8kjwAEYAVoTA0dh40JpUAK6a-tChuADE0AC2AJ4ADpXKAF65iQHKlJXcgDLkgPB-yrW%2BZGqUvZU5xCDt0ICg5F1AA), [[ty]](https://play.ty.dev/f6f5b8e9-9c4e-4ef4-a559-53662c76f5f1), [[pyrefly]](https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIAxlKnHAAQBiuuA2gCoC6iAOuvQPqYYYegHMYAFwAUcGFDABKegFoAfPXaJ6hXX0FCR9VJkysAyp1nyw2niGZtL9gDT1ckgBYwATtscWnG4A9Mrq9PYBzkg6euh8wqKS8JIA%2BpS4AG6oPhAYlDDSOWL%2BLKwQ6JKcYRoBuNgAVjCUVbz8gj5SAK4%2B-MUCAMT0ALakZG4AXl3YGG6SpNqAMuSA8H9uZLlinpJrpJ1gUAv0gKDky3wgLiDrIgeEkrjDUBRDAAq716T0aFh4%2BPQZ6JAxD1UJIILh0IQ%2BENzDAYPQtpJiHBEMFgld9qRCLgfGJgjB0MFMLhKHBgv9AcDQeDgvQwNjjNloKhsLA-uCKT4QWD%2BLhiFT0HBIegyF5wSpMr44Nz6ABeCIgADMhAAjAAWex8Vi%2BHzYuCcPhddAQYbEbHJTAqTAQTotCAS2X0ADkEDE6GxMEdfDdkhUnQAjl1rTALQBrGCkFSoSgFOgOx0Adxy6E96BAAF8LlHQRLGNAYBQvjgCCRyGmgA)\n\n```python\nclass Foo[T]:\n def get(self) -> T: ...\n def add[S](self: Foo[S], other: list[S], /) -> Foo[S]: ...\n\ndef test_covariance(arg: Foo[int]) -> Foo[object]:\n return arg # mypy, zuban, ty: ❌️, pyright, pyrefly: ✅️\n```\n\nFollowing the [variance inference specification](https://typing.python.org/en/latest/spec/generics.html#variance-inference), I believe `Foo` should be covariant in `T`, which is what `pyright` and `pyrefly` say, because `Foo[Dummy].add` is assignable to `Foo[object].add`. Intuitively, since `S` is method and not class scoped, and `T` does not appear in the signature of `Foo.add`, it has no bearing on variance.\n\nHowever, `mypy`, `ty` and `zuban` think `Foo` is invariant in `T`. In https://github.com/python/mypy/pull/19466#discussion_r2432203931, @ilevkivskyi argued that for an instance, `def add[S](self: Foo[S], other: list[S], /) -> Foo[S]: ...` is indistinguishable from `def add(self, other: list[T], /) -> Foo[T]: ...` and therefore `Foo` should be invariant in `T`.\n\nThis argumentation reveals another divergence between type checkers when it comes to subclassing generics: [[mypy]](https://mypy-play.net/?gist=0869df5e6345f426cba8dfe419ddbc5f), [[ty]](https://play.ty.dev/0e291fd1-d71f-4cd2-ae2f-5c1f8b58278a), [[pyrefly]](https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIAxlKnHAAQBiuuA2gCoC6iAOuvQPqYYYegHMYAFwAUcGFDABKegFoAfPXaJ6hXX0FCR9VJkysAyp1nyw2niGZtL9gDT1ckgBYwATtqgQcJIWnG4A9Mrq9PaOIfbauoR8fNS0DOYArtjSsRDokpyKvPyCAMT0ALakZG4AXlkYbpKkbmQ%2BIlCk2oCg5IDwf62kPhBinpLagDLkvfqCwqImmNYKbh7efvQBQax5BZEa9pnY8Tq6IC4gbR2khJK4FVAU5QAKg5f0aFh4%2BPSUuOiQYhkfKhJBBfkl0OVzDAYPRRpJiHBEGEwhcwJ1CLgfGIwjB0GFMLhKHAwj8-sNAcDQXj6GBMcYAG6oaCobCwb6-f4UkG-dzEbnoODgsheX4qem%2BOBU%2BgAXmiIAAzIQAIwAFnsfFYvh8mLgnD4GXQEAqxExkhgmBUmAg7UoIPFMvoAHJhuhMTBHXxXZIVO0AI4Za3mlQAaxgpBUqEolHgDFljoA7qgfOgPegQABfM6Ru0wRjQGAUd44AgkcjpoA), [[pyright]](https://pyright-play.net/?code=MYGwhgzhAEBiD28DaAVAugLgFDV9AJgKYBm0A5oQC4AUEhIxAlNALQB80KG0AdHzniKkw%2BfEgDKaWvWLcEySQBpo8SgAtCAJ24gAlhEoS0ygPTN2cREe58eWLKEgxxAVwBG1eUl0A7SmkZsPGgAYmgAWwBPAAdI5QAvdzAfZUo46FjNEhBI7kBQckB4P%2BVM3TI1Sm5AGXICgVwhaBF8aQZlVQ1taD0Dbz8A1g5XNxs%2BIA)\n\n```python\nclass Foo[T]:\n def get(self) -> T: ...\n def add[S](self: Foo[S], other: list[S], /) -> Foo[S]: ...\n\nclass Sub(Foo[int]):\n # mypy, zuban, ty, pyrefly: ✅️, pyright: ❌️\n def add(self, other: list[int]) -> Sub: ...\n```\n\n(I believe `pyright` is correct to error here, as for example a function that expects a `Foo[int]` could in principle try to call `type(arg).add(Foo[str](), [\"a\", \"b\", \"c\"])`)\n\nNotably, if we remove the `self: Foo[S]` annotation, then the inference changes:\n[[mypy]](https://mypy-play.net/?gist=942e7831ef27d9365ab249a2f48cc1b0), [[pyright]](https://pyright-play.net/?code=MYGwhgzhAEBiD28DaAVAugLgFDV9AJgKYBm0A5oQC4AUEhIxAlNALQB80KG0AdHzniKkw%2BfEgDKaWvWIAaaPEoALQgCduIAJYRKEtPID0zdnER7ufHlixDolQjoD6weADcwqzWAB2wQtQ8ybgRkTW9KNGMOEKR4ACMAK0JgCOw8aFUqAFdVb2hA3ABiaABbAE8ABzL5SmroKs8yJUp5BpIQOoAvLLifbkBQckB4P%2BtQSBhxHuoYsIjGNLxi2oHB0sq6hs0mluhu3u9WssziDu5AGXJh9NsRfGkGeUUVdWgtHSQZyNYOCbiLPiA), [[pyrefly]](https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIAxlKnHAAQBiuuA2gCoC6iAOuvQPqYYYegHMYAFwAUcGFDABKegFoAfPXaJ6hXX0FCR9VJkysAyp1nywAGnq5JACxgAnbVAhxJFzvYD0yur0PCDMbJah2rqEfHzCopLwkgD6lLgAbqiuEBiUMNLZYtrhrBDokpxBGqW42ABWMJSVvPyCrlIArq78RQIAxPQAtqRk9pKk9mQ5Yk6SU6QdYFCT9ABendgY2oCg5IDwf3Ho1LQM5pvSpeWViq0GgxO7e8Ojq9MQs-PrmxgLSyvagBlyA5tAQJYymawKeyOFzueiebxlCpVVQaUJnbBRHS6EC2EDTEQrQiSXBDKAUQYABUWhNI9DQWDw%2BHo6XQkDE3VQkgguHQsXQg3MMBg9DmkmIcEQ-n8BOWpEIuFcYn8MHQ-kwuEocH8rPZnO5vP89DAiuMWWgqGwsBZvL1ri5PP4uGIBvQcH5ZGcvJUGTccEd9AAvCEQABmQgARgALKE%2BKw3K5FXBOHxOugIENiIqkpgVJgIB1mhBfUH6AByd7oRUwMt8KuSFQdACOnQLMFzAGsYKQVKhKPk6KWywB3bLoWvoEAAXzxfe5vsY0BgFAZOAIJHIU6AA), [[ty]](https://play.ty.dev/36714b64-0db5-486f-9a30-30a9ff645b58)\n\n```python\nclass Foo[T]:\n def get(self) -> T: ...\n def add[S](self, other: list[S], /) -> Foo[S]: ...\n\ndef test_covariance(arg: Foo[int]) -> Foo[object]:\n return arg # mypy, ty, pyright, pyrefly, zuban: ✅️\n\nclass Sub(Foo[int]):\n # ty: ✅️ mypy, pyright, zuban, pyrefly: ❌️\n def add(self, other: list[int]) -> Sub: ...\n```\n\n## What's needed\n\nType checker divergence in variance inference is quite insidious; I believe the spec should be clarified in this regard and examples like the ones in this issue should be added to the conformance suite.\n\nI believe that `pyright` is the only type checker with correct behavior here at the moment, but maybe @erictraut can comment on these examples, and as there is some disagreement, at least from the `mypy` maintainers, I believe this needs some discussion.\n\n## References / Other Discussions\n\n- https://github.com/python/mypy/issues/19439\n- https://github.com/python/mypy/issues/20644\n- https://github.com/python/mypy/issues/18334\n- https://github.com/python/mypy/pull/19466","author":{"url":"https://github.com/randolf-scholz","@type":"Person","name":"randolf-scholz"},"datePublished":"2026-05-10T15:11:13Z","url":"https://github.com/python/typing/issues/2281"}
| 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:b47b1b89-5d83-78b8-897b-2f5267f4c2bf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9066:160FAB:BD2E2:1094A2:6B0052A2 |
| html-safe-nonce | ec07e4b55aeb518f395f53a32ccb2432b220fa6b3a587bb5eb9c3422d94b630f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MDY2OjE2MEZBQjpCRDJFMjoxMDk0QTI6NkIwMDUyQTIiLCJ2aXNpdG9yX2lkIjoiODA1NTc3OTQ3OTgxMDc1NzI4MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6f2b3e944976d8e2d83a10d781688a374c2be7ef7f81e14e407432c33ac4a504 |
| hovercard-subject-tag | issue:4415979804 |
| 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/typing/2281/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0f6205254babcd54fc5d7c0a0ad30e7f22e75940f98c420241b29bb948f2caa9/python/typing/issues/2281 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0f6205254babcd54fc5d7c0a0ad30e7f22e75940f98c420241b29bb948f2caa9/python/typing/issues/2281 |
| og:image:alt | Update: Since v0.0.36 ty now also considers Foo covariant in T in the example below Currently, all type checkers pass the generics_variance_inference portion of the conformance test suite. However,... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | randolf-scholz |
| hostname | github.com |
| expected-hostname | github.com |
| None | f420f4996e6b1ff1ccd6d7683f8134f4b5f0f8a50124c210dbfe7490e5644abb |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typing git https://github.com/python/typing.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 24604008 |
| octolytics-dimension-repository_nwo | python/typing |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24604008 |
| octolytics-dimension-repository_network_root_nwo | python/typing |
| 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 | 26e00df917aba0f128135dc073ea0c5e33a70d40 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width