Skip to content

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

@AJMansfield

Description

@AJMansfield

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy