python / python/mypy

[Plugins] Access value of a StarExpr

Offen
#12,651 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

documentation topic-plugins
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Hi folks, I'm trying to add support for StarExpr in a plugin but I'm unable to figure out the value of the StarExpr, here's an example of the code I want to make a plugin for:

def fake_union(name: str, types: Tuple[Types, ...]) -> Union[Types]:
    ...

UserErrors = (A, B)

Response = fake_union(types=(User, *UserErrors), name="Response")

I don't know if this is possible to figure out using MyPy, so far I was only able to fetch the Var definition, but not the type

def union_hook(ctx: DynamicClassDefContext) -> None:
    types_expr = ctx.call.args[ctx.call.arg_names.index("types")]

    assert isinstance(types_expr, TupleExpr)

    for type_ in types_expr.items:
        if isinstance(type_, StarExpr):
            assert isinstance(type_.expr, NameExpr)
            name = type_.expr.name

            # how can I find the value of this expr?
            print(name)

I made a repo with a full example here: https://github.com/patrick91/mypy-star-plugin

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

Beginne mit dem Plugin-Beispielrepository und den im Issue gezeigten Einstiegspunkten DynamicClassDefContext, TupleExpr, StarExpr und NameExpr. Verfolge, wie das Plugin StarExpr auf UserErrors prüfen kann, und verifiziere anschließend, dass das Beispiel beim Verarbeiten des types-Arguments auf den expandierten Wert zugreifen kann.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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