Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ethanjyx opened this issue May 31, 2019 · 7 comments
Closed

Comments

@ethanjyx
Copy link

ethanjyx commented May 31, 2019

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?

@hynek
Copy link
Member

hynek commented Jun 1, 2019

Check out attr.has(A)

@ethanjyx ethanjyx closed this as completed Jun 5, 2019
@brendan-simon-indt
Copy link

brendan-simon-indt commented Aug 25, 2022

That checks if the CLASS in an attrs decorated class.
The OP asked how to tell if an INSTANCE is an attrs class instance.

I tried this.

`isinstance( obj, AttrsInstance )`

but mypy gave an error:

Only @runtime_checkable protocols can be used with instance and class checks

This seems to keep mypy happy.

if attr.has( obj.__class__ ) :
    pass

@brendan-simon-indt
Copy link

brendan-simon-indt commented Aug 25, 2022

Actually attr.has( obj ) does work, but mypy doesn't like it because it expects a type argument.

Should the argument type be Union[ type, object ] or type | object or just object ?

@hynek
Copy link
Member

hynek commented Aug 25, 2022

I suspect this might have been fixed by #997?

@brendan-simon-indt
Copy link

Possibly. I'm using Debian 10 Buster, which has Python 3.7 installed and attrs 22.1.0 (in a venv).

Looks like a recent merge, so maybe the next attrs release will fix this. Will it fix it for older pythons too?

@hynek
Copy link
Member

hynek commented Aug 25, 2022

according to #997 (comment) it should

@gtpalmer
Copy link

attrs.has is insufficient. For example

@attrs.define
class Foo:
  foo: str

class Bar(Foo):
  bar: int

print(attrs.has(Bar)) # Prints True, but Bar does not have an attrs decorator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants