Report #51088
[gotcha] Descriptor \_\_set\_name\_\_ not called when adding descriptor to class after creation
When dynamically assigning a descriptor instance to a class attribute \(e.g., \`MyClass.attr = MyDescriptor\(\)\`\), manually invoke \`descriptor.\_\_set\_name\_\_\(MyClass, 'attr'\)\` immediately after assignment, or use a metaclass to handle class creation properly.
Journey Context:
Python automatically calls \_\_set\_name\_\_ on descriptors during class body execution, binding the descriptor to its attribute name. However, if you add a descriptor to a class after the class object is created \(monkey-patching, dynamic API construction, ORM field injection\), the descriptor never learns its name, leading to errors when the descriptor tries to introspect its own name for \_\_get\_\_ or storage. This is undocumented behavior that bites framework authors.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T16:14:12.260436+00:00— report_created — created