`[Async]ContextDecorator.__call__` return type should depend on the underlying context manager's `_ExitT_co`
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
- #14439 của @johnslavik — đã đóng, không merge
Đá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
- 35/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Cần làm rõ
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- python
- Lĩnh vực
- developer-experience
Hướng nghiên cứu
Bắt đầu với cuộc thảo luận Python được liên kết và phần triển khai thư viện chuẩn trong Lib/contextlib.py, tập trung vào ContextDecorator.call và _ExitT_co của context manager bên dưới. Xác định một mối quan hệ được hỗ trợ ở cấp độ kiểu cho kiểu trả về của callable được bao bọc, sau đó xác minh rằng annotation phản ánh các trường hợp khi việc suppression của context manager trả về None.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
[!Important]
Please see https://discuss.python.org/t/support-suppressing-generator-context-managers/103615 first!
This is how ContextDecorator.__call__ is implemented in the Python standard library:
def __call__(self, func):
@wraps(func)
def inner(*args, **kwds):
with self._recreate_cm():
return func(*args, **kwds)
return inner
In typeshed, ContextDecorator.__call__ currently has a return type of the original callable (func).
However, if the return value of self._recreate_cm().__call__ is truthy, the return type of ContextDecorator.__call__ can as well be None, like in the silly example below:
from collections.abc import Generator
from contextlib import contextmanager, suppress
from typing import reveal_type
@contextmanager
def zero_division_to_none() -> Generator[None]:
with suppress(ZeroDivisionError):
yield
@zero_division_to_none()
def div(a: int, b: int) -> float:
return a / b
reveal_type(div(1, 0))
# mypy, pyright: Revealed type is "builtins.float"
# Runtime type is 'NoneType'
My first shot was to patch the proper __call__ functions to "extend" the return type with _ThrottledExcReturnT (a new type parameter of ContextDecorator), constrained by Never and None, with the default type Never for backward compatibility. To me, there's no way to declare the relationship symbolically, so it would have to be typed manually in classes implementing the context manager protocol.
- Ngôn ngữ chính
- Python
- Star
- 5.1k
- Fork
- 2.1k
- Merge trung bình
- 2 ngày 1 giờ
- Pull request đã merge (30 ngày)
- 115
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc 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/typeshed
-
`networkx.set_edge_attributes` should support scalar value + nameCó thể đã có người làm @Fire162 đã nhận 10 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
Maintainer thường phản hồi trong vòng 1 ngày
-
false alarm on decode callCó thể đã có người làm @jw9829 đã nhận 38 ngày trước. Đang mởhelp wanted stubs: false positive
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
python/typeshed#16441 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Don't run pyright basic checks on projects with strict pyright settings in CICó thể đã có người làm @Mohith1612 đã nhận 74 ngày trước. Đang mởproject: infrastructure
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
python/typeshed#16005 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
stubs: improvement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
python/typeshed#15548 · 5 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
stubs: improvement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
python/typeshed#15529 · 5 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của python/typeshed
Issue tương tự
-
bump patch
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
autowarefoundation/autoware_carla_scenario#1321 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 78/100
QuantConnect/Documentation#2838 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
get_all_subclasses must deduplicate by class identityCó thể đã có người làm @Gambit-Checkmate đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 15 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
awslabs/route53-dynamic-dns-with-lambda#67 · 1 bình luận ·