Title: Crash in union with a forward reference to a bad type. · Issue #4543 · python/mypy · GitHub
Open Graph Title: Crash in union with a forward reference to a bad type. · Issue #4543 · python/mypy
X Title: Crash in union with a forward reference to a bad type. · Issue #4543 · python/mypy
Description: This code causes a crash: Alias = Union[Bad, C, D] Bad = object() class C: pass class D: pass option: Alias if isinstance(option, C): pass Here is a traceback: tmp.py:3: error: Invalid type "tmp.Bad" tmp.py:13: error: INTERNAL ERROR -- p...
Open Graph Description: This code causes a crash: Alias = Union[Bad, C, D] Bad = object() class C: pass class D: pass option: Alias if isinstance(option, C): pass Here is a traceback: tmp.py:3: error: Invalid type "tmp.Ba...
X Description: This code causes a crash: Alias = Union[Bad, C, D] Bad = object() class C: pass class D: pass option: Alias if isinstance(option, C): pass Here is a traceback: tmp.py:3: error: Invalid type "t...
Opengraph URL: https://github.com/python/mypy/issues/4543
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Crash in union with a forward reference to a bad type.","articleBody":"This code causes a crash:\r\n```python\r\nAlias = Union[Bad, C, D]\r\n\r\nBad = object()\r\n\r\nclass C:\r\n pass\r\nclass D:\r\n pass\r\n\r\noption: Alias\r\nif isinstance(option, C):\r\n pass\r\n```\r\nHere is a traceback:\r\n```\r\ntmp.py:3: error: Invalid type \"tmp.Bad\"\r\ntmp.py:13: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.570-dev-d2c04190b8d407476c535f30d07a9f6712e7b12a-dirty\r\nTraceback (most recent call last):\r\n File \"/Users/ilevkivskyi/mypy/bin/mypy\", line 11, in \u003cmodule>\r\n load_entry_point('mypy===0.570-dev-d2c04190b8d407476c535f30d07a9f6712e7b12a-dirty', 'console_scripts', 'mypy')()\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/__main__.py\", line 7, in console_entry\r\n main(None)\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/main.py\", line 80, in main\r\n type_check_only(sources, bin_dir, options, flush_errors)\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/main.py\", line 129, in type_check_only\r\n flush_errors=flush_errors)\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/build.py\", line 172, in build\r\n result = _build(sources, options, alt_lib_path, bin_dir, saved_cache, flush_errors)\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/build.py\", line 258, in _build\r\n graph = dispatch(sources, manager)\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/build.py\", line 2048, in dispatch\r\n process_graph(graph, manager)\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/build.py\", line 2349, in process_graph\r\n process_stale_scc(graph, scc, manager)\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/build.py\", line 2525, in process_stale_scc\r\n graph[id].type_check_first_pass()\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/build.py\", line 1923, in type_check_first_pass\r\n self.type_checker().check_first_pass()\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/checker.py\", line 228, in check_first_pass\r\n self.accept(d)\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/checker.py\", line 323, in accept\r\n stmt.accept(self)\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/nodes.py\", line 928, in accept\r\n return visitor.visit_if_stmt(self)\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/checker.py\", line 2232, in visit_if_stmt\r\n if_map, else_map = self.find_isinstance_check(e)\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/checker.py\", line 2835, in find_isinstance_check\r\n return conditional_type_map(expr, vartype, type)\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/checker.py\", line 3191, in conditional_type_map\r\n remaining_type = restrict_subtype_away(current_type, proposed_precise_type)\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/subtypes.py\", line 817, in restrict_subtype_away\r\n new_items = [item for item in t.relevant_items()\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/subtypes.py\", line 818, in \u003clistcomp>\r\n if (not (is_proper_subtype(erase_type(item), erased_s) or\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/erasetype.py\", line 25, in erase_type\r\n return typ.accept(EraseTypeVisitor())\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/types.py\", line 194, in accept\r\n return visitor.visit_unbound_type(self)\r\n File \"/Users/ilevkivskyi/mypy/lib/python3.6/site-packages/mypy/erasetype.py\", line 31, in visit_unbound_type\r\n assert False, 'Not supported'\r\nAssertionError: Not supported\r\n```\r\n\r\nIt looks like the problem is that clearly erroneous types are not wrapped in `ForwardRef` and are not post-processed in third pass. There are two options that I see now for `UnboundType`s (maybe there are other):\r\n* Expect them in type checker and amend the corresponding visitors\r\n* Replace them with `Any` after giving an `Invalid type` error (maybe with better error message, but we already have an issue for this.)\r\n\r\nI would prefer the second option, since this seems simpler and would probably minimize the amount of not useful errors (because of the initially invalid type).","author":{"url":"https://github.com/ilevkivskyi","@type":"Person","name":"ilevkivskyi"},"datePublished":"2018-02-05T17:02:13Z","url":"https://github.com/python/mypy/issues/4543"}
| 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:438cc7d3-ebb6-923c-6f3c-5a95724c48bb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E946:1EE55B:25F46D2:34196B8:6B00476A |
| html-safe-nonce | 02553560ace13cfe4ba32cb91c72285b66c40d33e543421ab8aa23eb44163978 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOTQ2OjFFRTU1QjoyNUY0NkQyOjM0MTk2Qjg6NkIwMDQ3NkEiLCJ2aXNpdG9yX2lkIjoiNTI4NzQwMTc5OTI2NTk5NTMwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | eb2b3eb93b521c0e35e91b7111630576d879a7d6cfb62e6f0619b340fa26a6ef |
| hovercard-subject-tag | issue:294468163 |
| 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/4543/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e20d03a142bd7cd3ea62314fe56e69e141423a1301fb6277f290155a6d6791f4/python/mypy/issues/4543 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e20d03a142bd7cd3ea62314fe56e69e141423a1301fb6277f290155a6d6791f4/python/mypy/issues/4543 |
| og:image:alt | This code causes a crash: Alias = Union[Bad, C, D] Bad = object() class C: pass class D: pass option: Alias if isinstance(option, C): pass Here is a traceback: tmp.py:3: error: Invalid type "tmp.Ba... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ilevkivskyi |
| 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