Skip to content

Crash when type variable has same name as an external symbol with an erroneous definition in PEP 695 generic class #19526

Closed

Description

@brianschubert

Crash Report

This crashes on master:

T = NotDefined
class A[T]: ...

Does not reproduce when using a generic function (def f[T](): ...) or when T has some other non-type-variable value (T = 1)

Traceback

TESTFILES/SCRATCH.py:2: 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.18.0+dev.ad76e1628e2d4fe52d89a80d51890bf84afad38f
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/brian/Projects/open-contrib/mypy/mypy/__main__.py", line 37, in <module>
    console_entry()
  File "/home/brian/Projects/open-contrib/mypy/mypy/__main__.py", line 15, in console_entry
    main()
  File "/home/brian/Projects/open-contrib/mypy/mypy/main.py", line 127, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/home/brian/Projects/open-contrib/mypy/mypy/main.py", line 211, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/brian/Projects/open-contrib/mypy/mypy/build.py", line 191, in build
    result = _build(
  File "/home/brian/Projects/open-contrib/mypy/mypy/build.py", line 267, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/brian/Projects/open-contrib/mypy/mypy/build.py", line 2939, in dispatch
    process_graph(graph, manager)
  File "/home/brian/Projects/open-contrib/mypy/mypy/build.py", line 3337, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/brian/Projects/open-contrib/mypy/mypy/build.py", line 3432, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "/home/brian/Projects/open-contrib/mypy/mypy/semanal_main.py", line 95, in semantic_analysis_for_scc
    process_top_levels(graph, scc, patches)
  File "/home/brian/Projects/open-contrib/mypy/mypy/semanal_main.py", line 222, in process_top_levels
    deferred, incomplete, progress = semantic_analyze_target(
  File "/home/brian/Projects/open-contrib/mypy/mypy/semanal_main.py", line 399, in semantic_analyze_target
    analyzer.refresh_partial(
  File "/home/brian/Projects/open-contrib/mypy/mypy/semanal.py", line 652, in refresh_partial
    self.refresh_top_level(node)
  File "/home/brian/Projects/open-contrib/mypy/mypy/semanal.py", line 670, in refresh_top_level
    self.accept(d)
  File "/home/brian/Projects/open-contrib/mypy/mypy/semanal.py", line 7314, in accept
    node.accept(self)
    ~~~~~~~~~~~^^^^^^
  File "/home/brian/Projects/open-contrib/mypy/mypy/nodes.py", line 1249, in accept
    return visitor.visit_class_def(self)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/home/brian/Projects/open-contrib/mypy/mypy/semanal.py", line 1769, in visit_class_def
    self.analyze_class(defn)
    ~~~~~~~~~~~~~~~~~~^^^^^^
  File "/home/brian/Projects/open-contrib/mypy/mypy/semanal.py", line 1910, in analyze_class
    bases, tvar_defs, is_protocol = self.clean_up_bases_and_infer_type_variables(
                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        defn, bases, context=defn
        ^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/home/brian/Projects/open-contrib/mypy/mypy/semanal.py", line 2215, in clean_up_bases_and_infer_type_variables
    assert isinstance(node.node, TypeVarLikeExpr)
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 

Activity

  1. changed the title [-]Crash type variable has same name as an external symbol with an erroneous definition in PEP 695 generic class[/-] [+]Crash when type variable has same name as an external symbol with an erroneous definition in PEP 695 generic class[/+] on Jul 28, 2025
  2. sterliakov commented on Jul 29, 2025

    @sterliakov
    Collaborator

    mypy/mypy/semanal.py

    Lines 6779 to 6784 in ad76e16

    elif name not in self.missing_names[-1] and "*" not in self.missing_names[-1]:
    names[name] = symbol
    if not no_progress:
    self.progress = True
    return True
    return False

    So T is a missing name, and we refuse it everywhere.

    mypy/mypy/semanal.py

    Lines 389 to 399 in ad76e16

    # These names couldn't be added to the symbol table due to incomplete deps.
    # Note that missing names are per module, _not_ per namespace. This means that e.g.
    # a missing name at global scope will block adding same name at a class scope.
    # This should not affect correctness and is purely a performance issue,
    # since it can cause unnecessary deferrals. These are represented as
    # PlaceholderNodes in the symbol table. We use this to ensure that the first
    # definition takes precedence even if it's incomplete.
    #
    # Note that a star import adds a special name '*' to the set, this blocks
    # adding _any_ names in the current file.
    missing_names: list[set[str]]

    cc @ilevkivskyi - would it be ok to exclude type parameters from this restriction?

    diff --git a/mypy/semanal.py b/mypy/semanal.py
    index 1840e606a..aff5a7601 100644
    --- a/mypy/semanal.py
    +++ b/mypy/semanal.py
    @@ -6776,7 +6776,9 @@ class SemanticAnalyzer(
                         self.add_redefinition(names, name, symbol)
                     if not (isinstance(new, (FuncDef, Decorator)) and self.set_original_def(old, new)):
                         self.name_already_defined(name, context, existing)
    -        elif name not in self.missing_names[-1] and "*" not in self.missing_names[-1]:
    +        elif type_param or (
    +            name not in self.missing_names[-1] and "*" not in self.missing_names[-1]
    +        ):
                 names[name] = symbol
                 if not no_progress:
                     self.progress = True
  3. ilevkivskyi commented on Aug 3, 2025

    @ilevkivskyi
    Member

    I think @JukkaL may be a better person to ask, as IIRC he implemented most of PEP 695.

  4. added a commit that references this issue on Oct 20, 2025
    583c5f7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions