python / python/mypy

Referring to class attributes of a generic type

Aperta
#7,121 6 commenti 12 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature priority-2-low topic-type-variables
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

I apologize for posting my question here as an issue; I am doing this out of desperation since my original post on StackOverflow went unanswered for three weeks. Below is a near-verbatim copy from StackOverflow.

I have an abstract base class which defines several class attributes which are types themselves (i.e., nested types); the attributes are overridden in concrete derived classes inheriting from the abstract class. The concrete derived classes are used with several other classes which are type-annotated as generics, where the type parameter has a type constraint that it must be derived from the abstract class. I want these other classes (generics) to refer to the class attributes (nested types) of the concrete classes. The following example should demonstrate the idea:

import typing

class Base:
    A: typing.Type[Base]

class One(Base):
    class A(Base):  # overrides Base.A
        pass

class Two(Base):
    class A(One):  # overrides Base.A
        pass


T = typing.TypeVar('T', bound=Base)

class Foo(typing.Generic[T]):                 # the generic
    def foo(self, a: typing.Type[T.A]) -> T:  # <-- here, see T.A
        ...

f: Foo[Two] = Foo()
f.foo(Two.A)        # line 22 is here

MyPy v0.701 reports an error on line 22:

Argument 1 to "foo" of "Foo" has incompatible type "Type[A]"; expected "Type[Two]"

MyPy seems to ignore the attribute reference at T.A. How do I make it understand that I am trying to refer to the type available via the attribute .A? The attribute is guaranteed to be available because the type variable T is constrained to Base, which has A. Note that extending the set of generic types is not an option; I could elaborate on this but the explanation would be too specific to my application.

UPDATE: MyPy v0.711 with --new-semantic-analyzer yields a more comprehensible error message:

Name 'T.A' is not defined

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

L’issue non indica file o test del repository; inizia dall’esempio Python minimo e segui come mypy gestisce i bound di TypeVar, le annotazioni generiche e i riferimenti ai tipi degli attributi di classe. Il lavoro è completo quando il comportamento di tipizzazione richiesto ha un’implementazione o una limitazione definita ed è coperto da un test di regressione.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.