python / python/mypy

Stubgen: Instance Attributes set to `Incomplete` even though explicit declared

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

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

feature topic-stubgen
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Bug Report

I am using Stubgen to type my files.
Here, I have a class with a lot of instance attributes.
They are all declared within the __init__ method.
They are set using a method which returns Any as type, but manually set to the correct type:

When generating the stub file, all are set to Incomplete instead of the highlighted code.
Trying to use # type: str behind it results in a error: Critical error during semantic analysis: foo/bar.py:424242: error: invalid syntax [syntax]

To Reproduce

from typing import Any


class MyClass:

    def __init__(self) -> None:
        bar = Bar("test")
        self.test: str = bar.foo("test")


class Bar:
    def __init__(self, anything: str) -> None:
          pass

    def foo(self, test: str) -> Any:
         return test

results in:

from _typeshed import Incomplete
from typing import Any

class MyClass:
    test: Incomplete
    def __init__(self) -> None: ...

class Bar:
    def __init__(self, anything: str) -> None: ...
    def foo(self, test: str) -> Any: ...

Gist URL: https://gist.github.com/27dfb95516013fa15ed80c1411ce5a4e
Playground URL: https://mypy-play.net/?mypy=latest&python=3.9&gist=27dfb95516013fa15ed80c1411ce5a4e

Expected Behavior

I want the attribute to have the correct, annotated type.

from typing import Any

class MyClass:
    test: str
    def __init__(self) -> None: ...

class Bar:
    def __init__(self, anything: str) -> None: ...
    def foo(self, test: str) -> Any: ...

Actual Behavior

It is exported as incomplete

from _typeshed import Incomplete
from typing import Any

class MyClass:
    test: Incomplete
    def __init__(self) -> None: ...

class Bar:
    def __init__(self, anything: str) -> None: ...
    def foo(self, test: str) -> Any: ...

Your Environment

  • Mypy version used: mypy 1.0.0 (compiled: yes)
  • Mypy command-line flags: stubgen ./foo_bar -o .
  • Mypy configuration options from mypy.ini (and other config files): None used
  • Python version used: Python 3.10.9

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

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

はじめの一歩

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

調査の方向性

まず、示されている Python の例と stubgen ./foo_bar -o . を使って報告内容を再現し、次に、Any を返すメソッドから代入される、明示的にアノテーションされたインスタンス属性を stubgen がどのように処理するかを調べます。生成されたスタブに Incomplete のインポートと使用ではなく test: str が含まれ、報告された構文エラーが発生しなければ完了です。

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

評価

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

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

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