Descriptors don't work with `TypeGuard`
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
- 42/100
Hướng nghiên cứu
Bắt đầu với bản tái hiện được liên kết trong mypy-play và so sánh hành vi của TypeGuard trong problem() với lệnh gọi get trực tiếp đang hoạt động trong contrast(). Theo dõi đường dẫn narrowing dựa trên descriptor và xác minh rằng instance được thu hẹp thành DerivedClass trong nhánh đúng, trong khi nhánh sai vẫn là BaseClass.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug Report
The tool doesn't correctly interpret TypeGuard in the context of a descriptor.
To Reproduce
mypy-play.net gist
from __future__ import annotations
from typing import *
from typing_extensions import (
TypeGuard,
)
class SomeDescriptor:
# simplified: no @overload for obj=None, klass=<type ...>
def __get__(
self, obj: object, klass: Optional[Type[object]] = None
) -> TypeGuard[DerivedClass]:
return isinstance(obj, DerivedClass)
class BaseClass:
described = SomeDescriptor()
class DerivedClass(BaseClass): pass
def problem(instance: BaseClass) -> None:
if instance.described:
reveal_type(instance) # BaseClass (expected: DerivedClass)
else:
reveal_type(instance) # BaseClass (correct)
def contrast(descr: SomeDescriptor, instance: BaseClass) -> None:
if descr.__get__(instance):
reveal_type(instance) # DerivedClass (correct)
else:
reveal_type(instance) # BaseClass (correct)
Expected Behavior
In problem(), the type of instance should be narrowed when instance.described is truthy.
Actual Behavior
While mypy understands that a TypeGuard returns a bool, it doesn't narrow the type when the TypeGuard returns True.
Your Environment
- Mypy version used: 1.8.0
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.8, 3.12
- 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ự
-
area/auth bug comp/agent P3 platform/discord type/security
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
NousResearch/hermes-agent#117848 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
bancolombia/sentinel#23 ·
-
test md Đang mởCI
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100