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

Replace pydantic with mashumaro #740

Merged
merged 11 commits into from
Dec 9, 2024

Conversation

bvanelli
Copy link
Contributor

@bvanelli bvanelli commented Nov 24, 2024

Proposed Changes

This PR exchanges the pydantic dependency with mashumaro. This is due to the fact that the version of pydantic used is not anymore supported, and a migration was not accepted (see #531)

I've taken into account the following PRs from the author as references on how to do the migration:

Changelog

I had to to some unrelated changes to get the pipeline to run through, due to some library upgrades that now fail on CI. Since the content is not crazy big, I decided to add them to the same PR, but let me know if I should make a separate PR instead. Here is the list of changes:

  • Exchanged pydantic to mashumaro, like it was done on the other repositories

Discussion

As far as tests are concerned, the changes should work out of the box, but I do not have one of those devices to test it. If there is a way to have simulated APIs or there are test cases that should be covered, let me know.

Related Issues

For the pydantic upgrade in HA, this change is necessary:

@bvanelli bvanelli requested a review from frenck as a code owner November 24, 2024 16:08
@bvanelli bvanelli changed the title Remove pydantic dependency Replace pydantic with mashumaro Nov 24, 2024
@allenporter
Copy link

Changelog

I had to to some unrelated changes to get the pipeline to run through, due to some library upgrades that now fail on CI.
Since the content is not crazy big, I decided to add them to the same PR, but let me know if I should make a separate PR instead.

I'd definitely say make each of these changes into separate PRs for each change mentioned (CI fixes, async timeout, grammar/spelling fixes, linting fixes, etc)

@bvanelli
Copy link
Contributor Author

bvanelli commented Nov 30, 2024

I moved the unrelated changes to get the pipelines to run to other PRs:

Those can be made first and I'll rebase this branch once that is done. I left safety dependency removal in this PR because it's required to be removed for pydantic to be completely dropped.

@bvanelli
Copy link
Contributor Author

@frenck frenck added the refactor Improvement of existing code, not introducing new features. label Dec 8, 2024
Copy link
Owner

@frenck frenck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, for your work on this @bvanelli 👍

I've merged all split out PRs, and went over the project in certain places myself as well. CI on the main branch now passes again.

Can you rebase this PR on top of the latest main branch?

Thanks! 👍

../Frenck

@codecov-commenter
Copy link

codecov-commenter commented Dec 9, 2024

Codecov Report

Attention: Patch coverage is 92.92929% with 7 lines in your changes missing coverage. Please review.

Project coverage is 94.50%. Comparing base (53134cb) to head (2916c4e).
Report is 95 commits behind head on main.

Files with missing lines Patch % Lines
src/demetriek/models.py 94.25% 2 Missing and 3 partials ⚠️
src/demetriek/device.py 77.77% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #740      +/-   ##
==========================================
+ Coverage   93.73%   94.50%   +0.76%     
==========================================
  Files           6        6              
  Lines         383      400      +17     
  Branches       35       23      -12     
==========================================
+ Hits          359      378      +19     
+ Misses         22       17       -5     
- Partials        2        5       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bvanelli
Copy link
Contributor Author

bvanelli commented Dec 9, 2024

@frenck did the rebase, everything looks nice except the poetry file that has some references to my own environent after running poetry lock --no-update to remove the dependency. You can also replace it on merge.

Let me know if I should change something else 👍

@frenck
Copy link
Owner

frenck commented Dec 9, 2024

Thanks, @bvanelli! 👍 I will take a look...

Copy link
Owner

@frenck frenck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be a serialization issue that is still left, the example provided in this repository doesn't work right now:

CleanShot 2024-12-09 at 17 04 40@2x

src/demetriek/models.py Show resolved Hide resolved
@frenck frenck marked this pull request as draft December 9, 2024 16:06
@frenck
Copy link
Owner

frenck commented Dec 9, 2024

With the last change in place, it would only omit None values from the top level notification object, and not its contents, right?

@bvanelli
Copy link
Contributor Author

bvanelli commented Dec 9, 2024

With the last change in place, it would only omit None values from the top level notification object, and not its contents, right?

Yes, that is correct, I did that based on the docs request, and there it does not seem clear to me that those sub-fields are optional, for example, for Model.sound:

object that describes the notification sound to play when notification pops on the LaMetric Time’s screen

Fields from the Notification have the clear Optional keyword, but not this one, so I guess they are not optional on the request.

I did not move to review again since there is another issue. It seems like __pre_deserialize__ does not work the same way as the root validator does, since it does only run when desserializing, but not when directly instancing the object. I'm currently looking for a way to do that.

@bvanelli
Copy link
Contributor Author

bvanelli commented Dec 9, 2024

I'm currently looking for a way to do that.

Never mind, I just realized I used the wrong validation, I should have used dataclasses __post_init__ instead.

@frenck frenck marked this pull request as ready for review December 9, 2024 19:26
@bvanelli
Copy link
Contributor Author

bvanelli commented Dec 9, 2024

@frenck Thanks for reviewing and noticing my mistake on the serialization. I have changed the __pre_deserialize__ to __post_init__ since Sound is only instantiated directly.

Regarding the omit optional fields, I have not changed the behaviour for the inner objects. Since you have more experience with the API, should that be the case? That is affectin:

  • Model.sound
  • Goal.icon
  • GoalData.icon
  • Simple.icon

and from what I understood from the documentation none of them are option.

@frenck
Copy link
Owner

frenck commented Dec 9, 2024

Awesome 👍

Just tested it against real devices, it seems to work as expected 👍

Will push up some nitpick sorting we can do now and continue on merging this one in.

../Frenck

Copy link
Owner

@frenck frenck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @bvanelli 👍

../Frenck

@frenck frenck merged commit 60c1ab9 into frenck:main Dec 9, 2024
13 checks passed
@bvanelli bvanelli deleted the remove-pydantic-dependency branch December 9, 2024 20:23
@github-actions github-actions bot locked and limited conversation to collaborators Dec 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
refactor Improvement of existing code, not introducing new features.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants