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

Pydantic plugins - entry-point support #6820

Merged
merged 62 commits into from Sep 22, 2023
Merged

Pydantic plugins - entry-point support #6820

merged 62 commits into from Sep 22, 2023

Conversation

Kludex
Copy link
Member

@Kludex Kludex commented Jul 24, 2023

Change Summary

Implement a plugin system using Python entry points.

Related issue number

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Missing

I'll add them if everything is fine with the current implementation.

  • Tests
  • Documentation
  • Other places where we can have PluggableSchemaValidator

Selected Reviewer: @samuelcolvin

@linear
Copy link

linear bot commented Jul 24, 2023

PYD-181 Pydantic observability plugin

The idea is to support plugins from Pydantic, in a way we could "intercept", or "have hooks" on validation. For that, we intend to leverage the Entry Points Specification from Python.

As a reference, the two most known entry points are for flake8 and pytest.

The plugin needs to be agnostic i.e. it should just wrap the code we call the SchemaValidator.validate_*, which should allow pydantic_sdk to intercept the validation, and retrieve the data it wants.

The implementation will be "hidden" in a fork.

Questions

  1. Do we want to raise if the extra key doesn't have a plugin?

@cloudflare-pages
Copy link

cloudflare-pages bot commented Jul 24, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: e073168
Status: ✅  Deploy successful!
Preview URL: https://720a4185.pydantic-docs2.pages.dev
Branch Preview URL: https://kludex-pyd-181.pydantic-docs2.pages.dev

View logs

@Kludex Kludex requested a review from adriangb July 24, 2023 10:35
@Kludex
Copy link
Member Author

Kludex commented Jul 24, 2023

please review

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

otherwise I think this looks like a great start.

Obviously needs tests and documentation.

pydantic/_internal/_dataclasses.py Outdated Show resolved Hide resolved
pydantic/plugin/loader.py Outdated Show resolved Hide resolved
pydantic/plugin/loader.py Outdated Show resolved Hide resolved

def on_step(*args: Any, **kwargs: Any) -> None:
for plugin in plugins:
with contextlib.suppress(AttributeError, TypeError):
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure we should be suppressing these errors?

Copy link
Member Author

@Kludex Kludex Jul 24, 2023

Choose a reason for hiding this comment

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

Otherwise, I'll need more conditionals below to check if the steps are implemented, i.e. if on_event is not None and if step_func is not None. It's a bit more verbose.

What do you prefer?

pydantic/plugin/schema_validator.py Outdated Show resolved Hide resolved
@Kludex
Copy link
Member Author

Kludex commented Jul 24, 2023

@samuelcolvin Should I allow BaseModel.__init_subclass__ to receive keyword arguments on this PR?

@Kludex
Copy link
Member Author

Kludex commented Jul 25, 2023

@samuelcolvin Should I allow BaseModel.__init_subclass__ to receive keyword arguments on this PR?

I've talked to @davidhewitt. I'll accept other keyword arguments for __init_subclass__, and allow the plugin to customize itself depending on that.

@Kludex Kludex marked this pull request as draft July 25, 2023 09:43
@Kludex Kludex force-pushed the kludex/PYD-181 branch 2 times, most recently from 54a7612 to f7c32d1 Compare July 25, 2023 12:29
@Kludex Kludex marked this pull request as ready for review July 26, 2023 10:16
@Kludex
Copy link
Member Author

Kludex commented Jul 26, 2023

Only some tests missing here.

@Kludex
Copy link
Member Author

Kludex commented Jul 26, 2023

please review

@pydantic-hooky pydantic-hooky bot assigned davidhewitt and unassigned Kludex Jul 26, 2023
@samuelcolvin samuelcolvin changed the title Support pydantic entry point Pydantic plugin - entry point support Jul 26, 2023
@samuelcolvin samuelcolvin mentioned this pull request Sep 22, 2023
7 tasks
docs/usage/plugins.md Outdated Show resolved Hide resolved
docs/usage/plugins.md Outdated Show resolved Hide resolved
pydantic/main.py Show resolved Hide resolved
tests/test_plugins.py Show resolved Hide resolved
tests/test_plugins.py Show resolved Hide resolved
@lig lig enabled auto-merge (squash) September 22, 2023 16:18
@lig lig merged commit f120e7c into main Sep 22, 2023
58 checks passed
@lig lig deleted the kludex/PYD-181 branch September 22, 2023 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support plugins
5 participants