Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash on sequence match statement in generic class with restricted type variable bound to sequence #18089

Open
rwhender opened this issue Nov 1, 2024 · 0 comments · May be fixed by #18091
Open
Labels

Comments

@rwhender
Copy link

rwhender commented Nov 1, 2024

Crash Report

mypy crashes when the following conditions are met:

  1. A generic class is present.
  2. The generic class refers to a typevar T that is bound to a sequence.
  3. The generic class has a method that attempts to pattern match a method parameter of type T
  4. One of the pattern matching cases is a sequence pattern such as _, _.

This crash is similar to but distinct from this issue: #12448

Traceback

$ mypy mypy_mre.py --show-traceback
mypy_mre.py:7: 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.14.0+dev.05a9e79068a5830e57264390c9f6bca859e92053
Traceback (most recent call last):
  File "~/venv-3.11.6/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/main.py", line 109, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/main.py", line 193, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/build.py", line 194, in build
    result = _build(
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/build.py", line 269, in _build
    graph = dispatch(sources, manager, stdout)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/build.py", line 2937, in dispatch
    process_graph(graph, manager)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/build.py", line 3335, in process_graph
    process_stale_scc(graph, scc, manager)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/build.py", line 3436, in process_stale_scc
    graph[id].type_check_first_pass()
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/build.py", line 2306, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checker.py", line 483, in check_first_pass
    self.accept(d)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checker.py", line 592, in accept
    stmt.accept(self)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/nodes.py", line 1196, in accept
    return visitor.visit_class_def(self)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checker.py", line 2461, in visit_class_def
    self.accept(defn.defs)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checker.py", line 592, in accept
    stmt.accept(self)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/nodes.py", line 1277, in accept
    return visitor.visit_block(self)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checker.py", line 2951, in visit_block
    self.accept(s)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checker.py", line 592, in accept
    stmt.accept(self)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/nodes.py", line 827, in accept
    return visitor.visit_func_def(self)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checker.py", line 1046, in visit_func_def
    self._visit_func_def(defn)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checker.py", line 1050, in _visit_func_def
    self.check_func_item(defn, name=defn.name)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checker.py", line 1084, in check_func_item
    self.check_func_def(defn, typ, name, allow_empty)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checker.py", line 1360, in check_func_def
    self.accept(item.body)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checker.py", line 592, in accept
    stmt.accept(self)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/nodes.py", line 1277, in accept
    return visitor.visit_block(self)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checker.py", line 2951, in visit_block
    self.accept(s)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checker.py", line 592, in accept
    stmt.accept(self)
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/nodes.py", line 1661, in accept
    return visitor.visit_match_stmt(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checker.py", line 5298, in visit_match_stmt
    pattern_types = [self.pattern_checker.accept(p, subject_type) for p in s.patterns]
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checker.py", line 5298, in <listcomp>
    pattern_types = [self.pattern_checker.accept(p, subject_type) for p in s.patterns]
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checkpattern.py", line 129, in accept
    result = o.accept(self)
             ^^^^^^^^^^^^^^
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/patterns.py", line 93, in accept
    return visitor.visit_sequence_pattern(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checkpattern.py", line 345, in visit_sequence_pattern
    new_type = self.construct_sequence_child(current_type, new_inner_type)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/venv-3.11.6/lib/python3.11/site-packages/mypy/checkpattern.py", line 766, in construct_sequence_child
    assert isinstance(proper_type, Instance)
AssertionError: 
mypy_mre.py:7: : note: use --pdb to drop into pdb

To Reproduce

Here is an MRE:

from typing import Generic, TypeVar, Sequence, Any

T = TypeVar("T", bound=Sequence[Any])

class C(Generic[T]):
    def f(self, x: T) -> None:
        match x:
            case _, _:
                pass
            case _:
                pass

Your Environment

  • Mypy version used: 1.14.0+dev.05a9e79068a5830e57264390c9f6bca859e92053 (current master)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files):
check_untyped_defs = true
disallow_any_generics = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
extra_checks = true
no_implicit_reexport = true
python_version = "3.11"
strict_equality = true
warn_redundant_casts = true
warn_return_any = true
warn_unused_configs = true
warn_unused_ignores = true
  • Python version used: 3.11.6
  • Operating system and version: AlmaLinux 9.4 under WSL 2 on Windows 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants