python / python/typing

Assuming `self` or `cls` parameter is positional-only

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

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

topic: other
Ngôn ngữ chính
Python
Star
1.8k
Fork
302
Merge trung bình
23 giờ
Pull request đã merge (30 ngày)
8

Mô tả

I'm interested in guidance from the typing community relating to a question that came up in a recent pyright bug report.

The issue has to do with the self parameter in an instance method and the cls parameter in a class method — whether a type checker should assume that they are implicitly positional-only parameters. In particular, the discussion was about method declarations within a protocol class.

PEP 570 makes a reference to this topic in its introductory section:

One special case of this situation is the self parameter for class methods: it is undesirable that a caller can bind by keyword to the name self when calling the method from the class.

But PEP 570 doesn't provide any specific guidance about self or cls in its specification section. PEP 544 (which introduced protocols) is also silent on the topic.

Pyright currently assumes that self and cls are not positional-only parameters (unless explicitly followed by a / parameter marker or named with double underscores). This assumption affects protocol matching — and more generally, subtype compatibility evaluation for callables.

@AlexWaygood is proposing that the first parameter of instance and class methods should always be considered position-only parameters.

This raises some questions in my mind:

  1. Is this a safe assumption? Does anyone know of a situation where self or cls are used as keyword arguments?
  2. Does this apply only to protocol methods, or does it apply generally to methods in any class?
  3. Should it be considered a type checker error if someone attempts to use a keyword argument to target the first parameter of an instance or class method? It works at runtime.
class Foo:
    def bar(self): pass

Foo.bar(self=Foo())
  1. Does the cls exemption apply only to explicit class methods, or does it also apply to __new__ and __init_subclass__?

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

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 việc đọc PEP 570 và PEP 544, sau đó xem lại báo cáo lỗi Pyright được liên kết và phần thảo luận của issue. Xác định xem self và cls có nên được coi là chỉ-định-vị-trí đối với các method, protocol, newinit_subclass hay không. Được xem là hoàn tất khi đạt được hướng dẫn cộng đồng được ghi lại hoặc một quyết định về đặc 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
tooling
Loại issue
Tài liệu
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
20/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.