Skip to content

Crash on match statement in generic class with restricted type variable #12448

Description

@keosak

Crash Report

Mypy crashes on match statement in generic class with type variable that is restricted to specific types.

Traceback

(venv) debian@989791c09e5b:/workspace$ mypy t.py --show-traceback
t.py:8: 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.950+dev.aa7c170b4ad31bada4faea003f9f0cb99bb58b25
Traceback (most recent call last):
File "/opt/venv/bin/mypy", line 8, in <module>
sys.exit(console_entry())
File "/opt/venv/lib/python3.10/site-packages/mypy/__main__.py", line 12, in console_entry
main(None, sys.stdout, sys.stderr)
File "/opt/venv/lib/python3.10/site-packages/mypy/main.py", line 96, in main
res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
File "/opt/venv/lib/python3.10/site-packages/mypy/main.py", line 173, in run_build
res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
File "/opt/venv/lib/python3.10/site-packages/mypy/build.py", line 180, in build
result = _build(
File "/opt/venv/lib/python3.10/site-packages/mypy/build.py", line 256, in _build
graph = dispatch(sources, manager, stdout)
File "/opt/venv/lib/python3.10/site-packages/mypy/build.py", line 2733, in dispatch
process_graph(graph, manager)
File "/opt/venv/lib/python3.10/site-packages/mypy/build.py", line 3081, in process_graph
process_stale_scc(graph, scc, manager)
File "/opt/venv/lib/python3.10/site-packages/mypy/build.py", line 3179, in process_stale_scc
graph[id].type_check_first_pass()
File "/opt/venv/lib/python3.10/site-packages/mypy/build.py", line 2192, in type_check_first_pass
self.type_checker().check_first_pass()
File "/opt/venv/lib/python3.10/site-packages/mypy/checker.py", line 318, in check_first_pass
self.accept(d)
File "/opt/venv/lib/python3.10/site-packages/mypy/checker.py", line 424, in accept
stmt.accept(self)
File "/opt/venv/lib/python3.10/site-packages/mypy/nodes.py", line 1021, in accept
return visitor.visit_class_def(self)
File "/opt/venv/lib/python3.10/site-packages/mypy/checker.py", line 1798, in visit_class_def
self.accept(defn.defs)
File "/opt/venv/lib/python3.10/site-packages/mypy/checker.py", line 424, in accept
stmt.accept(self)
File "/opt/venv/lib/python3.10/site-packages/mypy/nodes.py", line 1090, in accept
return visitor.visit_block(self)
File "/opt/venv/lib/python3.10/site-packages/mypy/checker.py", line 2162, in visit_block
self.accept(s)
File "/opt/venv/lib/python3.10/site-packages/mypy/checker.py", line 424, in accept
stmt.accept(self)
File "/opt/venv/lib/python3.10/site-packages/mypy/nodes.py", line 740, in accept
return visitor.visit_func_def(self)
File "/opt/venv/lib/python3.10/site-packages/mypy/checker.py", line 775, in visit_func_def
self._visit_func_def(defn)
File "/opt/venv/lib/python3.10/site-packages/mypy/checker.py", line 779, in _visit_func_def
self.check_func_item(defn, name=defn.name)
File "/opt/venv/lib/python3.10/site-packages/mypy/checker.py", line 841, in check_func_item
self.check_func_def(defn, typ, name)
File "/opt/venv/lib/python3.10/site-packages/mypy/checker.py", line 861, in check_func_def
expanded = self.expand_typevars(defn, typ)
File "/opt/venv/lib/python3.10/site-packages/mypy/checker.py", line 1470, in expand_typevars
result.append((expand_func(defn, mapping), expanded))
File "/opt/venv/lib/python3.10/site-packages/mypy/checker.py", line 6021, in expand_func
ret = defn.accept(visitor)
File "/opt/venv/lib/python3.10/site-packages/mypy/nodes.py", line 740, in accept
return visitor.visit_func_def(self)
File "/opt/venv/lib/python3.10/site-packages/mypy/treetransform.py", line 115, in visit_func_def
self.block(node.body),
File "/opt/venv/lib/python3.10/site-packages/mypy/treetransform.py", line 572, in block
new = self.visit_block(block)
File "/opt/venv/lib/python3.10/site-packages/mypy/treetransform.py", line 194, in visit_block
return Block(self.statements(node.body))
File "/opt/venv/lib/python3.10/site-packages/mypy/treetransform.py", line 583, in statements
return [self.stmt(stmt) for stmt in statements]
File "/opt/venv/lib/python3.10/site-packages/mypy/treetransform.py", line 583, in <listcomp>
return [self.stmt(stmt) for stmt in statements]
File "/opt/venv/lib/python3.10/site-packages/mypy/treetransform.py", line 557, in stmt
assert isinstance(new, Statement)
AssertionError: t.py:8: : note: use --pdb to drop into pdb

To Reproduce

fromtypingimportGeneric, TypeVarT=TypeVar("T", int, str)
classC(Generic[T]):
deff(self, x: str):
matchx:
case _:
pass

When the type variable T is not restricted, Mypy works as expected.

Your Environment

  • Mypy version used: 0.950+dev.aa7c170b4ad31bada4faea003f9f0cb99bb58b25 (compiled: no)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10.2
  • Operating system and version: Official Python Docker container

Notes

I investigated the problem in PDB a little bit (see below). It looks like right before the crash, the visit_match_stmt() method of TypeTransformVisitor class is called by stmt.accept(self). However, the method is not implemented there, so the default implementation in the NodeVisitor base class is executed instead. This method returns None, so the assert fails.

(venv) debian@989791c09e5b:/workspace$ mypy t.py --pdb
t.py:8: error: INTERNAL ERROR -- Please try using mypy master on Github:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
If this issue continues with mypy master, please report a bug at https://github.com/python/mypy/issues
version: 0.950+dev.aa7c170b4ad31bada4faea003f9f0cb99bb58b25
Dropping into pdb
> /opt/venv/lib/python3.10/site-packages/mypy/treetransform.py(557)stmt()
-> assert isinstance(new, Statement)
(Pdb) list
552 new.set_line(expr.line, expr.column)
553 return new
554 555 def stmt(self, stmt: Statement) -> Statement:
556 new = stmt.accept(self)
557 -> assert isinstance(new, Statement)
558 new.set_line(stmt.line, stmt.column)
559 return new
560 561 # Helpers
562 #
(Pdb) p self.visit_match_stmt
<bound method NodeVisitor.visit_match_stmt of <mypy.checker.TypeTransformVisitor object at 0x7f6e6482e470>>
(Pdb) p self.visit_match_stmt.__code__
<code object visit_match_stmt at 0x7f6e665889d0, file "/opt/venv/lib/python3.10/site-packages/mypy/visitor.py", line 473>

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions