Title:
Open Graph Title:
X Title:
Description: Bug Report The type narrowing seems to fail in some situations, from what i can tell its caused when: The type is narrowed to a union of subclasses, then narrowed further by one of the branches of that subclass using isinstance. in the e...
Open Graph Description: Bug Report The type narrowing seems to fail in some situations, from what i can tell its caused when: The type is narrowed to a union of subclasses, then narrowed further by one of the branches of ...
X Description: Bug Report The type narrowing seems to fail in some situations, from what i can tell its caused when: The type is narrowed to a union of subclasses, then narrowed further by one of the branches of ...
Opengraph URL: https://github.com/python/mypy/issues/16413
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":" \u003cnothing> when narrowing types on union of subtypes","articleBody":"\u003c!--\r\nIf you're not sure whether what you're experiencing is a mypy bug, please see the \"Question and Help\" form instead.\r\nPlease consider:\r\n\r\n- checking our common issues page: https://mypy.readthedocs.io/en/stable/common_issues.html\r\n- searching our issue tracker: https://github.com/python/mypy/issues to see if it's already been reported\r\n- asking on gitter chat: https://gitter.im/python/typing\r\n-->\r\n\r\n**Bug Report**\r\n\r\nThe type narrowing seems to fail in some situations, from what i can tell its caused when:\r\n\r\nThe type is narrowed to a union of subclasses, then narrowed further by one of the branches of that subclass using `isinstance`.\r\n\r\nin the example, i define `Base` and to mixins (`FooMixin` and `BarMixin`), then a function that takes `Base` and checks for the presence of the mixins.\r\n\r\nI use `isinstance(item, (FooMixin, BarMixin)` as a guard which narrows the type of `item` to `Union[__main__.\u003csubclass of \"Base\" and \"FooMixin\">, __main__.\u003csubclass of \"Base\" and \"BarMixin\">]` as expected.\r\n\r\nthen if i use `isinstance(item, FooMixin)` instead of getting `FooMixin` or `__main__.\u003csubclass of \"Base\" and \"FooMixin\">` for the type of `item` i get `\u003cnothing>` which has been hard to google for but i'm assuming is an internal type in mypy for \"i have no idea what this is\"?\r\n\r\nthe actual code this happens in is complex, but this seems to be the simplest case I've found to recreate it.\r\n\r\n**To Reproduce**\r\n\r\nhttps://mypy-play.net/?mypy=latest&python=3.11&gist=f64cd0c65509ac3d6b6498fed4ced174\r\n\r\n**Expected Behavior**\r\n\r\ni would had expected the type to be narrowed to either `\u003csubclass of \"Base\" and \"FooMixin\">` or `FooMixin` and for there to be no error.\r\n\r\n**Actual Behavior**\r\n\r\n```\r\nmain.py:22: note: Revealed type is \"Union[__main__.\u003csubclass of \"Base\" and \"FooMixin\">, __main__.\u003csubclass of \"Base\" and \"BarMixin\">]\"\r\nmain.py:25: note: Revealed type is \"\u003cnothing>\"\r\nmain.py:26: error: \u003cnothing> has no attribute \"foo\" [attr-defined]\r\nmain.py:28: note: Revealed type is \"__main__.\u003csubclass of \"Base\" and \"BarMixin\">\"\r\n```\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: 1.6.1\r\n- Mypy command-line flags: none\r\n- Mypy configuration options from `mypy.ini` (and other config files): defaults\r\n- Python version used: 3.11.2\r\n","author":{"url":"https://github.com/danielknell","@type":"Person","name":"danielknell"},"datePublished":"2023-11-06T14:45:39Z","url":"https://github.com/python/mypy/issues/16413"}
| 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:19d74c5e-e27a-e753-9d05-1ba2352bd3b9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CB44:2F2EB9:2E9DA0D:3DCAAFB:6B0028A3 |
| html-safe-nonce | 47cd23b0433cec8ff01125691e3b21df3a166dd0b1b79d5333786c1c8be988ee |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQjQ0OjJGMkVCOToyRTlEQTBEOjNEQ0FBRkI6NkIwMDI4QTMiLCJ2aXNpdG9yX2lkIjoiNjcxNDQ3NDI5MjkyMDE5OTMzMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9f70eae9d6146f24fb39a41ad91f8fe1c3c1314ebaf7dc5084ae6f7c6e85584e |
| hovercard-subject-tag | issue:1979342441 |
| 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/16413/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7ebe73d4fdaafac3108d617dc7c0f4db63026c6ffea3d4cdb38cd09b12059157/python/mypy/issues/16413 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7ebe73d4fdaafac3108d617dc7c0f4db63026c6ffea3d4cdb38cd09b12059157/python/mypy/issues/16413 |
| og:image:alt | Bug Report The type narrowing seems to fail in some situations, from what i can tell its caused when: The type is narrowed to a union of subclasses, then narrowed further by one of the branches of ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | danielknell |
| 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 | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width