python / python/mypy

Bad self type inference with Generics and inheritance?

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

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

bug priority-2-low
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

  • Are you reporting a bug, or opening a feature request?

Reporting a bug

  • Please insert below the code you are checking with mypy
from typing import TypeVar, Generic

T = TypeVar("T")

class Foo(Generic[T]):
    def __init__(self): ...

class Bar(Generic[T]): ...

class FooChild(Foo[Bar[T]]):
    def __init__(self, blah: T):
        Foo.__init__(self)
  • What is the actual behavior/output?
$ mypy foo.py
foo.py:18: error: Argument 1 to "__init__" has incompatible type "FooChild[T]"; expected "Foo[T]"
Found 1 error in 1 file (checked 1 source file)
  • What is the behavior/output you expect?
Success: no issues found in 1 source file
  • What are the versions of mypy and Python you are using?
mypy 0.750
Python 3.7.3
  • Do you see the same issue after installing mypy from Git master?

Yes, successfully reproduced with mypy 0.790+dev.df6894b5a9bb1a913e66df87138cd7a240c09fc1

  • What are the mypy flags you are using? (For example --strict-optional)

No flags.

  • If mypy crashed with a traceback, please paste
    the full traceback below.

No crash

  • Additional information:

The following versions typecheck successfully:

from typing import TypeVar, Generic

T = TypeVar("T")

class Foo(Generic[T]):
    def __init__(self): ...

class Bar(Generic[T]): ...

class FooChild(Foo[T]): # Removed one level of nesting here
    def __init__(self, blah: T):
        Foo.__init__(self)
from typing import TypeVar, Generic

T = TypeVar("T")

class Foo(Generic[T]):
    def __init__(self): ...

class Bar(Generic[T]): ...

class FooChild(Foo[Bar[T]]):
    def __init__(self): # Removed the argument here
        Foo.__init__(self)

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

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

はじめの一歩

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

調査の方向性

提供された foo.py の再現コードから始め、報告された mypy のバージョンに対して実行して、FooChild[T] の誤った self 型を確認します。mypy の型推論に関係するジェネリック継承とコンストラクター呼び出しを追跡し、ネストされた Bar[T] のケースがエラーなしで成功することを示すテストカバレッジを追加します。

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

評価

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

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

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