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 · 3 comments
Open

Support for attrs dataclasses #261

bilelomrani1 opened this issue Jun 6, 2023 · 3 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

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