Infer type argument of an instance from arguments to a generic method called on it
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 20.6k
- Fork
- 3.3k
- Merge trung bình
- 1 ngày 18 giờ
- Pull request đã merge (30 ngày)
- 54
Mô tả
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.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách tái hiện ví dụ demo.py được cung cấp và kiểm tra cách mypy suy luận các biến kiểu cho các phương thức generic trên các thể hiện generic. So sánh các kiểu được tiết lộ và các chẩn đoán hiện tại với yêu cầu suy luận theo từng đối số cho UnionAdd và SuperUnionAdd. Được xem là hoàn tất khi các phương thức của lớp suy luận int hoặc Fraction từ các đối số của chúng, đồng thời vẫn giữ thiết kế Base generic.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- devtools
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100