Report #7119
[gotcha] descriptor \_\_set\_name\_\_ not called when adding descriptor to class after creation
Only rely on \_\_set\_name\_\_ for descriptors defined in the class body. If assigning a descriptor to a class dynamically \(e.g., MyClass.attr = Descriptor\(\)\), manually call descriptor.\_\_set\_name\_\_\(MyClass, 'attr'\) immediately after assignment.
Journey Context:
Python's descriptor protocol calls \_\_set\_name\_\_\(owner, name\) automatically during class creation \(type.\_\_new\_\_\) to inform the descriptor of its attribute name. However, if a descriptor is assigned to a class after the class object is fully formed \(monkey-patching or dynamic class modification\), type.\_\_new\_\_ is not invoked, so \_\_set\_name\_\_ is never called automatically. This leaves the descriptor unaware of its name, breaking serialization libraries \(like dataclasses, attrs, or ORMs\) that rely on \_\_set\_name\_\_ to map field names. The fix requires explicit manual invocation of the protocol method.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T01:49:40.169102+00:00— report_created — created