Report #17819
[gotcha] Descriptor \_\_set\_name\_\_ not called when monkey-patching classes / dynamic descriptor attachment
When assigning descriptors to existing classes \(e.g., MyClass.attr = MyDescriptor\(\)\), manually invoke descriptor.\_\_set\_name\_\_\(MyClass, 'attr'\) to trigger the protocol, or subclass type to override \_\_setattr\_\_.
Journey Context:
PEP 487 introduced \_\_set\_name\_\_ so descriptors know their attribute name during class creation. However, this protocol only fires during type.\_\_new\_\_ execution \(class body evaluation\). When descriptors are attached after class creation—common in ORMs, serializers, or dynamic API construction—the descriptor remains anonymous, breaking code that relies on self.name for storage keys or lookups. Manual invocation of the protocol is required to maintain consistency.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T06:25:32.946572+00:00— report_created — created