Inconsistent behavior when validating type parameter substitutions
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug report
Bug description:
There are currently three different cases (two of them being closely related) where type parameters can be substituted/parameterized with concrete types:
-
On a user-defined generic class:
class MyGeneric[T1, T2]: ... alias = MyGeneric[int, str] -
On an already parameterized alias (following the previous example):
temp_alias = MyGeneric[int, T2] alias = temp_alias[str] -
On a PEP 695 type alias:
type MyAlias[T1, T2] = dict[T1, T2] gen_alias = MyAlias[int, str]
All of these three cases have slightly different behavior. The one that seems to be the most accurate is the second one. As alias is a _GenericAlias instance, parameterizing it will call _GenericAlias.__getitem__. There is a bunch of logic in there, and it seems that both __typing_prepare_subst__ and then __typing_subst__ (which is only doing type check assertions) are being called.
However, the first case is not making the calls to __typing_subst__, meaning the following would unexpectedly work:
class A[T, **P]: ...
A[int, str]
# ok at runtime, should fail as `P` should be substituted with a valid parameter expression
# (another ParamSpec, the ellipsis, a list/tuple of types or a Concatenate form).
If you do the same on a _GenericAlias instance (matches the second case), an error is raised:
alias = A[T, P]
alias[int, str]
# TypeError: Expected a list of types, an ellipsis, ParamSpec, or Concatenate. Got <class 'str'>
This leads us to the first point: should we apply the same logic between these two cases? To avoid breaking changes, we might have to consider forward references:
class A[T, **P]: ...
A[int, 'ForwardParamSpec']
ForwardParamSpec = ParamSpec('ForwardParamSpec')
So perhaps we can exclude ForwardRef from the type check in ParamSpec.__typing_subst__ (note that it already doesn't work for the second case).
I'll also note that having __typing_subst__ not called is not the only difference. For instance, the second case also calls _unpack_args() on the passed args, while the first case doesn't [^1]
Onto the last case (PEP 695 type aliases), currently no validation is performed whatsoever:
type MyAlias[T1, T2] = dict[T1, T2]
MyAlias[int] # no error
So the second point is: should we apply the same logic as in case 1/2? Again, I don't know if applying the same logic here on type aliases is going to introduce any breaking changes concerns?
[^1]: Here is an example of how this manifests: https://gist.github.com/Viicos/db10da58914809a87c0a82c1b2f19162
CPython versions tested on:
3.14
Operating systems tested on:
Linux
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 trong Lib/typing.py với _GenericAlias.getitem, ParamSpec.typing_subst và _unpack_args(), sau đó so sánh các đường đi này với việc thay thế trực tiếp lớp generic và bí danh kiểu PEP 695. Sử dụng các ví dụ trong báo cáo để xác định liệu việc xác thực có nên nhất quán hay không, bao gồm cả các tham chiếu chuyển tiếp, và kiểm tra hành vi kết quả trong cả ba trường hợp.
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
- compilers
- Loại issue
- Lỗi
- Độ 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
- Cần làm rõ
- Mức phù hợp với người mới
- 25/100