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

IsDataclass type #67

Closed
FBruzzesi opened this issue Apr 28, 2023 · 4 comments
Closed

IsDataclass type #67

FBruzzesi opened this issue Apr 28, 2023 · 4 comments

Comments

@FBruzzesi
Copy link
Contributor

I would be happy to work on IsDataclass (related to #3).

Before starting I have a few questions:

  • Should it be in the _other module?
  • Any drawback in using dataclasses.is_dataclass?
  • I think it would be good to have the option to distinguish between class definition and instance, but glad to get a feedback on this.
@samuelcolvin
Copy link
Owner

Yes please, module up to you.

dataclasses.is_dataclass should be fine.

If you want IsDataClassType I think that should be a separate type, but you could add it at the same time.

Another question, should IsDataclass look at fields, so you can do IsDataclass(foo=4, bar=5)? Then do we need IsPartialDataclass?

@FBruzzesi
Copy link
Contributor Author

If you want IsDataClassType I think that should be a separate type, but you could add it at the same time.

I had in mind an argument for it, but I guess having a different class itself is much more explicit.

Another question, should IsDataclass look at fields, so you can do IsDataclass(foo=4, bar=5)? Then do we need IsPartialDataclass?

Wouldn't this just come down to the following check(s)?

from dirty_equals import IsDict, IsPartialDict

instance.__dict__ == IsDict(foo=4, bar=5)
# or 
instance.__dict__ == IsPartialDict(foo=4)

@samuelcolvin
Copy link
Owner

Or HasAttributes, still I think it would be a useful addition.

@FBruzzesi
Copy link
Contributor Author

FBruzzesi commented Apr 28, 2023

What do you think about the following case:

@dataclass
class Foo:
    a: int
    b: int

foo = Foo(a=1, b=2)

foo == IsDataclass # -> True
foo == IsDataclass(a=1, b=2) # -> True
foo == IsDataclass(a=1) # -> False

foo == IsDataclass() # -> ???

(Current implementation I have makes it True)

@FBruzzesi FBruzzesi mentioned this issue Apr 29, 2023
samuelcolvin pushed a commit that referenced this issue May 2, 2023
Addresses #67 by introducing the following types:

- `IsDataclassType`: checks if type is dataclass.
- `IsDataclass`: checks if instance is dataclass, and possibly checks
exact matching of attribute values using `IsDict`.
- `IsPartialDataclass`: checks if instance is dataclass, and possibly
checks partial matching of attribute values using `IsPartialDict`.
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

2 participants