Skip to content

False warnings and crash with dynamic class in match statement #12886

@ljodal

Description

@ljodal

Crash Report

Using a dynamically created class in a match statement produces a warning about it not being a type. If the match also has *rest clause mypy crashes.

Traceback

reproducer.py:15: error: Expected type in class pattern; found "Type[reproducer.Example]"
reproducer.py:14: 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.960
Traceback (most recent call last):
  File "mypy/checker.py", line 431, in accept
  File "mypy/nodes.py", line 1415, in accept
  File "mypy/checker.py", line 4155, in visit_match_stmt
  File "mypy/checkpattern.py", line 104, in accept
  File "mypy/patterns.py", line 87, in accept
  File "mypy/checkpattern.py", line 261, in visit_sequence_pattern
  File "mypy/checkpattern.py", line 366, in expand_starred_pattern_types
IndexError: list index out of range
reproducer.py:14: : note: use --pdb to drop into pdb

To Reproduce

The following self-contained example demonstrates both the false warning and the crash. At runtime it works fine and prints SubClass with value: my value

# reproducer.py
class Example:
    __match_args__ = ("_value",)

    def __init__(self, value: str) -> None:
        self._value = value

def example(name: str) -> type[Example]:
    return type(name, (Example, ), {})

SubClass = example("SubClass")

t = [SubClass("my value"), SubClass("Other value")]

match t:
    case [SubClass(value), *rest]:
        print(f"SubClass with value: {value}")

Your Environment

  • Mypy version used: 0.960
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10.4
  • Operating system and version: macOS 12.3.1

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions