attrs icon indicating copy to clipboard operation
attrs copied to clipboard

Adding runtime_checkable to AttrsInstance

Open ryan-minato opened this issue 11 months ago • 1 comments

Is it possible to add the runtime_checkable decorator to AttrsInstance protocol?

https://github.com/python-attrs/attrs/blob/598494a618410490cfbe0c896b7a544f6d23e0d9/src/attr/init.py#L37-L38

In my project, I am working on a decorator that automates converting a dict into an AttrsInstance. To perform reflection checks on input definitions, I found myself needing to subclass AttrsInstance without modifying it—just to add the runtime_checkable decorator. It would be incredibly helpful if the attrs library's definition already included runtime_checkable.

ryan-minato avatar Dec 23 '24 02:12 ryan-minato

As @Tinche commented on your PR, this is not trivial, because runtime_checkable checks whether an instance fulfills the Protocol you defined and since AttrsInstance is empty, every class will conform.

Maybe it's possible if we switch to an abc with a dedicated __instancecheck__?

hynek avatar Jan 12 '25 05:01 hynek