Skip to content

Crash in match statement with literals #13666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Tinche opened this issue Sep 14, 2022 · 3 comments
Closed

Crash in match statement with literals #13666

Tinche opened this issue Sep 14, 2022 · 3 comments
Assignees
Labels

Comments

@Tinche
Copy link
Contributor

Tinche commented Sep 14, 2022

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 Tinche added the crash label Sep 14, 2022
@sobolevn sobolevn self-assigned this Sep 15, 2022
@sobolevn
Copy link
Member

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

@sobolevn
Copy link
Member

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.

hauntsaninja pushed a commit that referenced this issue Jan 13, 2025
)

Fixes #16793. Fixes crash in #13666.

Previously mypy considered that variables in match/case patterns must be
Var's, causing a hard crash when a name of captured pattern clashes with
a name of some existing function. This PR removes such assumption about
Var and allows other nodes.
@sterliakov
Copy link
Collaborator

The crash is gone now (#18449), and the tuple reachability problem is reported separately in #18792.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants