python / python/mypy

False positive with sequence subclass in match-case statement

Aperta
#17,134 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug false-positive topic-match-statement
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci il fallimento usando l'esempio mypy-play collegato e il traceback. Inizia in checkpattern.py, da construct_sequence_child, quindi leggi la gestione circostante di match in checker.py e la definizione del pattern di sequenza in patterns.py. Il lavoro è completato quando questo esempio non genera più un INTERNAL ERROR e produce il normale output del controllo dei tipi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.