TYPE_CHECKING in ternary expression

Đang mở
#15,856 0 bình luận 7 reaction 0 người được giao Xem trên GitHub

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
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
devtools

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ả

documentation topic-ternary-expression

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

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của python/mypy

Tất cả issue của python/mypy

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.