Skip to content

Pydantic plugins - entry-point support#6820

Merged
lig merged 62 commits intomainfrom
kludex/PYD-181
Sep 22, 2023
Merged

Pydantic plugins - entry-point support#6820
lig merged 62 commits intomainfrom
kludex/PYD-181

Conversation

@Kludex
Copy link
Copy Markdown
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
Copy Markdown

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-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-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
Copy Markdown
Member Author

Kludex commented Jul 24, 2023

please review

Copy link
Copy Markdown
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.

Comment thread pydantic/_internal/_dataclasses.py Outdated
Comment thread pydantic/plugin/loader.py Outdated
Comment thread pydantic/plugin/loader.py Outdated
Comment thread pydantic/plugin/schema_validator.py Outdated

def on_step(*args: Any, **kwargs: Any) -> None:
for plugin in plugins:
with contextlib.suppress(AttributeError, TypeError):
Copy link
Copy Markdown
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
Copy Markdown
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?

Comment thread pydantic/plugin/schema_validator.py Outdated
@pydantic-hooky pydantic-hooky Bot added awaiting author revision awaiting changes from the PR author and removed ready for review labels Jul 24, 2023
@pydantic-hooky pydantic-hooky Bot assigned Kludex and unassigned davidhewitt Jul 24, 2023
@Kludex
Copy link
Copy Markdown
Member Author

Kludex commented Jul 24, 2023

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

@Kludex
Copy link
Copy Markdown
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
Copy Markdown
Member Author

Kludex commented Jul 26, 2023

Only some tests missing here.

@Kludex
Copy link
Copy Markdown
Member Author

Kludex commented Jul 26, 2023

please review

@pydantic-hooky pydantic-hooky Bot added ready for review and removed awaiting author revision awaiting changes from the PR author labels Jul 26, 2023
@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
Comment thread docs/usage/plugins.md Outdated
Comment thread docs/usage/plugins.md Outdated
Comment thread pydantic/main.py
Comment thread tests/test_plugins.py
Comment thread tests/test_plugins.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting author revision awaiting changes from the PR author relnotes-feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support plugins

5 participants