python / python/mypy

Infer type argument of an instance from arguments to a generic method called on it

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

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

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

説明

I think this is a feature request to allow specifying typing for the following. See the following example:

from abc import *
from fractions import *
from typing import *

Input = TypeVar('Input')
Output = TypeVar('Output')

class Base(Generic[Input, Output]):
    def do(self, input: Input) -> Output:
        # In reality do a bit more than just call "_operator".
        return self._operator(input)

    @abstractmethod
    def _operator(self, input: Input) -> Output:
        pass

class IntAdd(Base[int, int]):
    def _operator(self, input: int) -> int:
        return input + 1

class UnionAdd(Base[Union[int, Fraction], Union[int, Fraction]]):
    def _operator(self, input: Union[int, Fraction]) -> Union[int, Fraction]:
        return input + 1

reveal_type(IntAdd().do(1))
reveal_type(UnionAdd().do(1))

A = TypeVar('A', bound=Union[int, Fraction])

def do(input: A) -> A:
    return input + 1

reveal_type(do(1))
reveal_type(do(Fraction(2, 3)))

class SuperUnionAdd(Base[A, A]):
    def _operator(self, input: A) -> A:
        return input + 1

reveal_type(SuperUnionAdd().do(1))
reveal_type(SuperUnionAdd().do(Fraction(2, 3)))

The output I get is:

demo.py:25: error: Revealed type is 'builtins.int*'
demo.py:26: error: Revealed type is 'Union[builtins.int, fractions.Fraction]'
demo.py:31: error: Incompatible return value type (got "Union[int, Any]", expected "A")
demo.py:33: error: Revealed type is 'builtins.int*'
demo.py:34: error: Revealed type is 'fractions.Fraction*'
demo.py:38: error: Incompatible return value type (got "Union[int, Any]", expected "A")
demo.py:40: error: Revealed type is '<nothing>'
demo.py:40: error: Argument 1 to "do" of "Base" has incompatible type "int"; expected <nothing>
demo.py:41: error: Revealed type is '<nothing>'
demo.py:41: error: Argument 1 to "do" of "Base" has incompatible type "Fraction"; expected <nothing>

I am running mypy 0.701 on Python 3.7.1. I am on purpose use Fraction here so that we do not get automatic compatibility between numbers into effect.

What I would like to define is a UnionAdd class where if I pass to do an int, the return type would also be an int. And if I pass in Fraction, return type would be Fraction. But at the same time, I would like to use a Generic base class.

I know that I could define something like the do function I defined at the end. That one returns correctly the revealed type on lines 33 and 34 based on the input type. (Not sure what is the Incompatible return value type on line 31 about and how to fix it). But what I do not see is a way to combine the effect what I am doing for function do with the UnionAdd class and generic Base class. The example SuperUnionAdd where I blindly combine A and Base does not work.

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

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

はじめの一歩

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

調査の方向性

まず、提供された demo.py の例を再現し、ジェネリックなインスタンス上のジェネリックメソッドについて mypy が型変数をどのように推論するかを調べます。現在の revealed types と診断結果を、UnionAdd と SuperUnionAdd に対して要求されている引数ごとの推論と比較します。クラスメソッドが引数から int または Fraction を推論し、ジェネリックな Base の設計を維持できれば完了です。

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

評価

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

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

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