Title: Inheritance + if/elif/else chain of isinstance() doesn't constrain type as expected · Issue #4864 · python/mypy · GitHub
Open Graph Title: Inheritance + if/elif/else chain of isinstance() doesn't constrain type as expected · Issue #4864 · python/mypy
X Title: Inheritance + if/elif/else chain of isinstance() doesn't constrain type as expected · Issue #4864 · python/mypy
Description: I have something like the following code: from typing import Any, Union class A: pass class B(A): def __init__(self): self.member = 1 class C(A): def __init__(self): self.member = 2 def f(obj: A) -> int: if isinstance(obj, B): pass elif ...
Open Graph Description: I have something like the following code: from typing import Any, Union class A: pass class B(A): def __init__(self): self.member = 1 class C(A): def __init__(self): self.member = 2 def f(obj: A) -...
X Description: I have something like the following code: from typing import Any, Union class A: pass class B(A): def __init__(self): self.member = 1 class C(A): def __init__(self): self.member = 2 def f(obj: A) -...
Opengraph URL: https://github.com/python/mypy/issues/4864
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Inheritance + if/elif/else chain of isinstance() doesn't constrain type as expected","articleBody":"I have something like the following code:\r\n\r\n```\r\nfrom typing import Any, Union\r\n\r\n\r\nclass A:\r\n pass\r\n\r\n\r\nclass B(A):\r\n def __init__(self):\r\n self.member = 1\r\n\r\n\r\nclass C(A):\r\n def __init__(self):\r\n self.member = 2\r\n\r\n\r\ndef f(obj: A) -> int:\r\n if isinstance(obj, B):\r\n pass\r\n elif isinstance(obj, C):\r\n pass\r\n else:\r\n raise ValueError()\r\n return obj.member\r\n```\r\n\r\nmypy claims:\r\n\r\n```\r\ntest.py:25: error: \"A\" has no attribute \"member\"\r\n```\r\n\r\nHowever, at line 25, it's clear that obj must be either a `B` or a `C`. If I remove the `elif isinstance(obj, C)` branch, then there is no error.\r\n\r\nThis similar code has a related error:\r\n\r\n```\r\nfrom typing import Any, Union\r\n\r\n\r\nclass A:\r\n pass\r\n\r\n\r\nclass B(A):\r\n def __init__(self):\r\n self.member = 1\r\n\r\n\r\nclass C(A):\r\n def __init__(self):\r\n self.member = 2\r\n\r\n\r\ndef f(obj: A) -> Union[B, C]:\r\n if isinstance(obj, B):\r\n pass\r\n elif isinstance(obj, C):\r\n pass\r\n else:\r\n raise ValueError()\r\n return obj\r\n```\r\n\r\n```\r\ntest.py:25: error: Incompatible return value type (got \"A\", expected \"Union[B, C]\")\r\n```\r\nInterestingly, if I remove the inheritance so that `B` and `C` are no longer subclasses of `A`, there is no error.\r\n\r\nThis happens on mypy 0.570 and current git (0.590-dev-3346cfad75374c096b1dd8024df24ce50f18a35c).","author":{"url":"https://github.com/osandov","@type":"Person","name":"osandov"},"datePublished":"2018-04-06T06:00:42Z","url":"https://github.com/python/mypy/issues/4864"}
| 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:c339887d-5bd8-9560-b2d5-2b743ecca3a0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E886:26654F:341CBDB:456F5E1:6B004698 |
| html-safe-nonce | 495d24cdb2e1d455bd9ac28e4e65917d3b1a52f4123ecaeac6d6ad09b2fcda42 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFODg2OjI2NjU0RjozNDFDQkRCOjQ1NkY1RTE6NkIwMDQ2OTgiLCJ2aXNpdG9yX2lkIjoiMjIwNzE4OTU1NzgzOTkzOTIyNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 77c5a24e20cd61ef23594cc3e9b049c34f539b279f2ca9c68939932b09cff36f |
| hovercard-subject-tag | issue:311856670 |
| 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/4864/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4adf53ad36daee55c5b2cb7a5c74ab18ddd5e501a8b45df48cce49c42a759994/python/mypy/issues/4864 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4adf53ad36daee55c5b2cb7a5c74ab18ddd5e501a8b45df48cce49c42a759994/python/mypy/issues/4864 |
| og:image:alt | I have something like the following code: from typing import Any, Union class A: pass class B(A): def __init__(self): self.member = 1 class C(A): def __init__(self): self.member = 2 def f(obj: A) -... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | osandov |
| 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