-
Notifications
You must be signed in to change notification settings - Fork 83
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
Improvements to unify the functionality from decorator & Dataclass
in a single class
#2764
Conversation
The intention is to avoid having both the `dataclass` decorator and the `Dataclass` which feels a bit redundant.
e125189
to
c1b7e9d
Compare
LGTM! |
@eos87 How does this work with the IDE predictions/suggestions when typing field names? |
It does. This is vscode below. I would assume it would also work on PyCharm and others. |
Testing this locally, and I found 2 things (1 of those is probably an issue with my original code).
from superdesk.core.resources import Dataclass
class Person(Dataclass):
name: str
assert type(Person(name="Monkeys")) == Person # True
assert type(Person.from_dict(dict(name="Monkeys"))) == Person # False
assert type(Person.from_dict(dict(name="Monkeys"))) == dict # True
assert type(Person.from_json('{"name": "Monkeys"}')) == Person # False
assert type(Person.from_json('{"name": "Monkeys"}')) == dict # False
Person(name="Monkeys") # mypy error: Unexpected keyword argument "name" for "Person" [call-arg] Might be good to add some tests with using this new datatype of ours |
yeah, it was already a dict indeed. I'll take a look to return the proper model instance.
interesting, I've just tried this locally and it worked for me. Maybe we have a different version of mypy? |
Improvements added so editor will autocomplete the attributes inline when creating an instance of a class. Also added some tests to double check the functionality of the utility methods |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a good improvement, thanks mate
Purpose
The intention to avoid needing to have both a
Dataclass
and a@dataclass
decoratorWhat has changed
DataclassBase
for reusable utility methods (to_dict
,from_dict
,ser_model
).DataclassMeta
to handle automatic Pydantic dataclass wrapping and customConfig
merging.Config
dictionary in child classes.Example usage
@MarkLark86 following up on our conversation here what do you think? If you think it looks good I'd merge it and replace the usages of the decorator. Also extend the documentation a little bit to make it clear what is the intention.
Thanks for checking!