-
-
Notifications
You must be signed in to change notification settings - Fork 45
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
Add sort keys option for dataclasses inheriting from mixin #157
Conversation
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.
There is one minor but everything else is fine.
I've only added the feature to Mixin subclasses for now, but not for arbitrary dataclasses
It will also work for arbitrary dataclasses:
@dataclass
class Foo:
b: int
a: int
@dataclass
class Bar(DataClassDictMixin):
z: int
foo: Foo
bar = Bar(z=3, foo=Foo(b=2, a=1))
print(bar.to_dict()) # {'foo': {'a': 1, 'b': 2}, 'z': 3}
Would you like to add such a case to the tests since you mentioned it?
add plain dataclass test remove list in builder
d30b1b6
to
7521abc
Compare
now pulled and rebased. added a test + added a README |
@mishamsk Thank you for contribution, I'm merging it now. |
glad to help! |
I just realised that I was wrong. Config options are not propagated to inner dataclasses by design, and the tests didn't show it because two dictionaries with different key order are equal. I created the pull request to fix the tests. |
@Fatal1ty ouch. sorry. my bad! |
Hi,
Thought this is small enough to directly open a PR and see if you would support this feature.
What: add a
sort_keys
option forBaseConfig
which would efficiently generate serialization code such that keys are sorted on creation (which in many cases will make much heavier encoder approach unnecessary).What's missing:
Rationale:
I am using key sort (via
__post_serialize__
hook) a lot in order to maintain stable serialized view of my objects (those pesky trees). This would make it more efficient (depending on how classes are defined of course).