Self is not rejected when used as a PEP 695 type parameter bound

Đang mở
#21,960 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.

Đá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
72/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
compilers

Hướng nghiên cứu

Bắt đầu với test-data/unit/check-selftype.test, khoảng dòng 1480, và tái hiện ví dụ PEP 695 từ issue. Truy vết phần validation hiện có của Self được dùng cho các vị trí không hợp lệ khác, sau đó bổ sung coverage cho bound hoặc constraint của một type parameter. Hoàn tất khi mypy báo lỗi cho ví dụ, trong khi các test Self hiện có vẫn tiếp tục pass.

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

Mô tả

bug topic-self-types
Bug Report

Self is only valid in the specific locations listed in PEP 673 and Mypy already rejects several invalid locations.
See: https://github.com/python/mypy/blob/ae39cdb2fba8908eeff6ec8d6b88879c62495fcc/test-data/unit/check-selftype.test#L1480

It seems one location wasn't covered: using Self as the bound (or constraint) of a PEP 695 type parameter.

To Reproduce:
from typing import Self
class Foo:
      def fails[T: Self](self: T) -> None: pass
Expected Behavior

Mypy should reject Self here with an error.

From a quick look at other type checkers: pyright and ty treat it as an invalid/generic TypeVar bound (TypeVar upper bound cannot be generic), while zuban rejects it with the same message mypy already uses elsewhere,
Self type is only allowed in annotations within class definition.

Actual Behavior

No error is reported.

Your Environment

Mypy version: https://github.com/python/mypy/commit/ae39cdb2fba8908eeff6ec8d6b88879c62495fcc
Python version: 3.12

TLDR; originally spotted while working on #21928 See: https://github.com/python/mypy/pull/21928#issuecomment-5557910054

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.