python / python/typing_extensions
Discussion: Runtime error when overriding a mutable key with a `ReadOnly` one
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 583
- Fork
- 146
- Merge trung bình
- 10 giờ 11 phút
- Pull request đã merge (30 ngày)
- 5
Mô tả
Follow up from here: https://github.com/python/typing_extensions/issues/606#issuecomment-3277382801
Background:
Currently CPython emits a TypeError when a mutable key from a base TypedDict is overwritten by a ReadOnly one of a child: https://github.com/python/cpython/blob/805e3368d6d07e58430654d1365283924fdf4143/Lib/typing.py#L3151
and it has an explicit test for that behavior: https://github.com/python/cpython/blob/805e3368d6d07e58430654d1365283924fdf4143/Lib/test/test_typing.py#L9077
However typing_extensions permits and tests for that:
- https://github.com/python/typing_extensions/blob/main/src/typing_extensions.py#L1221
- https://github.com/python/typing_extensions/blob/main/src/test_typing_extensions.py#L5000
Originally typing_extensions had the same behaviour as CPython, however that was changed: https://github.com/python/typing_extensions/commit/d6c50f585c386490d38ad6b8ce5543aed6e633a2
This adjustment was not taken into account when the CPython implementation was done: https://github.com/python/cpython/pull/116350
Although this point was later raised in the issue (after the merge of the PR): https://github.com/python/cpython/issues/116127#issuecomment-3176307396
My opinion:
From what i understand the TypeError in CPython for this behaviour is pretty unique, as i dont think any other such attribute is runtime enforced.
typing.Final and typing.ReadOnly even specify "There is no runtime checking for this property." in the docs. (Although that obviously refers them only being read, but still).
Additionally the typing docs in general state "The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc.". Lastly, neither the docs nor the PEP say anything about such a type error existing.
So i definitely feel like the optimal thing here would be to not have a runtime error.
On top of that i feel like removing a TypeError is probably a less intrusive change than adding one, even though changes in CPython are obviously more impactful than ones in typing_extensions.
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
So sánh cách xử lý ReadOnly-over-mutable-key trong src/typing_extensions.py với phần triển khai typing.py tương ứng được liên kết của CPython. Đọc các trường hợp tương ứng trong src/test_typing_extensions.py và test_typing.py của CPython, sau đó xác định dự án nên hỗ trợ hành vi thời gian chạy nào. Công việc được xem là hoàn tất khi quyết định đó được ghi lại bằng phần triển khai và các bài kiểm thử nhất quán.
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
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- 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