python / python/mypy

Generic argument is not being identified

Offen
#18,933 7 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-error-reporting topic-pep-695
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

The code in this mypy playground raises the following error.

Name "T" is not defined  [name-defined]

However, as seen in this pyright playground there is no error, which leads me to believe that the snippet is well defined.

This one is probably related but the revealed types do not seem to match as well (the ? in the type name seems to be rather strange).

mypy

"__main__.Repo[T?, U`-2]"

pyright

"Repo[T@some, U@some]"

To Reproduce (this is the same code as in the mypy playground)

from typing import cast, reveal_type
from collections.abc import Callable

class Repo[T, U]: ...

def some[T, U, **P](func: Callable[P, U]) -> Callable[P, U]:
    def wrapper(*args: P.args, **kwargs: P.kwargs) -> U:
        reveal_type(cast(Repo[T, U], args[0]))
        return func(*args, **kwargs)
    return wrapper

Expected Behavior

No errors from mypy, similar behavior to pyright.

Actual Behavior

I am not sure if mypy is misdiagnosing this, if you feel this is intended behavior I can try raising an issue with pyright.

Your Environment

  • Mypy version used: 1.15.0 (playground)
  • Pyright version used: 1.1.399 (playground)
  • Mypy command-line flags: default settings in mypy playground (if I am not mistaken, there are no extra flags)
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.13 (for both playgrounds)

Thank You

Edit: Simplified the example a bit by removing the bounds and awaitables.

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 verlinkten mypy playground und der minimalen Reproduktion unter „To Reproduce“, und vergleiche deren Ergebnis mit dem verlinkten pyright playground. Verfolge, wie die generischen Parameter T und U innerhalb von wrapper und in cast(Repo[T, U], args[0]) aufgelöst werden; die Aufgabe ist abgeschlossen, wenn mypy T nicht mehr als undefiniert meldet und sein offengelegter Typ mit der erwarteten generischen Form übereinstimmt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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