python / python/cpython

Class creation doesn't call __set_name__ when creating class members in another __set_name__

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

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

type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug report

Bug description:

When inserting a class member during some other class member's __set_name__ call, the __set_name__ method of the newly-inserted member doesn't get called as expected.

As an example, take this snippet:

class I_Am_Altering_The_Deal:
    def __set_name__(self, owner, name):
        print(f"__set_name__({self.__class__.__name__}, {owner.__name__}, {name!r}")
        owner.han_solo = Pray_I_Dont_Alter_It_Any_Further()
        
class Pray_I_Dont_Alter_It_Any_Further:
    def __set_name__(self, owner, name):
        print(f"__set_name__({self.__class__.__name__}, {owner.__name__}, {name!r}")

class Cloud_City:
    luke_skywalker = I_Am_Altering_The_Deal()

I would expect both __set_name__ functions to be called, resulting in the output:

__set_name__(I_Am_Altering_The_Deal, Cloud_City, 'luke_skywalker')
__set_name__(Pray_I_Dont_Alter_It_Any_Further, Cloud_City, 'han_solo')

But what is actually output, is:

__set_name__(I_Am_Altering_The_Deal, Cloud_City, 'luke_skywalker')

Discovered while testing a PEP-487 implementation for Micropython: micropython/micropython#15503.

Maybe be related to #72983.

CPython versions tested on:

3.8, 3.10, 3.12

Operating systems tested on:

Linux

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 đoạn mã tạo class của issue và so sánh đầu ra với hai lệnh gọi set_name dự kiến. Đọc phần triển khai PEP-487 và issue liên quan #72983; bản sửa lỗi hoàn tất khi một member được chèn trong lúc gọi set_name của một member khác nhận được callback dự kiến của chính nó, kèm theo kiểm thử hồi quy.

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ó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
35/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.