mypy icon indicating copy to clipboard operation
mypy copied to clipboard

INTERNAL ERROR on match-case statement

Open randolf-scholz opened this issue 3 months ago • 1 comments

Bug Report

The following causes an INTERNAL ERROR. The issue seems to be related to the usage of match-case. https://mypy-play.net/?mypy=master&python=3.12&flags=show-traceback&gist=7ccec270743b3e26d66d211839e82e64

To Reproduce

from typing import Self, Protocol, overload
from collections.abc import Sequence

class Encoder(Protocol):
    def __call__(self, u, /): ...
    
    def simplify(self) -> "Encoder":
        return self

class Chain(Encoder, Sequence[Encoder]):
    
    encoders: list[Encoder]
    
    def __init__(self, *encoders: Encoder):
        self.encoders = list(encoders)
    
    def __len__(self) -> int:
        return len(self.encoders)

    @overload
    def __getitem__(self, index: int) -> Encoder: ...
    @overload
    def __getitem__(self, index: slice) -> Self: ...
    def __getitem__(self, index,  /):
        return self.encoders[index]
    
    def __call__(self, x):
        for encoder in self.encoders:
            x = encoder(x)
        return x
    
    def simplify(self) -> Encoder | Self:
        match self:
            case [encoder]:
                return encoder.simplify()
            case _:
                cls = type(self)
                return cls(*(e.simplify() for e in self.encoders))

        # The equivalent code below works...
        # if len(self)==1:
        #     return self.encoders[0].simplify()
        # cls = type(self)
        # return cls(*(e.simplify() for e in self.encoders))

Actual Behavior

main.py:33: 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: 1.11.0+dev.0570f71f000489c94021d956662ab3373f7296bc
main.py:33: : note: please use --show-traceback to print a traceback when reporting a bug
Full Traceback
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/__main__.py", line 37, in <module>
    console_entry()
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/main.py", line 100, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/main.py", line 182, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 192, in build
    result = _build(
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 266, in _build
    graph = dispatch(sources, manager, stdout)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 2942, in dispatch
    process_graph(graph, manager)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 3340, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 3441, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 2310, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 481, in check_first_pass
    self.accept(d)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 589, in accept
    stmt.accept(self)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/nodes.py", line 1142, in accept
    return visitor.visit_class_def(self)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 2340, in visit_class_def
    self.accept(defn.defs)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 589, in accept
    stmt.accept(self)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/nodes.py", line 1223, in accept
    return visitor.visit_block(self)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 2802, in visit_block
    self.accept(s)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 589, in accept
    stmt.accept(self)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/nodes.py", line 787, in accept
    return visitor.visit_func_def(self)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 1000, in visit_func_def
    self._visit_func_def(defn)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 1004, in _visit_func_def
    self.check_func_item(defn, name=defn.name)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 1077, in check_func_item
    self.check_func_def(defn, typ, name, allow_empty)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 1309, in check_func_def
    self.accept(item.body)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 589, in accept
    stmt.accept(self)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/nodes.py", line 1223, in accept
    return visitor.visit_block(self)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 2802, in visit_block
    self.accept(s)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 589, in accept
    stmt.accept(self)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/nodes.py", line 1607, in accept
    return visitor.visit_match_stmt(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 5107, in visit_match_stmt
    pattern_types = [self.pattern_checker.accept(p, subject_type) for p in s.patterns]
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checkpattern.py", line 129, in accept
    result = o.accept(self)
             ^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/patterns.py", line 93, in accept
    return visitor.visit_sequence_pattern(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checkpattern.py", line 335, in visit_sequence_pattern
    new_type = self.construct_sequence_child(current_type, new_inner_type)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checkpattern.py", line 756, in construct_sequence_child
    assert isinstance(proper_type, Instance)
AssertionError: 
main.py:33: 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: 1.11.0+dev.0570f71f000489c94021d956662ab3373f7296bc
main.py:33: : note: use --pdb to drop into pdb

Your Environment

https://mypy-play.net/?mypy=master&python=3.12&flags=show-traceback&gist=7ccec270743b3e26d66d211839e82e64

randolf-scholz avatar Apr 17 '24 15:04 randolf-scholz

Some of the traceback (from mypy-play):

  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 5107, in visit_match_stmt
    pattern_types = [self.pattern_checker.accept(p, subject_type) for p in s.patterns]
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checkpattern.py", line 129, in accept
    result = o.accept(self)
             ^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/patterns.py", line 93, in accept
    return visitor.visit_sequence_pattern(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checkpattern.py", line 335, in visit_sequence_pattern
    new_type = self.construct_sequence_child(current_type, new_inner_type)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checkpattern.py", line 756, in construct_sequence_child
    assert isinstance(proper_type, Instance)
AssertionError

Looks like a missed case in the match implementation.

JelleZijlstra avatar Apr 17 '24 16:04 JelleZijlstra