python / python/mypy

Multiple extensions using `get_base_class_hook` interfere with each other

Aperta
#19,524 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-plugins
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Merge medio
1g 18h
PR unite (30g)
54

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo il comportamento con mypy_plugin1.py e mypy_plugin2.py, configurati tramite mypy.ini, ed esegui poetry run mypy. Leggi la documentazione di get_base_class_hook e segui come vengono interrogati i plugin configurati; è completato quando vengono invocati i callback di entrambi i plugin per le stesse definizioni di classe.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.