TYPE_CHECKING in ternary expression
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Trước tiên, hãy tái hiện cả hai ví dụ Python bằng mypy và so sánh cách TYPE_CHECKING được xử lý trong biểu thức ba ngôi so với dạng if/else. Theo dõi điểm vào của quá trình kiểm tra kiểu đối với biểu thức ba ngôi để xác định liệu hành vi này là một bug hay nên được ghi lại trong tài liệu. Công việc được xem là hoàn tất khi ví dụ được xác thực chính xác hoặc hành vi khác biệt được ghi lại rõ ràng, kèm theo phạm vi kiểm thử hồi quy nếu hành vi thay đổi.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
This issue should be marked with "topic-ternary-expression" label.
Mypy doesn't understand TYPE_CHECKING checked in a ternary expression.
Example 1
from typing import TYPE_CHECKING
from redis.asyncio import ConnectionPool
from redis.asyncio import Redis as OGRedis
from . import settings
Redis = OGRedis[bytes] if TYPE_CHECKING else OGRedis # Redis is Generic in stubs
async def func() -> None:
connection_pool = ConnectionPool.from_url(settings.redis_url)
async with Redis(connection_pool=connection_pool) as redis:
await redis.set(
name="test",
value="Hello World",
)
$ mypy project/
project/code.py:13: error: "object" has no attribute "__aenter__" [attr-defined]
project/code.py:13: error: "object" has no attribute "__aexit__" [attr-defined]
Found 2 errors in 1 file (checked 3 source files)
Example 2
from typing import TYPE_CHECKING
from redis.asyncio import ConnectionPool
from redis.asyncio import Redis as OGRedis
from . import settings
if TYPE_CHECKING: # these
Redis = OGRedis[bytes] # 4 lines
else: # are
Redis = OGRedis # the only difference
async def func() -> None:
connection_pool = ConnectionPool.from_url(settings.redis_url)
async with Redis(connection_pool=connection_pool) as redis:
await redis.set(
name="test",
value="Hello World",
)
$ mypy project/
Success: no issues found in 3 source files
That's either a bug or an expected behavior that can be documented. I can imagine a programmer spending an hour debugging and searching why his/her code isn't validated properly.
- 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