Title: isinstance(x, (T1, T2)) won't narrow the type of x when NamedTuple is involved. · Issue #1914 · python/mypy · GitHub
Open Graph Title: isinstance(x, (T1, T2)) won't narrow the type of x when NamedTuple is involved. · Issue #1914 · python/mypy
X Title: isinstance(x, (T1, T2)) won't narrow the type of x when NamedTuple is involved. · Issue #1914 · python/mypy
Description: Test case: from typing import NamedTuple, Union A = NamedTuple('A', [('y', int)]) B = NamedTuple('B', [('x', int)]) C = NamedTuple('C', [('x', int)]) def foo(a: Union[A, B, C]): if isinstance(a, (B, C)): a.x b = a # type: Union[B, C] Thi...
Open Graph Description: Test case: from typing import NamedTuple, Union A = NamedTuple('A', [('y', int)]) B = NamedTuple('B', [('x', int)]) C = NamedTuple('C', [('x', int)]) def foo(a: Union[A, B, C]): if isinstance(a, (B...
X Description: Test case: from typing import NamedTuple, Union A = NamedTuple('A', [('y', int)]) B = NamedTuple('B', [('x', int)]) C = NamedTuple('C', [('x', int)])...
Opengraph URL: https://github.com/python/mypy/issues/1914
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"isinstance(x, (T1, T2)) won't narrow the type of x when NamedTuple is involved.","articleBody":"Test case:\n\n``` python\nfrom typing import NamedTuple, Union\n\nA = NamedTuple('A', [('y', int)])\nB = NamedTuple('B', [('x', int)])\nC = NamedTuple('C', [('x', int)])\n\ndef foo(a: Union[A, B, C]):\n if isinstance(a, (B, C)):\n a.x\n b = a # type: Union[B, C]\n```\n\nThis fails with:\n\n```\n1.py: note: In function \"foo\":\n1.py:9: error: Some element of union has no attribute \"x\"\n1.py:10: error: Incompatible types in assignment (expression has type \"Union[A, B, C]\", variable has type \"Union[B, C]\")\n```\n\nThe error happens only when NamedTuple is involved. Otherwise the type of `a` is correctly narrowed to `Union[B, C]` inside the branch.\n\ncc #1328 \n\n```\n$ mypy -V\nmypy 0.4.3\n$ python3 -V\nPython 3.5.2\n```\n","author":{"url":"https://github.com/kennytm","@type":"Person","name":"kennytm"},"datePublished":"2016-07-20T15:54:24Z","url":"https://github.com/python/mypy/issues/1914"}
| 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:c8030ce9-25bc-a937-01cd-b49a51cf2e3a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C6B4:2AD163:1C1EE88:25FEB76:6B000677 |
| html-safe-nonce | 90649e57ff30fadcbaa65e1f195fc21e7fc0ab71559e1186ba194dbac67927ba |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNkI0OjJBRDE2MzoxQzFFRTg4OjI1RkVCNzY6NkIwMDA2NzciLCJ2aXNpdG9yX2lkIjoiOTE1MzY4MDc5MTIwMzQ4MTIwNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 05c0aac391b9098bb85d4d98219c48a86e59cc5c863cd70c15c48443ed51200e |
| hovercard-subject-tag | issue:166617021 |
| 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/1914/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ed18cc7dbf3c51fc37426bb793eac0680caeb63e9c8f375dd49b379deceed569/python/mypy/issues/1914 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ed18cc7dbf3c51fc37426bb793eac0680caeb63e9c8f375dd49b379deceed569/python/mypy/issues/1914 |
| og:image:alt | Test case: from typing import NamedTuple, Union A = NamedTuple('A', [('y', int)]) B = NamedTuple('B', [('x', int)]) C = NamedTuple('C', [('x', int)]) def foo(a: Union[A, B, C]): if isinstance(a, (B... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kennytm |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width