python / python/mypy

Generic type variable not unified with specific bound type.

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

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

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

説明

Bug Report

In g below, argument types of different versions of x.f from different bounds of T cross-contaminate. When T is A, x.f can only accept A = T. Likewise, when T is B, x.f can only accept B = T. Hence the code is correct. However, mypy seems to believe that T can somehow be both A and B at simultaneously.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=a92693b3d619e1a11011b86a7cee1ce2

from typing import TypeVar, Generic

class A:
    def f(self, other: A) -> None:
        ...

class B:
    def f(self, other: B) -> None:
        ...

T = TypeVar('T', bound = A | B)

def g(x: T) -> None:
    x.f(x)

Expected Behavior

g typechecked successfully.

Actual Behavior

g fails typechecking:

main.py:14: error: Argument 1 to "f" of "A" has incompatible type "T"; expected "A"  [arg-type]
main.py:14: error: Argument 1 to "f" of "B" has incompatible type "T"; expected "B"  [arg-type]

Your Environment

  • Mypy version used: 1.5.0
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11

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

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

はじめの一歩

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

調査の方向性

まず、リンクされた mypy-play の再現例を mypy 1.5.0 とデフォルトのフラグで実行し、報告されたエラーを g で期待される型チェック成功の結果と比較します。呼び出し x.f(x) の背後にある型変数の処理を追跡します。この例が A と B の互換性のない引数エラーなしで型チェックを通過し、回帰テストのカバレッジが追加されれば完了です。

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

評価

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

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

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