Title: Incorrect type reification on optional union attribute in conditional · Issue #7884 · python/mypy · GitHub
Open Graph Title: Incorrect type reification on optional union attribute in conditional · Issue #7884 · python/mypy
X Title: Incorrect type reification on optional union attribute in conditional · Issue #7884 · python/mypy
Description: Repro: bug.py: from __future__ import annotations from typing import * class B: pass class A: name: Optional[Union[str, B]] def foo(a: A): if isinstance(a.name, str): pass elif isinstance(a.name, B): pass else: raise AssertionError bar(a...
Open Graph Description: Repro: bug.py: from __future__ import annotations from typing import * class B: pass class A: name: Optional[Union[str, B]] def foo(a: A): if isinstance(a.name, str): pass elif isinstance(a.name, B...
X Description: Repro: bug.py: from __future__ import annotations from typing import * class B: pass class A: name: Optional[Union[str, B]] def foo(a: A): if isinstance(a.name, str): pass elif isinstance(a.name, B...
Opengraph URL: https://github.com/python/mypy/issues/7884
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect type reification on optional union attribute in conditional","articleBody":"Repro:\r\n\r\n**bug.py**:\r\n\r\n```python\r\n\r\nfrom __future__ import annotations\r\n\r\nfrom typing import *\r\n\r\n\r\nclass B:\r\n pass\r\n\r\n\r\nclass A:\r\n name: Optional[Union[str, B]]\r\n\r\n\r\ndef foo(a: A):\r\n if isinstance(a.name, str):\r\n pass\r\n elif isinstance(a.name, B):\r\n pass\r\n else:\r\n raise AssertionError\r\n\r\n bar(a.name)\r\n\r\n\r\ndef bar(arg: Union[str, B]):\r\n pass\r\n```\r\n\r\nmypy incorrectly reports an error:\r\n\r\n`bug.py:22: error: Argument 1 to \"bar\" has incompatible type \"object\"; expected \"Union[str, B]\"`\r\n\r\nInterestingly, when `a.name` is aliased with another name, the issue disappers, i.e. the following is checked without an issue:\r\n\r\n**ok.py**:\r\n```python\r\nfrom __future__ import annotations\r\n\r\nfrom typing import *\r\n\r\n\r\nclass B:\r\n pass\r\n\r\n\r\nclass A:\r\n name: Optional[Union[str, B]]\r\n\r\n\r\ndef foo(a: A):\r\n a_name = a.name\r\n\r\n if isinstance(a_name, str):\r\n pass\r\n elif isinstance(a_name, B):\r\n pass\r\n else:\r\n raise AssertionError\r\n\r\n bar(a_name)\r\n\r\n\r\ndef bar(arg: Union[str, B]):\r\n pass\r\n```","author":{"url":"https://github.com/elprans","@type":"Person","name":"elprans"},"datePublished":"2019-11-05T17:12:54Z","url":"https://github.com/python/mypy/issues/7884"}
| 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:7b58806b-0c86-b7a9-8644-f301b020ad50 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D0B2:2E9DBB:27DA912:3684525:6B0049DE |
| html-safe-nonce | e0dfffcbb7a087c37abd96a7547838f07a70d7bbb4ad9f3731e5b3d3e9f973bc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMEIyOjJFOURCQjoyN0RBOTEyOjM2ODQ1MjU6NkIwMDQ5REUiLCJ2aXNpdG9yX2lkIjoiNTM2ODI4MDUxODQxODc3ODU5MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c8ddc0b4c9e9bdae6d30340d489adc0b27b1c8d27890c2251df0e8cd725e0fc4 |
| hovercard-subject-tag | issue:517906631 |
| 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/7884/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e7084fce9b900e16dfdde889c390fca74795ffa66076d3291f8ba9b6be9ce1de/python/mypy/issues/7884 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e7084fce9b900e16dfdde889c390fca74795ffa66076d3291f8ba9b6be9ce1de/python/mypy/issues/7884 |
| og:image:alt | Repro: bug.py: from __future__ import annotations from typing import * class B: pass class A: name: Optional[Union[str, B]] def foo(a: A): if isinstance(a.name, str): pass elif isinstance(a.name, B... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | elprans |
| hostname | github.com |
| expected-hostname | github.com |
| None | f9517bf360d46638a1e5e7daee7158842fe5ef43c9000afa0e127564a7c2f39b |
| 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 | b27c5dde30676028abd5c5fc53ce5c9b625f8281 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width