mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Crash in match statement with literals

Open Tinche opened this issue 3 years ago • 2 comments

Crash Report

Mypy crashes when run over a match statement.

Traceback

a05.py:10: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 0.990+dev.7c14feedd2a6889d9eab8b0ac8dc8aab630bbed3
Traceback (most recent call last):
  File "/Users/tintvrtkovic/hr/shop-service/.venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/Users/tintvrtkovic/hr/shop-service/.venv/lib/python3.10/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/Users/tintvrtkovic/hr/shop-service/.venv/lib/python3.10/site-packages/mypy/main.py", line 95, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/Users/tintvrtkovic/hr/shop-service/.venv/lib/python3.10/site-packages/mypy/main.py", line 174, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/Users/tintvrtkovic/hr/shop-service/.venv/lib/python3.10/site-packages/mypy/build.py", line 188, in build
    result = _build(
  File "/Users/tintvrtkovic/hr/shop-service/.venv/lib/python3.10/site-packages/mypy/build.py", line 271, in _build
    graph = dispatch(sources, manager, stdout)
  File "/Users/tintvrtkovic/hr/shop-service/.venv/lib/python3.10/site-packages/mypy/build.py", line 2885, in dispatch
    process_graph(graph, manager)
  File "/Users/tintvrtkovic/hr/shop-service/.venv/lib/python3.10/site-packages/mypy/build.py", line 3269, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/Users/tintvrtkovic/hr/shop-service/.venv/lib/python3.10/site-packages/mypy/build.py", line 3370, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/Users/tintvrtkovic/hr/shop-service/.venv/lib/python3.10/site-packages/mypy/build.py", line 2302, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/Users/tintvrtkovic/hr/shop-service/.venv/lib/python3.10/site-packages/mypy/checker.py", line 462, in check_first_pass
    self.accept(d)
  File "/Users/tintvrtkovic/hr/shop-service/.venv/lib/python3.10/site-packages/mypy/checker.py", line 570, in accept
    stmt.accept(self)
  File "/Users/tintvrtkovic/hr/shop-service/.venv/lib/python3.10/site-packages/mypy/nodes.py", line 1559, in accept
    return visitor.visit_match_stmt(self)
  File "/Users/tintvrtkovic/hr/shop-service/.venv/lib/python3.10/site-packages/mypy/checker.py", line 4551, in visit_match_stmt
    inferred_types = self.infer_variable_types_from_type_maps(type_maps)
  File "/Users/tintvrtkovic/hr/shop-service/.venv/lib/python3.10/site-packages/mypy/checker.py", line 4600, in infer_variable_types_from_type_maps
    assert isinstance(node, Var)
AssertionError:
a05.py:10: : note: use --pdb to drop into pdb

To Reproduce

A few code snippets for context (imports omitted for clarity).

This works:

x: Literal["test"]

match x:
    case "test":
        pass
    case _:
        assert_never(x)

This doesn't type-check (but should), but no crash:

x: tuple[Literal["test"]]


match x:
    case ("test",):
        pass
    case _:
        assert_never(x)

And finally, the crash:

def x() -> tuple[Literal["test"]]:
    ...


match x():
    case (x,) if x == "test":
        pass
    case _:
        assert_never(1)

Your Environment

  • Mypy version used: mypy 0.990+dev.7c14feedd2a6889d9eab8b0ac8dc8aab630bbed3 (compiled: no)
  • Mypy command-line flags: --show-traceback
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.10.5
  • Operating system and version: macOs

Tinche avatar Sep 14 '22 17:09 Tinche

I will try to fix a crash first, then fix the second issue.

sobolevn avatar Sep 15 '22 08:09 sobolevn

The problem is that several places assert that we only work with Var, but in your crashing example we work with FuncDef.

That's why it crashes.

sobolevn avatar Sep 15 '22 08:09 sobolevn