Inferred attribute type feeds back as call context, rejecting its own defining assignment
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
- 48/100
Hướng nghiên cứu
Bắt đầu với trình tái hiện trong issue và lần theo quá trình kiểm tra lại bất đồng bộ bị trì hoãn đối với phép gán cho một thuộc tính self chưa được khai báo. So sánh đường đi này với các trường hợp thuộc tính cục bộ và thuộc tính có chú thích đang chạy thành công, tập trung vào cách ngữ cảnh giá trị trả về được suy luận truyền đến quá trình suy luận đối số generic. Được xem là hoàn tất khi trình tái hiện chạy thành công với K được suy luận là str và một bài kiểm thử hồi quy bao quát hành vi này.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug Report
Assigning a generic call returning K | None to a previously undeclared
self. attribute spuriously fails [arg-type] on that same assignment. The
attribute's inferred type (str | None) apparently comes back as call context
on a later pass, solving K = str | None; the invariant Mapping key then
rejects dict[str, str].
The same expression passes with no context, an annotated local, an annotated
attribute, or a reassigned local (whose second assignment is likewise checked
against its own inferred str | None) — the optional-stripping special case
seems skipped on the inferred-attribute path alone. A sync version (no
async/await) also passes, pointing at the deferred re-check await
triggers.
To Reproduce
from collections.abc import Mapping
async def select_one[K](options: Mapping[K, str]) -> K | None: ...
class ViaInferredAttr:
async def m(self) -> None:
cases = {"a": "x"}
self._done = await select_one(cases) # error (spurious)
async def via_local() -> None:
cases = {"a": "x"}
done = await select_one(cases) # ok
done = await select_one(cases) # ok (context = inferred str | None)
done2: str | None = await select_one(cases) # ok
class ViaAnnotatedAttr:
_done: str | None
async def m(self) -> None:
cases = {"a": "x"}
self._done = await select_one(cases) # ok
Expected Behavior
All call sites typecheck, with K = str.
Actual Behavior
repro.py:10: error: Argument 1 to "select_one" has incompatible type "dict[str, str]"; expected "Mapping[str | None, str]" [arg-type]
The str | None key shows the return's | None leaked into K.
Your Environment
- mypy 1.20.2 (compiled), no flags
- Python 3.13
Related: #5874 (over-eager outer context with unions), #19304 (assignment target
changes results). New here: the context is self-inferred — no annotation exists
anywhere — and explicit spellings of the same type behave differently.
- 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