Title: A constrained TypeVar value fails type() narrowing tests · Issue #18428 · python/mypy · GitHub
Open Graph Title: A constrained TypeVar value fails type() narrowing tests · Issue #18428 · python/mypy
X Title: A constrained TypeVar value fails type() narrowing tests · Issue #18428 · python/mypy
Description: Bug Report When a TypeVar is constrained, passing a value of the constrained type into type() fails narrowing tests. To Reproduce from typing import TypeVar W = TypeVar("W", int, str) def fn(w: W) -> W: if type(w) is str: # main.py:30: n...
Open Graph Description: Bug Report When a TypeVar is constrained, passing a value of the constrained type into type() fails narrowing tests. To Reproduce from typing import TypeVar W = TypeVar("W", int, str) def fn(w: W) ...
X Description: Bug Report When a TypeVar is constrained, passing a value of the constrained type into type() fails narrowing tests. To Reproduce from typing import TypeVar W = TypeVar("W", int, str) def...
Opengraph URL: https://github.com/python/mypy/issues/18428
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"A constrained TypeVar value fails type() narrowing tests","articleBody":"**Bug Report**\r\n\r\nWhen a `TypeVar` is constrained, passing a value of the constrained type into `type()` fails narrowing tests.\r\n\r\n**To Reproduce**\r\n\r\n```python\r\nfrom typing import TypeVar\r\n\r\nW = TypeVar(\"W\", int, str)\r\n\r\ndef fn(w: W) -> W:\r\n if type(w) is str:\r\n # main.py:30: note: Revealed type is \"builtins.int\"\r\n # main.py:30: note: Revealed type is \"builtins.str\"\r\n reveal_type(w)\r\n\r\n # reports error:\r\n # main.py:31: error: Incompatible types in assignment (expression has type \"int\", variable has type \"str\") [assignment]\r\n s: str = w\r\n print(\"s: %s\" % s)\r\n\r\n elif type(w) is int:\r\n # same in this block\r\n reveal_type(w)\r\n i: int = w\r\n print(\"s: %d\" % i)\r\n \r\n return w\r\n```\r\nIf I change `W` to use binding, the problem goes away. That is, if I use this, no errors are reported.\r\n\r\n```python\r\nW = TypeVar(\"W\", bound=int|str)\r\n```\r\n\r\nTesting the original code in Pyright does not report any problems.\r\n\r\nUsing `isinstance()` makes it work for class types in general, but cannot be used against `TypedDict` instances and where a specific class type is required.\r\n\r\n**Expected Behavior**\r\n\r\nThe condition `type(w) is str` should narrow `w` to have `str` in its conditional block. \r\n\r\n**Actual Behavior**\r\n\r\nThese errors are reported:\r\n\r\n> main.py:35: error: Incompatible types in assignment (expression has type \"str\", variable has type \"int\") [assignment]\r\n> main.py:44: error: Incompatible types in assignment (expression has type \"int\", variable has type \"str\") [assignment]\r\n\r\n**Your Environment**\r\n\r\nThis is not environment specific and I can reproduce the same on Windows or Linux.\r\n\r\n- Mypy version used: 1.14.1\r\n- Mypy command-line flags: --strict\r\n- Mypy configuration options from `mypy.ini` (and other config files): none\r\n- Python version used: 3.10\r\n\r\n","author":{"url":"https://github.com/gh-andre","@type":"Person","name":"gh-andre"},"datePublished":"2025-01-07T17:05:54Z","url":"https://github.com/python/mypy/issues/18428"}
| 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:279ce8d3-3156-7a96-6423-b453216f327e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B5BE:22E601:115015:175969:6B001DFE |
| html-safe-nonce | b039def20bea4b0ac3cb493443ebf74fbf901d6f3e41cdd0aef6b16c7bb8cb06 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNUJFOjIyRTYwMToxMTUwMTU6MTc1OTY5OjZCMDAxREZFIiwidmlzaXRvcl9pZCI6IjE3MjA4NDE4ODM3NDEwNjg3OTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | be0f9cfd8129c296546007f396a5a1d50c46791e51437be73fe7ca43f8acbea3 |
| hovercard-subject-tag | issue:2773340701 |
| 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/18428/issue_layout |
| twitter:image | https://opengraph.githubassets.com/521704213e24e547a1ed6fd534693826677d11edeb647fbdfdbdb4609feb7db4/python/mypy/issues/18428 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/521704213e24e547a1ed6fd534693826677d11edeb647fbdfdbdb4609feb7db4/python/mypy/issues/18428 |
| og:image:alt | Bug Report When a TypeVar is constrained, passing a value of the constrained type into type() fails narrowing tests. To Reproduce from typing import TypeVar W = TypeVar("W", int, str) def fn(w: W) ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gh-andre |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width