python / python/mypy

Plugin's get_type_analyze_hook is not used for Annotated type in type alias or generic specialization

Aperta
#17,514 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

Mypy doesn't use plugin's get_type_analyze_hook for Annotated type used in type alias or generic specialization. However, the hook is used for custom types (not Annotated) used in these contexts, and it's also called for Annotated used in other contexts.

To Reproduce

plugin.py

from typing import Callable

from mypy.plugin import AnalyzeTypeContext, Plugin
from mypy.types import NoneType, Type

def plugin(version: str):
    return MyPlugin

class MyPlugin(Plugin):
    def get_type_analyze_hook(self, fullname: str) -> Callable[[AnalyzeTypeContext], Type] | None:
        if fullname in {'typing.Annotated', 'typing_extensions.Annotated'}:
            return _type_analyze
        return None

def _type_analyze(ctx: AnalyzeTypeContext) -> Type:
    ctx.api.note('Analyzing type of Annotated[...]', ctx.context)
    return NoneType()  # for the sake of example

module.py

from typing import Annotated, Generic, TypeAlias, TypeVar, reveal_type

AnnotatedTA: TypeAlias = Annotated[dict, None]
typed_with_alias: AnnotatedTA
reveal_type(typed_with_alias)  # Revealed type is "builtins.dict[Any, Any]"

typed_directly: Annotated[dict, None]  # Analyzing type of Annotated[...]
reveal_type(typed_directly)  # Revealed type is "None", as expected


T = TypeVar('T')

class GenericType(Generic[T]):
    dummy: T

class Specialization(GenericType[Annotated[dict, None]]):
    ...

reveal_type(Specialization().dummy)  # Revealed type is "builtins.dict[Any, Any]"

Expected Behavior

All revealed types are None as requested by _type_analyze hook

Actual Behavior

Only directly typed variable typed_directly has the type requested by plugin

Your Environment

  • Mypy version used: 1.10.1
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): plugins = plugin.py
  • Python version used: 3.8, 3.12

Background

I'm writing a plugin which generates TypedDictType for Annotated[dict, ...] annotations based on some external configuration files. The arguments to Annotated are string literals, so I can't really use generics. In runtime these are just normal dicts.

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 eseguendo il reproducer plugin.py e module.py con le versioni di Python e mypy indicate, quindi traccia l’analisi dei tipi per Annotated nell’uso diretto, per TypeAlias e per la specializzazione generica. Il lavoro è completato quando l’hook viene invocato in tutti e tre i casi e ogni reveal_type riporta None come richiesto da _type_analyze.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.