Skip to content

What is the best way for me to check if an object is attrs decorated class? #537

@ethanjyx

Description

@ethanjyx

Say I have

import attr
@attr.s(kw_only=True, frozen=True)
class A:
    a: int = attr.ib()

obj = A(a=1)

Then how do I do something like isinstance(obj, attrs) to check if this class is attrs decorated?

it seems by playing with it we have

>> dir(obj)

['__annotations__',
 '__attrs_attrs__',
 '__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'a']

is it always correct to use '__attrs_attrs__' in dir(obj) to check whether obj belongs to a attrs decorated class?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions