python / python/mypy

stubgen creates duplicate class definitions

Open
#10,526 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-stubgen
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with stubgen and its handling of the platform-conditional definitions in serialposix; the generated stubs/serial/serialposix.pyi shows the duplicate PlatformSpecific declarations. Determine how platform-specific class definitions should be represented, then verify that the generated stub no longer produces duplicate names and that mypy accepts it across platforms.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.