python / python/mypy

Multiple extensions using `get_base_class_hook` interfere with each other

Open
#19,524 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

The first plugin that uses get_base_class_hook and returns a callback appears to take exclusive ownership of that hook, preventing other plugins from intercepting the same class definitions.

I specifically noticed this with the mypy_zope plugin which uses get_base_class_hook to analyze every class. And then writing my own custom mypy plugin where I also want to use get_base_class_hook but it wasn't getting called at all.

To Reproduce

  1. Setup two plugins using get_base_class_hook. To notice the effect, they should return a callback instead of None.
    mypy_plugin1.py
    from typing import Callable, Optional, Type
    
    from mypy.plugin import (
        ClassDefContext,
        Plugin,
    )
    
    
    class Plugin1(Plugin):
        def get_base_class_hook(
            self, fullname: str
        ) -> Optional[Callable[[ClassDefContext], None]]:
            def _analyze_class(classdef_ctx: ClassDefContext) -> None:
                print("plugin1 get_base_class_hook", fullname)
    
            return _analyze_class
    
    
    def plugin(version: str) -> Type[Plugin1]:
        return Plugin1
    
    mypy_plugin2.py
    from typing import Callable, Optional, Type
    
    from mypy.plugin import (
        ClassDefContext,
        Plugin,
    )
    
    
    class Plugin2(Plugin):
        def get_base_class_hook(
            self, fullname: str
        ) -> Optional[Callable[[ClassDefContext], None]]:
            def _analyze_class(classdef_ctx: ClassDefContext) -> None:
                print("plugin2 get_base_class_hook", fullname)
    
            return _analyze_class
    
    
    def plugin(version: str) -> Type[Plugin2]:
        return Plugin2
    
  2. Configure mypy to use those plugins. Edit mypy.ini and add plugins = scripts-dev/mypy_plugin1.py, scripts-dev/mypy_plugin2.py
  3. Lint some files, poetry run mypy
  4. Notice that there is only logs from plugin1

Expected Behavior

mypy calls get_base_class_hook in every plugin (logs for plugin1 and plugin2 show up)

Actual Behavior

plugin1 blocks other plugins from hooking into get_base_class_hook (only logs from plugin1 show up).

Your Environment

  • Mypy version used: v1.16.1
  • Mypy command-line flags: (none)
  • Mypy configuration options from mypy.ini (and other config files): plugins = scripts-dev/mypy_plugin1.py, scripts-dev/mypy_plugin2.py (real-life use case)
  • Python version used: 3.13.3

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 reproducing the behavior with mypy_plugin1.py and mypy_plugin2.py, configured through mypy.ini, and run poetry run mypy. Read the get_base_class_hook documentation and trace how the configured plugins are queried; done means both plugins' callbacks are invoked for the same class definitions.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.