python / python/mypy

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

Offen
#17,514 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Führe zunächst den Reproducer plugin.py und module.py mit den gemeldeten Python- und mypy-Versionen aus und verfolge dann die Typanalyse für Annotated bei direkter Verwendung, für TypeAlias und für die Spezialisierung von Generics. Erledigt ist die Aufgabe, wenn der Hook in allen drei Fällen aufgerufen wird und jedes reveal_type wie von _type_analyze angefordert None meldet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.