Make each assignment to define a distinct variable with independent type
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
- 25/100
Hướng nghiên cứu
Trước tiên, hãy đọc các issue liên quan #6233 và #6232, sau đó xem xét phần triển khai prototype được đề cập trong issue. Công việc được đề xuất là một pass đổi tên biến tổng quát sử dụng các biến nội bộ riêng biệt và các nút phi, với mục tiêu hoàn tất là đảm bảo khả năng tương thích với ngữ nghĩa hiện tại.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
These long-standing issues can be solved by generalizing the variable renaming pass that is used by --allow-redefinition:
- #6233
- #6232
Each assignment to a name would generate a separate internal variable. We will use "phi nodes" to merge these variables when different control flow paths assign to different variants. Here is a simple example:
def f() -> None:
if c():
x = 0
else:
x = ""
reveal_type(x)
The new variable renaming pass would produce a new AST that resembles this program (note that phi(...) is a new special AST node type and not a function call):
def f() -> None:
if c():
x = 0
else:
x' = ""
x'' = phi(x, x')
reveal_type(x'') # int | str
This resembles the static single assignment form (SSA) used by many compilers, but probably would not conform to it 100% due to various practical reasons.
I'm working on a prototype implementation.
I hope that we can make this sufficiently compatible with the current semantics so that we can enable it by default (in mypy 2.0, possibly).
An alternative way to provide similar functionality would be to infer variable types from multiple assignments, similar to what already happens if a variable is declared as x: object. The renaming approach has a few notable benefits:
- It can (more) easily support partial types (e.g. inferring a list item type from an
x.appendcall). - It's a generalization of how we've already implemented
--allow-redefinition. - It should make it easier to generate efficient code in mypyc.
- The implementation will mostly be a new renaming pass, so it won't make other parts of mypy much more complicated (though mypyc needs changes).
- The conditional type binder has some tech debt and I'm not excited about making it even more complicated, which would be the case if we'd use the alternative approach.
- 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
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.
Issue khác của python/mypy
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
bug topic-configuration topic-error-reporting
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Issue tương tự
-
link-check link-check:sphinx-theme
Độ 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 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
OpenHands/extensions#626 · 1 bình luận ·
-
Change observation tooltip text Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
CSCfi/sd-search-api#39 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100