When using `isinstance` to infer a generic type in a block, use any covariant bounds on the type variable.

Offen
#1,998 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
35/100
Issue-Typ
Feature
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
python
Bereich
compilers, devtools

Rechercherichtung

Start with the isinstance-based generic inference example in the issue and reproduce its reveal_type result for AppleAction. Done means covariant, bounded type variables are reflected in the inferred type rather than producing AppleAction[Any], with the behavior validated for the shown hierarchy.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

bug priority-1-normal topic-type-variables

I have a class hierarchy like the following:

class Apple(object): ...

T_co = TypeVar('T_co', covariant=True)
A_co = TypeVar('A_co', bound=Apple, covariant=True)

class Action(Generic[T_co]): ...

class AppleAction(Generic[A_co], Action[A_co]): ...

Later in my code, I have a block like so:

if isinstance(action, AppleAction):
   reveal_type(action)

And I get AppleAction[Any]. Because A_co is covariant, and bounded by Apple, even if A_co is bound to a subtype of Apple, the resulting object is going to be a valid instance of AppleAction[Apple]. I'd like to have action be AppleAction[Apple] in this case.

Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
Ø Merge
1 T. 18 Std.
Gemergte PRs (30 T.)
54

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python/mypy

Alle Issues in python/mypy

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

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