python / python/mypy

Extending `TraverserVisitor` causes `TypeError: interpreted classes cannot inherit from compiled traits`

Open
#19,456 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Plugin code cannot extend any of the Visitor classes

To Reproduce

from mypy.nodes import IndexExpr, MypyFile
from mypy.plugin import Plugin
from mypy.traverser import TraverserVisitor


def plugin(_: str) -> type[Plugin]:
    return ReproducerPlugin


class ReproducerPlugin(Plugin):
    def get_additional_deps(self, file: MypyFile) -> list[tuple[int, str, int]]:
        class IndexVisitor(TraverserVisitor):
            def visit_index_expr(self, ie: IndexExpr, /) -> None:
                print(ie)

        file.accept(IndexVisitor())

Expected Behavior
It not crash

Actual Behavior

[ERROR   ] An un-handled exception was caught by Salt's global exception handler:
TypeError: interpreted classes cannot inherit from compiled traits
Traceback (most recent call last):
  File "venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
             ~~~~~~~~~~~~~^^
  File "venv/lib/python3.13/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
    ~~~~^^
  File "mypy/main.py", line 127, in main
  File "mypy/main.py", line 211, in run_build
  File "mypy/build.py", line 191, in build
  File "mypy/build.py", line 267, in _build
  File "mypy/build.py", line 2886, in dispatch
  File "mypy/build.py", line 3072, in load_graph
  File "mypy/build.py", line 1994, in __init__
  File "mypy/build.py", line 2292, in compute_dependencies
  File "mypy/plugin.py", line 845, in get_additional_deps
  File "plugin/__init__.py", line 16, in get_additional_deps
    file.accept(IndexVisitor())
                ~~~~~~~~~~~~^^
TypeError: interpreted classes cannot inherit from compiled traits

Your Environment

LOG:  Mypy Version:           1.17.0
LOG:  Config File:            pyproject.toml
LOG:  Configured Executable:  venv/bin/python3.13
LOG:  Current Executable:     venv/bin/python3.13
LOG:  Cache Dir:              .mypy_cache
LOG:  Compiled:               True
LOG:  Exclude:                []
LOG:  Found source:           BuildSource(path='test.py', module='test', has_text=False, base_dir='.', followed=False)
LOG:  Metadata abandoned for test: options differ
LOG:  Metadata not found for test
LOG:  Parsing test.py (test)
LOG:  Build finished in 0.013 seconds with 1 modules, and 0 errors

from pyproject.toml:

[tool.mypy]
plugins = ["pydantic.mypy", "plugin"]
strict = true
namespace_packages = true
mypy_path = ".mypy-stubs/"
scripts_are_modules = true
  • Python version used: 3.13

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Python 3.13 reproducer with mypy compiled, then inspect TraverserVisitor in mypy/traverser.py and the plugin-loading path shown in mypy/plugin.py. Trace why an interpreted subclass fails to initialize and verify the reproducer completes without the TypeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.