Duplicate keys cause earlier TypedDict values to be skipped during type checking

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

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
74/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
python
Lĩnh vực
compilers

Hướng nghiên cứu

Bắt đầu tại mypy/checkexpr.py, khoảng các dòng 839-842, và tái hiện vấn đề với example.py bằng lệnh mypy được cung cấp. Theo dõi cách các khóa TypedDict trùng lặp thay thế các biểu thức trước đó; hoàn thành khi mọi biểu thức giá trị đều được kiểm tra kiểu và trường hợp parse(123) báo lỗi arg-type như mong đợi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug topic-typed-dict

Bug Report

When a dictionary literal used to construct a TypedDict contains duplicate
keys, mypy only type-checks the final value for that key. Earlier value
expressions are skipped entirely.

This can hide real errors because Python still evaluates every value expression
in the dictionary literal, including values that are later overwritten by a
duplicate key.

To Reproduce

from typing import TypedDict


class Payload(TypedDict):
    value: int


def parse(value: str) -> int:
    return len(value)


payload = Payload({"value": parse(123), "value": 0})
$ mypy --show-error-codes --no-incremental example.py
Success: no issues found in 1 source file

Running the program demonstrates that the skipped expression is evaluated:

TypeError: object of type 'int' has no len()

Expected Behavior

Mypy should type-check every value expression in the dictionary literal, even
if a later duplicate key determines the final value of the TypedDict field.
In this example, it should report an error similar to:

error: Argument 1 to "parse" has incompatible type "int"; expected "str"  [arg-type]

The same expression is correctly rejected when the target is a regular
dict[str, int]. The issue also occurs with a contextually typed literal:

payload: Payload = {"value": parse(123), "value": 0}

Actual Behavior

Mypy reports no issues because only the last value associated with the
duplicate key is checked.

Additional Context

The current implementation replaces the previously collected expression
without type-checking it and has a TODO to type-check all duplicate-key values:

https://github.com/python/mypy/blob/6eb141f03a8b43c274f15cc4978d617a71d8f37e/mypy/checkexpr.py#L839-L842

PR #15425 also mentioned repeated TypedDict keys as a known limitation, but I
could not find an existing issue that tracks it:

https://github.com/python/mypy/pull/15425

Environment Used to Reproduce

  • Mypy versions used:
    • mypy 2.3.0 (compiled: yes)
    • mypy 2.4.0+dev.6eb141f03a8b43c274f15cc4978d617a71d8f37e (compiled: no)
  • Mypy command-line flags: --show-error-codes --no-incremental
  • Mypy configuration options: none
  • Python version used: 3.12.8
Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Merge trung bình
1 ngày 9 giờ
Pull request đã merge (30 ngày)
56

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.