python / python/mypy

Overload validation does not properly handle implied subtype relationships (e.g. int and float)

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

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

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

説明

I'm working to make pyright's overload validation work consistently with mypy's.

Mypy's overload validation behavior is spec'ed here: https://github.com/python/typing/issues/253#issuecomment-389262904

As part of this validation, mypy reports errors when there is overlap between two overloads that return different return types. The following test case shows four such examples. Mypy properly reports the error in three of the four cases, but it misses the case where there is an implied subtype relationship, as between int and float.

# pyright: strict

from typing import Literal, Union, overload

class Parent: ...
class Child(Parent): ...

# Test 1: Literal subtype
@overload
def foo1(x: Literal[3]) -> int: ...
@overload
def foo1(x: int) -> str: ...

# Test 2: Subclass subtype
@overload
def foo2(x: Child) -> str: ...
@overload
def foo2(x: Parent) -> int: ...

# Test 3: Implicit subtype
@overload
def foo3(x: int) -> str: ...  # Mypy does not report error here
@overload
def foo3(x: float) -> int: ...

# Test 4: Union subtype
@overload
def foo4(x: int) -> str: ...
@overload
def foo4(x: Union[int, str]) -> int: ...

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

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

はじめの一歩

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

調査の方向性

まず、issue にある4つの overload の例を再現し、mypy の診断をリンク先の typing specification と比較します。次に、サブタイプの重複チェックを担当する overload 検証パスを追跡し、そのうえで int/float のケースが literal、subclass、union のケースと一貫して診断されることを確認します。

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

評価

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

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

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