Title: Incorrect type narrowing when swapping variables after isinstance check · Issue #21586 · python/mypy · GitHub
Open Graph Title: Incorrect type narrowing when swapping variables after isinstance check · Issue #21586 · python/mypy
X Title: Incorrect type narrowing when swapping variables after isinstance check · Issue #21586 · python/mypy
Description: Bug Report Swapping the variables doesn't seem to make mypy swap their types and the second variable is left with its type as before the swap. Changing the example below to use a temp variable fixes the issue: tmp = a a, b = b, tmp revea...
Open Graph Description: Bug Report Swapping the variables doesn't seem to make mypy swap their types and the second variable is left with its type as before the swap. Changing the example below to use a temp variable fixe...
X Description: Bug Report Swapping the variables doesn't seem to make mypy swap their types and the second variable is left with its type as before the swap. Changing the example below to use a temp variable ...
Opengraph URL: https://github.com/python/mypy/issues/21586
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect type narrowing when swapping variables after isinstance check","articleBody":"\u003c!--\nIf you're not sure whether what you're experiencing is a mypy bug, please see the \"Question and Help\" form instead.\nPlease consider:\n\n- checking our common issues page: https://mypy.readthedocs.io/en/stable/common_issues.html\n- searching our issue tracker: https://github.com/python/mypy/issues to see if it's already been reported\n- asking on gitter chat: https://gitter.im/python/typing\n-->\n\n**Bug Report**\n\n\u003c!--\nIf you're reporting a problem with a specific library function, the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues\n\nIf the project you encountered the issue in is open source, please provide a link to the project.\n-->\n\nSwapping the variables doesn't seem to make mypy swap their types and the second variable is left with its type as before the swap.\n\nChanging the example below to use a temp variable fixes the issue:\n```python\ntmp = a\na, b = b, tmp\nreveal_type(a) # note: Revealed type is \"test.Sub1\"\nreveal_type(b) # note: Revealed type is \"test.Sub2\"\n```\n\n**To Reproduce**\n\n```python\nclass Base:\n pass\n\nclass Sub1(Base):\n pass\n\nclass Sub2(Base):\n pass\n\ndef f(a: Base, b: Base) -> None:\n if isinstance(b, Sub1) and isinstance(a, Sub2):\n a, b = b, a\n reveal_type(a) # note: Revealed type is \"test.Sub1\"\n reveal_type(b) # note: Revealed type is \"test.Sub1\"\n```\n\n**Expected Behavior**\n\nType of `b` is `Sub2` after the swap.\n\n\u003c!--\nHow did you expect mypy to behave? It’s fine if you’re not sure your understanding is correct.\nWrite down what you thought would happen. If you expected no errors, delete this section.\n-->\n\n**Actual Behavior**\n\nType of `b` is `Sub1` after the swap.\n\n\u003c!-- What went wrong? Paste mypy's output. -->\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: mypy 2.2.0+dev.52de0c739d2f066ea06fa9a886bc8fcc9fa079c6\n- Mypy command-line flags: None\n- Mypy configuration options from `mypy.ini` (and other config files): None\n- Python version used: 3.13.5\n\n\u003c!-- You can freely edit this text, please remove all the lines you believe are unnecessary. -->\n","author":{"url":"https://github.com/p-sawicki","@type":"Person","name":"p-sawicki"},"datePublished":"2026-06-03T17:22:51Z","url":"https://github.com/python/mypy/issues/21586"}
| 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:2d1ae9d2-e8b5-c351-f563-231567cfe1fa |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E96E:1B0A31:22D361E:2F69B22:6B002CC2 |
| html-safe-nonce | 9506c1cac31fc1a4024314a41a9b61ae22deedb5b65d1bf92a9412d3c3d3d1e8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOTZFOjFCMEEzMToyMkQzNjFFOjJGNjlCMjI6NkIwMDJDQzIiLCJ2aXNpdG9yX2lkIjoiNDI3MDEyNDczNDUwODcwNzAxMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4e02c87d21be2eba559d3b2084a32501f783d18d89e849e4919ba14bc7dcd5ec |
| hovercard-subject-tag | issue:4582122040 |
| 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/21586/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7525bdfd9294c18aef0191de7f559fbcddd1ad135dde845ae9585b52e043f9f2/python/mypy/issues/21586 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7525bdfd9294c18aef0191de7f559fbcddd1ad135dde845ae9585b52e043f9f2/python/mypy/issues/21586 |
| og:image:alt | Bug Report Swapping the variables doesn't seem to make mypy swap their types and the second variable is left with its type as before the swap. Changing the example below to use a temp variable fixe... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | p-sawicki |
| 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