Title: Attribute of type `A | B | None` becomes `object` after narrowing checks · Issue #12009 · python/mypy · GitHub
Open Graph Title: Attribute of type `A | B | None` becomes `object` after narrowing checks · Issue #12009 · python/mypy
X Title: Attribute of type `A | B | None` becomes `object` after narrowing checks · Issue #12009 · python/mypy
Description: Bug Report When doing narrowing of a class attribute with a particular type shape A | B | None, the type of the attribute becomes object after the narrowing condition chain. To Reproduce The following is the most minimal reproduction I w...
Open Graph Description: Bug Report When doing narrowing of a class attribute with a particular type shape A | B | None, the type of the attribute becomes object after the narrowing condition chain. To Reproduce The follow...
X Description: Bug Report When doing narrowing of a class attribute with a particular type shape A | B | None, the type of the attribute becomes object after the narrowing condition chain. To Reproduce The follow...
Opengraph URL: https://github.com/python/mypy/issues/12009
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Attribute of type `A | B | None` becomes `object` after narrowing checks","articleBody":"**Bug Report**\r\n\r\nWhen doing narrowing of a class attribute with a particular type shape `A | B | None`, the type of the attribute becomes `object` after the narrowing condition chain.\r\n\r\n**To Reproduce**\r\n\r\nThe following is the most minimal reproduction I was able to create.\r\n\r\n```py\r\nclass A: pass\r\nclass B: pass\r\nclass C:\r\n attr: A | B | None\r\n\r\nc = C()\r\nif c.attr is None: pass\r\nelif isinstance(c.attr, A): pass\r\nelif isinstance(c.attr, B): pass # Optional line - no difference if removed\r\nreveal_type(c.attr)\r\n```\r\n\r\n**Expected Behavior**\r\n\r\nRevealed type is `A | B | None`.\r\n\r\n**Actual Behavior**\r\n\r\nRevealed type is `object`.\r\n\r\nI made some additional tests:\r\n\r\n- `pyright` gives `A | B | None` (good).\r\n- Doing `x = c.x` and then doing the narrowing and `reveal_type` on `attr` gives `A | B | None` (good).\r\n- Replacing the `None` with `A | B | D` gives `A | B | D` (good). Also if I replace `if isinstance(c.attr, D)` with `if type(c.attr) is D` (good). So the `None` seems essential.\r\n- With just `attr: A | None`, gives `A | None` (good). So more than one type besides the `None` seems essential.\r\n- Changing the `elif` -> `if` gives `A | B | None` (good). So the `elif` seems essential.\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: master, 0.931, 0.900 (didn't try earlier versions).\r\n- Mypy command-line flags: ``--strict``.\r\n- Mypy configuration options from `mypy.ini` (and other config files):\r\n- Python version used: 3.9, 3.10\r\n- Operating system and version: Arch Linux","author":{"url":"https://github.com/bluetech","@type":"Person","name":"bluetech"},"datePublished":"2022-01-18T15:51:18Z","url":"https://github.com/python/mypy/issues/12009"}
| 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:ffbcce2a-1d15-0a66-bcff-2cc02b5b16eb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9364:2A087:5C0870:7AFE5E:6B00341B |
| html-safe-nonce | 13b7478f7f899e13c559e05a93e287017825114b47e96832a927f5f1d3e3dd27 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MzY0OjJBMDg3OjVDMDg3MDo3QUZFNUU6NkIwMDM0MUIiLCJ2aXNpdG9yX2lkIjoiNTQxNTY3MDcyODAwMTMzNjM0NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ad370c24c417fee907e6620acbcb709933092ff1921620d726f31cd87d336464 |
| hovercard-subject-tag | issue:1107069795 |
| 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/12009/issue_layout |
| twitter:image | https://opengraph.githubassets.com/24040f317476627b6cf67c1c0159549415effbadb8f018c6d989006c7924d82a/python/mypy/issues/12009 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/24040f317476627b6cf67c1c0159549415effbadb8f018c6d989006c7924d82a/python/mypy/issues/12009 |
| og:image:alt | Bug Report When doing narrowing of a class attribute with a particular type shape A | B | None, the type of the attribute becomes object after the narrowing condition chain. To Reproduce The follow... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bluetech |
| 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