Skip to content

Mypy crashes on abstract class with generic type parameter #19946

Closed

Description

@bmmcq

Crash Report

crash at the first line of :

class JobWeaver[T](abc.ABC):
    @abc.abstractmethod
    def data_type(self) -> type[T] | None: 
        ...
    @abc.abstractmethod
    def map[OT](self, args) -> "JobWeaver[OT]"
        ...

It only defines a totally abstract class with a generic type parameter.

Traceback

src/nagas_api/weaver.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.18.1
Traceback (most recent call last):
  File "mypy/semanal.py", line 7350, in accept
  File "mypy/nodes.py", line 1440, in accept
  File "mypy/semanal.py", line 1775, in visit_class_def
  File "mypy/semanal.py", line 1916, in analyze_class
  File "mypy/semanal.py", line 2222, in clean_up_bases_and_infer_type_variables
AssertionError: 
src/nagas_api/weaver.py:7: : note: use --pdb to drop into pdb

Your Environment

  • Mypy version used: 1.18.1

  • Mypy command-line flags:
    uv run --active --group test mypy ./src --show-traceback --config-file ../pyproject.toml

  • Mypy configuration options from mypy.ini (and other config files):

[tool.mypy]
ignore_missing_imports = true
check_untyped_defs = true
follow_imports = "silent"
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = true
exclude = [
    '^test\.',
    'tests/'
]
  • Python version used: 3.12

Activity

  1. sterliakov commented on Sep 28, 2025

    @sterliakov
    Collaborator

    Okay, but this doesn't seem to cause a crash? Neither your original snippet (SyntaxError, normally reported) nor the fixed one (with the missing semicolon restored and import abc added). Could you try to provide a working repro case?

    https://mypy-play.net/?mypy=1.18.1&python=3.13&flags=ignore-missing-imports%2Cdisallow-untyped-defs%2Cwarn-return-any&gist=e85b22f47263da6fff29585120eed653

  2. bmmcq commented on Sep 29, 2025

    @bmmcq
    Author

    During my attempts to reproduce this issue, I identified that the crash was caused by an import statement. When a "xxx" module uses __all__ statement to define the contents that can be included by import *, the issue is triggered when using from xxx import *. However, Mypy runs normally when importing explicitly via from xxx import (a, b, c, ...). While the exact root cause of the crash remains unclear, I have already identified a viable solution.

  3. sterliakov commented on Sep 29, 2025

    @sterliakov
    Collaborator

    Oh, the star import probably brings another T into your namespace, then duplicate of #19526 or #18507. Could you please confirm that that your xxx module defines a symbol named T (either a type variable or something similar; may be imported transitively)?

  4. bmmcq commented on Sep 29, 2025

    @bmmcq
    Author

    Ok.

    In the first Python file(fileA), several classes with type parameters are defined, and all classes defined in this file are included in the __all__ list :

    # some imports
    import xxx
    
    class OperatorA[I, O](abc.ABC):
        ...
    
    class OperatorB[T](abc.ABC):
        ...
    
    __all__ = ["OperatorA", "OperatorB"]

    Since we use Python 3.12's generic syntax, no symbols or type variables are declared using TypeVar.

    The second Python file imports the first file,if it is imported in this way:

    import abc
    from .fileA import *
    
    class JobWeaver[T](abc.ABC): # crash at here
        @abc.abstractmethod
        def data_type(self) -> type[T] | None: 
            ...
        @abc.abstractmethod
        def map[OT](self, args) -> "JobWeaver[OT]"
            ...

    mypy crashed as described above, but if import fileA like this:

    import abc
    from .fileA import (OperatorA, OperatorB)
    
    class JobWeaver[T](abc.ABC): # crash at this line
        @abc.abstractmethod
        def data_type(self) -> type[T] | None: 
            ...
        @abc.abstractmethod
        def map[OT](self, args) -> "JobWeaver[OT]"
            ...

    mypy works well.

    I think the second way also import some classes with type parameter "T" , but it works.

  5. bmmcq commented on Sep 29, 2025

    @bmmcq
    Author

    And even if I change the generic 'T' to another name, like 'Z':

    import abc
    from .fileA import *
    
    class JobWeaver[Z](abc.ABC): # crash at here
        @abc.abstractmethod
        def data_type(self) -> type[Z] | None: 
            ...
        @abc.abstractmethod
        def map[OT](self, args) -> "JobWeaver[OT]"
            ...

    it still crashed at same place.

  6. sterliakov commented on Sep 29, 2025

    @sterliakov
    Collaborator

    Hm, this sounds more concerning, but I still can't reproduce.

    $ tree
    tree
    .
    ├── foo                                                                                                                                                                                                           
    │   ├── a.py                                                                                                                                                                                                      
    │   ├── b.py                                                                                                                                                                                                      
    │   └── __init__.py                                                                                                                                                                                               
    └── pyproject.toml                                                                                                                                                                                                
                                                                                                                                                                                                                      
    2 directories, 4 files 
    $ cat pyproject.toml
    [tool.mypy]
    ignore_missing_imports = true
    check_untyped_defs = true
    follow_imports = "silent"
    warn_return_any = true
    warn_unused_configs = true
    disallow_untyped_defs = true
    exclude = [
        '^test\.',
        'tests/'
    ]
    $ cat foo/a.py
    # some imports
    import xxx
    import abc
    
    class OperatorA[I, O](abc.ABC):
        ...
    
    class OperatorB[T](abc.ABC):
        ...
    
    __all__ = ["OperatorA", "OperatorB"]
    $ cat foo/b.py
    import abc
    from .a import *
    
    class JobWeaver[T](abc.ABC): # crash at here
        @abc.abstractmethod
        def data_type(self) -> type[T] | None: 
            ...
        @abc.abstractmethod
        def map[OT](self, args) -> "JobWeaver[OT]":
            ...
    $ mypy foo --config-file pyproject.toml
    foo/b.py:9: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
    Found 1 error in 1 file (checked 3 source files)
    
  7. 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

    Labels

    crashpendingIssues that may be closedtopic-pep-695Issues related to PEP 695 syntax

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions