Title: internal error when inferring type of callable resulting from untyped ternary expression · Issue #9234 · python/mypy · GitHub
Open Graph Title: internal error when inferring type of callable resulting from untyped ternary expression · Issue #9234 · python/mypy
X Title: internal error when inferring type of callable resulting from untyped ternary expression · Issue #9234 · python/mypy
Description: Consider the following example: import random from typing import Union, Callable class Wrapper: def __init__(self, x): self.x = x def main() -> Union[Wrapper, int]: wrap = Wrapper if random.random() < 0.5 else lambda x: x return wrap(2) ...
Open Graph Description: Consider the following example: import random from typing import Union, Callable class Wrapper: def __init__(self, x): self.x = x def main() -> Union[Wrapper, int]: wrap = Wrapper if random.random(...
X Description: Consider the following example: import random from typing import Union, Callable class Wrapper: def __init__(self, x): self.x = x def main() -> Union[Wrapper, int]: wrap = Wrapper if random.rand...
Opengraph URL: https://github.com/python/mypy/issues/9234
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"internal error when inferring type of callable resulting from untyped ternary expression","articleBody":"Consider the following example:\r\n\r\n```\r\nimport random\r\nfrom typing import Union, Callable\r\n\r\n\r\nclass Wrapper:\r\n def __init__(self, x):\r\n self.x = x\r\n\r\ndef main() -> Union[Wrapper, int]:\r\n wrap = Wrapper if random.random() \u003c 0.5 else lambda x: x\r\n return wrap(2)\r\n```\r\n\r\nRunning mypy (from the master branch in the repo) on it crashes with the following stack trace:\r\n\r\n```\r\nmypytest.py:18: error: INTERNAL ERROR -- Please try using mypy master on Github:\r\nhttps://mypy.rtfd.io/en/latest/common_issues.html#using-a-development-mypy-build\r\nPlease report a bug at https://github.com/python/mypy/issues\r\nversion: 0.790+dev.cc46692020ef6b672b761512095fbba281fdefc7\r\nTraceback (most recent call last):\r\n File \"/home/edo/miniconda3/envs/abm2/bin/mypy\", line 8, in \u003cmodule>\r\n sys.exit(console_entry())\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/__main__.py\", line 8, in console_entry\r\n main(None, sys.stdout, sys.stderr)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/main.py\", line 89, in main\r\n res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/build.py\", line 180, in build\r\n result = _build(\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/build.py\", line 252, in _build\r\n graph = dispatch(sources, manager, stdout)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/build.py\", line 2626, in dispatch\r\n process_graph(graph, manager)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/build.py\", line 2949, in process_graph\r\n process_stale_scc(graph, scc, manager)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/build.py\", line 3047, in process_stale_scc\r\n graph[id].type_check_first_pass()\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/build.py\", line 2107, in type_check_first_pass\r\n self.type_checker().check_first_pass()\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checker.py\", line 294, in check_first_pass\r\n self.accept(d)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checker.py\", line 401, in accept\r\n stmt.accept(self)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/nodes.py\", line 677, in accept\r\n return visitor.visit_func_def(self)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checker.py\", line 726, in visit_func_def\r\n self._visit_func_def(defn)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checker.py\", line 730, in _visit_func_def\r\n self.check_func_item(defn, name=defn.name)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checker.py\", line 792, in check_func_item\r\n self.check_func_def(defn, typ, name)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checker.py\", line 975, in check_func_def\r\n self.accept(item.body)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checker.py\", line 401, in accept\r\n stmt.accept(self)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/nodes.py\", line 1005, in accept\r\n return visitor.visit_block(self)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checker.py\", line 1973, in visit_block\r\n self.accept(s)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checker.py\", line 401, in accept\r\n stmt.accept(self)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/nodes.py\", line 1141, in accept\r\n return visitor.visit_return_stmt(self)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checker.py\", line 3156, in visit_return_stmt\r\n self.check_return_stmt(s)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checker.py\", line 3188, in check_return_stmt\r\n typ = get_proper_type(self.expr_checker.accept(\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checkexpr.py\", line 3768, in accept\r\n typ = node.accept(self)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/nodes.py\", line 1545, in accept\r\n return visitor.visit_call_expr(self)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checkexpr.py\", line 263, in visit_call_expr\r\n return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checkexpr.py\", line 340, in visit_call_expr_inner\r\n ret_type = self.check_call_expr_with_callee_type(callee_type, e, fullname,\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checkexpr.py\", line 817, in check_call_expr_with_callee_type\r\n return self.check_call(callee_type, e.args, e.arg_kinds, e,\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checkexpr.py\", line 876, in check_call\r\n return self.check_callable_call(callee, args, arg_kinds, context, arg_names,\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/checkexpr.py\", line 941, in check_callable_call\r\n if (callee.is_type_obj() and callee.type_object().is_abstract\r\n File \"/home/edo/miniconda3/envs/abm2/lib/python3.8/site-packages/mypy/types.py\", line 1094, in type_object\r\n assert isinstance(ret, Instance)\r\nAssertionError:\r\nmypytest.py:18: : note: use --pdb to drop into pdb\r\n```\r\n\r\nWhere `ret` is `Any` and `callee` in the frame above is `def (x: Any) -> Any`.\r\n\r\nThe error can be prevented either by assigning the lambda to a variable\r\n\r\n```\r\n identity = lambda x: x\r\n wrap = Wrapper if random.random() \u003c 0.5 else identity\r\n```\r\n\r\nwhere in this case no issues are reported, or the wrapper:\r\n\r\n```\r\nclass Wrapper:\r\n def __init__(self, x: int) -> None:\r\n self.x = x\r\n\r\ndef main() -> Union[Wrapper, int]:\r\n wrap = Wrapper if random.random() \u003c 0.5 else lambda x: x\r\n```\r\n\r\nbut in this case two errors are reported (line 12 is `wrap = ` and line 14 is `return`\r\n\r\n```\r\nmypytest.py:12: error: Incompatible return value type (got \"int\", expected \"Wrapper\")\r\nmypytest.py:14: error: Incompatible return value type (got \"object\", expected \"Union[Wrapper, int]\")\r\n```\r\n\r\nthese erros do not occur when both fixes are applied.\r\n\r\nexpected behavior: no errors reported in all three cases, or at least no crash.","author":{"url":"https://github.com/e-dorigatti","@type":"Person","name":"e-dorigatti"},"datePublished":"2020-07-31T14:16:33Z","url":"https://github.com/python/mypy/issues/9234"}
| 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:3c44da0d-6632-9c90-67aa-2d99a563e1f9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CE66:26654F:2E6A6D8:3D8F5B4:6B002A39 |
| html-safe-nonce | dd2a74d1b977e188caad14bd14aaf1921cee4c41f6c23dac54d2618702083bb6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRTY2OjI2NjU0RjoyRTZBNkQ4OjNEOEY1QjQ6NkIwMDJBMzkiLCJ2aXNpdG9yX2lkIjoiNzMwNjAzMjQ0Nzk3NDU1MDA3MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b1db4ebddb572eabb90b4eb895b6e607cd04e76a4c3699cbae68679a07dd1161 |
| hovercard-subject-tag | issue:669863637 |
| 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/9234/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6836bba49a48fe7799dab9493d48d38d84bcc94bc61458159c0b6bdff00b746f/python/mypy/issues/9234 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6836bba49a48fe7799dab9493d48d38d84bcc94bc61458159c0b6bdff00b746f/python/mypy/issues/9234 |
| og:image:alt | Consider the following example: import random from typing import Union, Callable class Wrapper: def __init__(self, x): self.x = x def main() -> Union[Wrapper, int]: wrap = Wrapper if random.random(... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | e-dorigatti |
| 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