Skip to content

Crash with Self + Generic + dataclass #14199

@sterliakov

Description

@sterliakov

Crash Report
Self within Generic dataclass field crashes on master.

Traceback

Copied from local run, only the version differs in playground.

g.py:16: 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.0.0+dev.a9c62c5f82f34a923b8117a5394983aefce37b63
g.py:16: : note: please use --show-traceback to print a traceback when reporting a bug
(venv) stas@pc:/tmp/h$ mypy g.py --show-traceback
g.py:16: 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.0.0+dev.a9c62c5f82f34a923b8117a5394983aefce37b63
Traceback (most recent call last):
  File "/tmp/h/venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/main.py", line 95, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/main.py", line 174, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/build.py", line 194, in build
    result = _build(
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/build.py", line 277, in _build
    graph = dispatch(sources, manager, stdout)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/build.py", line 2914, in dispatch
    process_graph(graph, manager)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/build.py", line 3311, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/build.py", line 3406, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/semanal_main.py", line 84, in semantic_analysis_for_scc
    process_top_levels(graph, scc, patches)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/semanal_main.py", line 211, in process_top_levels
    deferred, incomplete, progress = semantic_analyze_target(
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/semanal_main.py", line 339, in semantic_analyze_target
    analyzer.refresh_partial(
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/semanal.py", line 599, in refresh_partial
    self.refresh_top_level(node)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/semanal.py", line 610, in refresh_top_level
    self.accept(d)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/semanal.py", line 6179, in accept
    node.accept(self)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/nodes.py", line 1121, in accept
    return visitor.visit_class_def(self)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/semanal.py", line 1530, in visit_class_def
    self.analyze_class(defn)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/semanal.py", line 1606, in analyze_class
    self.analyze_class_body_common(defn)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/semanal.py", line 1632, in analyze_class_body_common
    defn.defs.accept(self)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/nodes.py", line 1202, in accept
    return visitor.visit_block(self)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/semanal.py", line 4396, in visit_block
    self.accept(s)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/semanal.py", line 6179, in accept
    node.accept(self)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/nodes.py", line 1289, in accept
    return visitor.visit_assignment_stmt(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/semanal.py", line 2701, in visit_assignment_stmt
    self.process_type_annotation(s)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/semanal.py", line 3212, in process_type_annotation
    self.defer(s)
  File "/tmp/h/venv/lib/python3.11/site-packages/mypy/semanal.py", line 5856, in defer
    assert not self.final_iteration, "Must not defer during final iteration"
AssertionError: Must not defer during final iteration
g.py:16: : note: use --pdb to drop into pdb

To Reproduce

Running on this example results in internal error (Try me!):

from typing import Generic, Sequence, TypeVar
from typing_extensions import Self
from dataclasses import dataclass


_T = TypeVar('_T', bound='Foo')


@dataclass
class Foo:
    foo: int


@dataclass
class Element(Generic[_T]):
    elements: Sequence[Self]

Your Environment

  • Mypy version used: master (sandbox and locally; a9c62c5f82f34a923b8117a5394983aefce37b63)
  • Mypy command-line flags: --show-traceback
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.10, 3.11
  • Operating system and version: Ubuntu 22.04

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions