Consider loosening restrictions of `Final`
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 1.8k
- Fork
- 302
- Merge trung bình
- 23 giờ
- Pull request đã merge (30 ngày)
- 8
Mô tả
I think that Final is a very based idea, but I feel that it's current restrictions make using it painful.
I think that split assignments should be allowed:
def foo(cond: bool) -> None:
if cond:
a: Final = Funny("spam")
else:
a: Final = Funny("eggs") # this is currently an error
Most languages allow this pattern to initialize constant variables:
def foo(cond: bool) -> None:
a: Final[Funny]
if cond:
a = Funny("spam")
else:
a = Funny("eggs")
I think these are convenient and enables Final to be used in more scenarios, but this is currently an error.
But this is allowed:
def foo(cond: bool) -> None:
a: Final = Funny("spam") if cond else Funny("eggs")
Another issue I've found is the prohibition of Final declarations within loops:
def foo() -> None:
for i in [1,2,3]:
a: Final = i * 2 # currently an error
a = 10 # error
print(a)
print(a)
a = 10 # error
This is defined in the pep:
Note that a type checker need not allow Final declarations inside loops since the runtime will see multiple assignments to the same variable in subsequent iterations.
I understand that at runtime the same variable is reused for each iteration of the loop, but if you ignore that fact and just look at the semantic meaning of the code, it matches exactly to this Kotlin example:
fun foo() {
val a = listOf(1, 2, 3).map { i ->
val a = i
a = 10 // error
println(a)
a
}.last()
println(a)
a = 10 // error
}
Disallowing Final in loops doesn't address any of the motivations listed in the pep, any I can't see any reason why it should be disallowed.
Also, why are Final annotations not allowed on functional arguments?
Finalmay only be used as the outermost type in assignments or variable annotations. Using it in any other position is an error. In particular,Finalcan't be used in annotations for function arguments:
Why? This is commonly seen in other languages. In Kotlin, function parameters can only be val(Kotlin's form of Final), in Java a function parameter may be marked with final.
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.
Hướng nghiên cứu
Không có tệp hay bài kiểm thử nào được nêu tên. Hãy bắt đầu bằng việc xem lại các quy tắc Final hiện tại và các đoạn PEP được trích dẫn trong issue, sau đó đọc chuỗi bình luận để nắm các phản đối trước đây. Công việc được xem là hoàn tất khi đã đưa ra quyết định rõ ràng về phép gán tách, khai báo vòng lặp và chú thích đối số hàm, đồng thời ghi lại hành vi được chấp nhận trong đặc tả typing liên quan.
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
- tooling
- 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
- 25/100