python / python/mypy

stubgen creates duplicate class definitions

Aperta
#10,526 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-stubgen
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

I need a stub for the serial package, and attempted to use stubgen to create it. The problem arises in the serialposix subpackage, which defines the PlatformSpecific class differently depending upon the platform it's running on. When stubgen processes this code, it discards all the conditional pieces, the end result being multiple definitions of the PlatformSpecific class, which, not surprisingly, mypy is very unhappy about.

Here's what the original code looks like (trimmed for compactness):

plat = sys.platform.lower()
if plat[:5] == 'linux':    # Linux (confirmed)  # noqa
    class PlatformSpecific(PlatformSpecificBase):
        …
elif plat == 'cygwin':       # cygwin/win32 (confirmed)
    class PlatformSpecific(PlatformSpecificBase):
        …
elif plat[:6] == 'darwin':   # OS X
    class PlatformSpecific(PlatformSpecificBase):
        …

and so on. You get the picture.

Running mypy after generating the stub gives:

stubs/serial/serialposix.pyi:24: error: Name 'PlatformSpecific' already defined on line 20
stubs/serial/serialposix.pyi:29: error: Name 'PlatformSpecific' already defined on line 20
stubs/serial/serialposix.pyi:37: error: Name 'PlatformSpecific' already defined on line 20
stubs/serial/serialposix.pyi:42: error: Name 'PlatformSpecific' already defined on line 20

Now, I can manually go in and edit the generated stub, commenting out the class definitions for platforms other than the one I am running on. But the problem is that I can't then check in that stub and expect mypy to run correctly on other platforms.

I don't think this is a typeshed issue, because I don't see a way that the stub can be written to work across platforms, which seems to be a problem inherent in the way that stubgen, and stubs in general, work. Unless I'm missing something (which is entirely possible), there is nothing in mypy that will allow this type of code to be stubbed successfully.

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

Inizia con stubgen e con la sua gestione delle definizioni condizionali della piattaforma in serialposix; il file generato stubs/serial/serialposix.pyi mostra le dichiarazioni duplicate di PlatformSpecific. Determina come debbano essere rappresentate le definizioni delle classi specifiche della piattaforma, quindi verifica che lo stub generato non produca più nomi duplicati e che mypy lo accetti su tutte le piattaforme.

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

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.