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

Support for attrs dataclasses #261

Open
bilelomrani1 opened this issue Jun 6, 2023 · 5 comments
Open

Support for attrs dataclasses #261

bilelomrani1 opened this issue Jun 6, 2023 · 5 comments

Comments

@bilelomrani1
Copy link

attrs is a popular third-party package for defining dataclasses with additional convenient features (converters, more fine-grained initialization, etc.). Currently simple-parsing only supports native dataclasses, is there any interest to support attrs dataclasses as well?

@lebrice
Copy link
Owner

lebrice commented Jun 6, 2023

Hello there @bilelomrani1, thanks for posting!

Hmm that's interesting. Does attrs have an API that is interchangeable with the built-in dataclasses?

For parser.add_arguments(<attrs.dataclass>, dest="foo") to work, what we'd need are equivalents to:

  • dataclasses.fields(dataclass_type_or_instance) -> Iterable[dataclasses.Field]
  • dataclasses.is_dataclass(dataclass) -> bool
  • dataclasses.Field attributes:
    • name: str
    • type: str | type
    • default: Any | dataclasses.MISSING
    • default_factory: Callable[[], Any] | dataclasses.MISSING
    • metadata: dict

If there's a way of get this information from the attrs.dataclass, then yeah, this would be feasible, nice contribution! :)

@lebrice
Copy link
Owner

lebrice commented Aug 21, 2023

Hey @bilelomrani1, do you know if attrs has this kind of interop with dataclasses ?

@bilelomrani1
Copy link
Author

bilelomrani1 commented Sep 1, 2023

Hi @lebrice I'm sorry for the long delay. I no longer need this feature as I found an existing alternative for my use case. But I can still provide some answers if you are still interested in this feature, I once wrote a dataclasses/attrs parser some time ago, the code is available here.
Looking at it, here is what I used:

  • attr.fields(attr_type): list of attrs fields, containing the field name, type, factory/default value.
  • attr.has(attr_type) --> bool: equivalent of dataclasses.is_dataclass(dataclass) -> bool

@chicham
Copy link

chicham commented Nov 20, 2024

Hi @lebrice,

I'm interested in this integration too!

For your questions:

Hmm that's interesting. Does attrs have an API that is interchangeable with the built-in dataclasses?

Yes, absolutely!

dataclasses.fields(dataclass_type_or_instance) -> Iterable[dataclasses.Field]

attrs.fields(cls) -> tuple[attrs.Attribute, ...]

dataclasses.is_dataclass(dataclass) -> bool

attrs.has(cls) -> bool

dataclasses.Field attributes:
name: str
type: str | type
default: Any | dataclasses.MISSING
default_factory: Callable[[], Any] | dataclasses.MISSING
metadata: dict

https://www.attrs.org/en/stable/api.html#attrs.Attribute

For the record, the dataclass module is heavily inspired by the attrs library, and attrs is usually more featured with a very close API.

I'd be happy to help explore this further! What are your thoughts?

@lebrice
Copy link
Owner

lebrice commented Nov 20, 2024

Sure, we can add support for attrs dataclasses. A first step could probably be to replace all the uses of the functions from dataclasses with generic functions with the same name, that dispatch either to the attrs function or dataclasses function depending on the class /field type.

If you're willing to help with that, I'd be happy to give some pointers or feedback!

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

3 participants