python / python/mypy

Mypy gives confusing/misleading error message

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

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

bug
Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug Report

Mypy reports error: Name "..." is not defined [name-defined] when using an instance attribute that was annotated.

To Reproduce

from inspect import Parameter


class ListElementDescriptor[T]:
    name: str
    item_type: type[T]

    @property
    def signature_parameter(self) -> Parameter:
        return Parameter(name=self.name, kind=Parameter.KEYWORD_ONLY, annotation=list[self.item_type])

Expected Behavior

Definitely not an error that self.item_type is not defined. I suspect mypy tries to to convey a different error, maybe related to using variables for type parameters in generics, though I disagree with that as well since list[] is not used here in a typing context to specify a type, but is used as a value.

Actual Behavior

typing-7.py:11: error: Name "self.item_type" is not defined  [name-defined]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.12.0+dev.6a0657e5959ba1777c4d427f8f355d499035d145 (compiled: no)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files):
[tool.mypy]
enable_incomplete_feature = "NewGenericSyntax"
disable_bytearray_promotion = true
disable_memoryview_promotion = true
check_untyped_defs = true
warn_unreachable = true
warn_redundant_casts = true
warn_unused_ignores = true
  • Python version used: 3.12

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.

Hướng nghiên cứu

Bắt đầu bằng cách chạy ví dụ ListElementDescriptor được cung cấp với mypy 1.12.0+dev.6a0657e5959ba1777c4d427f8f355d499035d145 và cấu hình Python 3.12 đã nêu. Theo dõi chẩn đoán cho list[self.item_type] và cập nhật nó để vấn đề được báo cáo là chính xác, thay vì cho rằng self.item_type chưa được định nghĩa; chạy lại reproduction để xác nhận lỗi name-defined gây hiểu nhầm đã biến mất.

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

Đánh giá

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

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.