Cannot anotate type of generic functions.
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 32/100
Hướng nghiên cứu
Bắt đầu với các ví dụ về hàm generic và issue Pyright được liên kết, sau đó so sánh chúng với các quy tắc hiện tại về phạm vi của biến kiểu. Công việc được xem là hoàn tất khi đặc tả typing có một hướng giải quyết đã được thống nhất cho việc chú thích biến hàm generic và biến thể hiện của lớp; issue không nêu tên tệp hoặc test nào trong repository.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
The current scoping rules for type variables seems to deny type annotations over generic functions.
The issue
define two functions as follows:
def fx_1(f: Callable[[bool], int]) -> int:
return f(True)
def fx_2[T](f: Callable[[bool], T]) -> T:
return f(True)
If a function alias is required, these functions can be assigned to variables. However, if type annotations are used, fx_2 cannot be annotated using compliant type checkers (i.e. pyright).
T = TypeVar("T")
fx_1_alias: Callable[[Callable[[bool], int]], int] = fx_1 # OK!
fx_2_alias: Callable[[Callable[[bool], T]], T] = fx_2 # complains that T has no meaning
Justification
- All variables should be able to be type annotated, even when those variables are functions or the annotation is not required.
defis a syntactic sugar equivalent to a variable assignation over a lambda function. As a result, it should be possible to define generic functions withoutdefstatements e.g.
fx_2_alternative: Callable[[Callable[[bool], T]], T] = lambda f: f(True)
- Generics are the first ladder of what is called a "dependent type". In the case of functions, this corresponds to a Π type where the input itself is the concrete type that will be replaced on the type var of a generic type. Here type-vars scope is over the function type itself rather than some outer entity (class, function, method).
- By these definitions of Π type, it cannot exist an unbound type variable when function/callable types are involved, since in python, the type argument is implicit and supplied when referring a TypeVar.
e.g.
T = TypeVar("T")
fx_2_alias: Callable[[Callable[[bool], T]], T] = fx_2
fx_2_alias_2: Callable[[Callable[[bool], T]], T] = fx_2
Even when using the same type var fx_2_alias and fx_2_alias_2 each var is an independent one.
A more clear syntax (like the introduced on def statements on python312) could be
fx_2_alias: Callable[X][[Callable[[bool], X]], X] = fx_2
fx_2_alias_2: Callable[X][[Callable[[bool], X]], X] = fx_2
This marks explicitly that the scope of the var is over the type itself. However, this issue does not aim to propose a new syntax, but rather aims to adjust the scoping rules of type vars.
Motivation
This issue was derived from a pyright issue where generic functions as class instance variables are denied by the checker because of the same scope issues described before.
- Ngôn ngữ chính
- Python
- Star
- 1.8k
- Fork
- 302
- Merge trung bình
- 23 giờ
- Pull request đã merge (30 ngày)
- 8
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
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.
Issue khác của python/typing
-
topic: typing spec
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
topic: typing spec
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
topic: documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
topic: documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
topic: conformance tests topic: typing spec
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
Tất cả issue của python/typing
Issue tương tự
-
Add: hunch Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
AbdelStark/awesome-typesafe#104 ·
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
DiamondLightSource/dodal#2211 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
openml/openml-python#1749 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
sipyourdrink-ltd/bernstein#6191 ·