stubgen creates duplicate class definitions

オープン
#10,526 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
python
領域
tooling

調査の方向性

stubgen と、serialposix におけるプラットフォーム条件付き定義の処理から始めます。生成された stubs/serial/serialposix.pyi には、重複した PlatformSpecific 宣言が示されています。プラットフォーム固有のクラス定義をどのように表現すべきかを判断し、その後、生成されたスタブで重複した名前が生成されなくなり、すべてのプラットフォームで mypy が受け入れることを確認します。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/mypy のほかの issue

python/mypy の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。