python / python/mypy

False positive with sequence subclass in match-case statement

Open
#17,134 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug false-positive topic-match-statement
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure using the linked mypy-play example and traceback. Start in checkpattern.py at construct_sequence_child, then read the surrounding match handling in checker.py and the sequence pattern definition in patterns.py. Done means this example no longer raises an INTERNAL ERROR and produces normal type-checking output.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.