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 jsonschema with pydantic #65

Closed
simskij opened this issue Jun 20, 2022 · 6 comments
Closed

Replace jsonschema with pydantic #65

simskij opened this issue Jun 20, 2022 · 6 comments

Comments

@simskij
Copy link
Member

simskij commented Jun 20, 2022

The traefik libraries would be excellent candidates for trying out replacing jsonschema with pydantic. According to lessons learned by the charmcraft team, pydantic plays a lot nicer with more complex data structures, is faster in the general use case and has better error logging.

In addition to the swap, it would be excellent if we generated the pydantic model from the json schemas of the interface spec.

@jnsgruk
Copy link
Member

jnsgruk commented Jun 20, 2022

I don't disagree here, but we should be cautious merging, as it will essentially be a breaking change due to changing the external requirements of the lib :) I'm interested to see the diff in the code though.

@simskij
Copy link
Member Author

simskij commented Jun 20, 2022

I don't disagree here, but we should be cautious merging, as it will essentially be a breaking change due to changing the external requirements of the lib :) I'm interested to see the diff in the code though.

Agreed. Given that @PietroPasotti is working on v1 of the libs (#63), which essentially will be incompatible regardless, it feels like it could be a good time to do both these changes at the same time to not have break the compatibility twice. :)

@PietroPasotti
Copy link
Contributor

I like the idea! I was thinking to combine this with #38
And also in reference to canonical/charm-relation-interfaces#7 I'm thinking this would definitely settle the yaml/json score (pydantic has builtin json converters I believe) So dumping data is as easy as model.json().

I'm thinking about the last line you added; what do you reckon would the user story look like?

  • I want to implement an ingress Requirer.
  • I go to charm-relation-interfaces, grab the jsonschema spec
  • run the spec through our lib-provided tool jsonschema_to_pydantic /path/to/jsonschema/file.json, copy the result into the new requirer file.
  • Profit?

@simskij
Copy link
Member Author

simskij commented Jun 20, 2022

I'm thinking we'd use the datamodel-code-generator, probably as part of the CI of the relation interfaces repo itself, and that users then would be able to download the pydantic model straight from github and use it in their projects.

@PietroPasotti
Copy link
Contributor

@jnsgruk @simskij see #66

@dstathis
Copy link
Contributor

dstathis commented Nov 7, 2024

We are already using pydantic.

@dstathis dstathis closed this as completed Nov 7, 2024
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

4 participants