Title: Mypy rejects assignment `type[T]=X` despite `T` having a bound of `X` · Issue #21157 · python/mypy · GitHub
Open Graph Title: Mypy rejects assignment `type[T]=X` despite `T` having a bound of `X` · Issue #21157 · python/mypy
X Title: Mypy rejects assignment `type[T]=X` despite `T` having a bound of `X` · Issue #21157 · python/mypy
Description: Bug Report The following signature complains about assigning RepositoryInterface to type[T] where T has a bound RepositoryInterface. self, expected: type[T] = RepositoryInterface ) -> Iterator[T]: Why: Idea behind the signature: reposito...
Open Graph Description: Bug Report The following signature complains about assigning RepositoryInterface to type[T] where T has a bound RepositoryInterface. self, expected: type[T] = RepositoryInterface ) -> Iterator[T]: ...
X Description: Bug Report The following signature complains about assigning RepositoryInterface to type[T] where T has a bound RepositoryInterface. self, expected: type[T] = RepositoryInterface ) -> Iterator[T...
Opengraph URL: https://github.com/python/mypy/issues/21157
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Mypy rejects assignment `type[T]=X` despite `T` having a bound of `X`","articleBody":"**Bug Report**\nThe following signature complains about assigning `RepositoryInterface` to `type[T]` where `T` has a bound `RepositoryInterface`.\n``` def repositories[T: RepositoryInterface](\n self, expected: type[T] = RepositoryInterface\n ) -> Iterator[T]:\n```\n\nWhy: Idea behind the signature: `repositories()` gives me an object which implements `RepositoryInterface`,\nbut I can also ask for a more specific type which might implement additonal methods, i.e. `repositories(Bar)`, gives me a `Bar` (so I and the typechecker know that I have a `buzzer` method on the return value.\n\n**To Reproduce**\n\nhttps://mypy-play.net/?gist=5eb2082fc5bbfea262f77ce00383d5be\n```python\n# Ideally, a small sample program that demonstrates the problem.\n# Or even better, a reproducible playground link https://mypy-play.net/ (use the \"Gist\" button)\nfrom abc import ABC, abstractmethod\nfrom typing import TYPE_CHECKING, Protocol, override\n\nif TYPE_CHECKING:\n from collections.abc import Iterator\n\n\nclass RepositoryInterface(Protocol):\n \"\"\"Interface for a repository.\"\"\"\n\n\nclass Implementation(RepositoryInterface):\n pass\n\n\nclass Bar(RepositoryInterface):\n def buzzer(self) -> None:\n print(\"bzzz\")\n\n\nclass FooForm(ABC):\n @abstractmethod\n def repositories[T: RepositoryInterface](\n self, expected: type[T] = RepositoryInterface\n ) -> Iterator[T]:\n pass\n\n\nclass Foo(FooForm):\n @override\n def repositories[T: RepositoryInterface](\n self, expected: type[T] = RepositoryInterface\n ) -> Iterator[T]:\n if issubclass(Implementation, expected):\n if TYPE_CHECKING:\n # Ugly workarround isinstnace on type[T] does not make sense, but\n # mypy does not narrow the type correctly without this\n # Bug reported here: https://github.com/python/mypy/issues/19917\n if not isinstance(expected, Implementation):\n raise TypeError(f\"Expected {expected}, got {type(self)}\")\n\n yield Implementation()\n elif issubclass(Bar, expected):\n if TYPE_CHECKING:\n # Ugly workarround isinstnace on type[T] does not make sense, but\n # mypy does not narrow the type correctly without this\n # Bug reported here: https://github.com/python/mypy/issues/19917\n if not isinstance(expected, Bar):\n raise TypeError(f\"Expected {expected}, got {type(self)}\")\n\n yield Bar()\n else:\n raise TypeError(f\"Expected {expected}, got {type(self)}\")\n```\n\n**Expected Behavior**\n\nmypy does not complain about an assignment of `Foo` to `type[T]` if `T` has a bound `Foo`\n\n**Actual Behavior**\n```\nmain.py:24: error: Incompatible default for argument \"expected\" (default has type \"type[RepositoryInterface]\", argument has type \"type[T]\") [assignment]\nmain.py:32: error: Incompatible default for argument \"expected\" (default has type \"type[RepositoryInterface]\", argument has type \"type[T]\") [assignment]\n```\n**Your Environment**\n\n- Mypy version used: 1.20\n- Mypy command-line flags:\n- Mypy configuration options from `mypy.ini` (and other config files):\n- Python version used: 3.14\n\nThis issue might be related to https://github.com/python/mypy/issues/3737","author":{"url":"https://github.com/maltevesper","@type":"Person","name":"maltevesper"},"datePublished":"2026-04-03T08:36:28Z","url":"https://github.com/python/mypy/issues/21157"}
| 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:1fee3505-ebf3-0f05-d5af-c4984d715ae4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C676:179A45:9F9A8:CF467:6AFF9C0A |
| html-safe-nonce | 24d5994d6164a1aee4009a5ec02f4ca0efb502cf7b6ca836b05f83cd99322d1f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNjc2OjE3OUE0NTo5RjlBODpDRjQ2Nzo2QUZGOUMwQSIsInZpc2l0b3JfaWQiOiI3MDUxNjM0MDkxODU0MTA5NzA2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 37eebe06094251521e71350d5f28b905c29666c052023f6988437a046a2fa6cc |
| hovercard-subject-tag | issue:4199249569 |
| 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/21157/issue_layout |
| twitter:image | https://opengraph.githubassets.com/94713c29f49c27442876b7137accffe8c78f7b6c1c42f0c95d9772df79d27087/python/mypy/issues/21157 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/94713c29f49c27442876b7137accffe8c78f7b6c1c42f0c95d9772df79d27087/python/mypy/issues/21157 |
| og:image:alt | Bug Report The following signature complains about assigning RepositoryInterface to type[T] where T has a bound RepositoryInterface. self, expected: type[T] = RepositoryInterface ) -> Iterator[T]: ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | maltevesper |
| hostname | github.com |
| expected-hostname | github.com |
| None | 2c75d045e8c84b51e2ac680086a6dd1eeaf2bc15b82137d22ee5106835d6256c |
| 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 | 525b66d22c4f7e6bb8b62934bec198964f7e8b93 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width