stubgen creates duplicate class definitions

Offen
#10,526 0 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
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
python
Bereich
tooling

Rechercherichtung

Beginne mit stubgen und seiner Behandlung der plattformbedingten Definitionen in serialposix; die generierten stubs/serial/serialposix.pyi zeigt die doppelten PlatformSpecific-Deklarationen. Ermittle, wie plattformspezifische Klassendefinitionen dargestellt werden sollten, und überprüfe anschließend, dass der generierte Stub keine doppelten Namen mehr erzeugt und dass mypy ihn auf allen Plattformen akzeptiert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

bug topic-stubgen

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.

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.