Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex committed Jul 25, 2023
1 parent 2c58afa commit 92b2bce
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 18 deletions.
12 changes: 8 additions & 4 deletions docs/integrations/plugins.md
Expand Up @@ -16,15 +16,19 @@ The entry point group is `pydantic`, and the name of the entry point is the name

Plugins are loaded in the order they are found, and the order they are found is not guaranteed.

Consider that you have a plugin called `pydantic_plugin`, then you can use it like this:
As a user, you can modify the behavior of the plugin in a `BaseModel` using the `plugin_settings`
class keyword argument. This argument takes a dictionary of settings that will be passed to the
plugin. The plugin can then use these settings to modify its behavior.

```py test="skip"
from pydantic import BaseModel


class Foo(BaseModel, pydantic_plugin='all'):
class Foo(BaseModel, plugin_settings=dict(observe='all')):
...
```

On each validation call, a callable registered for the event `all` will be called with the
instance of `Foo`, the event name, and the validation result.
## Build a plugin

??? api "API Documentation"
[`pydantic.plugin.plugin`][pydantic.plugin.plugin]<br>
12 changes: 12 additions & 0 deletions docs/usage/validators.md
@@ -1,6 +1,18 @@

## Annotated Validators

??? api "API Documentation"
[`pydantic.functional_validators.WrapValidator`][pydantic.functional_validators.WrapValidator]<br>

??? api "API Documentation"
[`pydantic.functional_validators.PlainValidator`][pydantic.functional_validators.PlainValidator]<br>

??? api "API Documentation"
[`pydantic.functional_validators.BeforeValidator`][pydantic.functional_validators.BeforeValidator]<br>

??? api "API Documentation"
[`pydantic.functional_validators.AfterValidator`][pydantic.functional_validators.AfterValidator]<br>

Pydantic provides a way to apply validators via use of `Annotated`.
You should use this whenever you want to bind validation to a type instead of model or field.

Expand Down
4 changes: 2 additions & 2 deletions pydantic/_internal/_dataclasses.py
Expand Up @@ -14,7 +14,7 @@

from ..errors import PydanticUndefinedAnnotation
from ..fields import FieldInfo
from ..plugin.schema_validator import schema_validator_cls
from ..plugin.schema_validator import create_schema_validator
from ..warnings import PydanticDeprecatedSince20
from . import _config, _decorators, _discriminated_union, _typing_extra
from ._core_utils import collect_invalid_schemas, flatten_schema_defs, inline_schema_defs
Expand Down Expand Up @@ -163,7 +163,7 @@ def __init__(__dataclass_self__: PydanticDataclass, *args: Any, **kwargs: Any) -
# debug(schema)
cls.__pydantic_core_schema__ = schema = _discriminated_union.apply_discriminators(flatten_schema_defs(schema))
simplified_core_schema = inline_schema_defs(schema)
cls.__pydantic_validator__ = validator = schema_validator_cls()(
cls.__pydantic_validator__ = validator = create_schema_validator(
simplified_core_schema, core_config, config_wrapper.plugin_settings
)
cls.__pydantic_serializer__ = SchemaSerializer(simplified_core_schema, core_config)
Expand Down
4 changes: 2 additions & 2 deletions pydantic/_internal/_model_construction.py
Expand Up @@ -14,7 +14,7 @@

from ..errors import PydanticUndefinedAnnotation, PydanticUserError
from ..fields import Field, FieldInfo, ModelPrivateAttr, PrivateAttr
from ..plugin.schema_validator import schema_validator_cls
from ..plugin.schema_validator import create_schema_validator
from ..warnings import PydanticDeprecatedSince20
from ._config import ConfigWrapper
from ._core_utils import collect_invalid_schemas, flatten_schema_defs, inline_schema_defs
Expand Down Expand Up @@ -479,7 +479,7 @@ def complete_model_class(
# debug(schema)
cls.__pydantic_core_schema__ = schema
simplified_core_schema = inline_schema_defs(schema)
cls.__pydantic_validator__ = schema_validator_cls()(
cls.__pydantic_validator__ = create_schema_validator(
simplified_core_schema, core_config, config_wrapper.plugin_settings
)
cls.__pydantic_serializer__ = SchemaSerializer(simplified_core_schema, core_config)
Expand Down
4 changes: 2 additions & 2 deletions pydantic/_internal/_validate_call.py
Expand Up @@ -8,7 +8,7 @@
import pydantic_core

from ..config import ConfigDict
from ..plugin.schema_validator import schema_validator_cls
from ..plugin.schema_validator import create_schema_validator
from . import _discriminated_union, _generate_schema, _typing_extra
from ._config import ConfigWrapper
from ._core_utils import flatten_schema_defs, inline_schema_defs
Expand Down Expand Up @@ -66,7 +66,7 @@ def __init__(self, function: Callable[..., Any], config: ConfigDict | None, vali
core_config = config_wrapper.core_config(self)
schema = _discriminated_union.apply_discriminators(flatten_schema_defs(schema))
simplified_schema = inline_schema_defs(schema)
self.__pydantic_validator__ = schema_validator_cls()(
self.__pydantic_validator__ = create_schema_validator(
simplified_schema, core_config, config_wrapper.plugin_settings
)

Expand Down
5 changes: 4 additions & 1 deletion pydantic/plugin/plugin.py
@@ -1,4 +1,7 @@
"""Plugin interface for Pydantic plugins, and related types."""
"""Usage docs: https://docs.pydantic.dev/dev-v2/integrations/plugins#build-a-plugin
Plugin interface for Pydantic plugins, and related types.
"""
from __future__ import annotations

from abc import abstractmethod
Expand Down
16 changes: 11 additions & 5 deletions pydantic/plugin/schema_validator.py
Expand Up @@ -15,14 +15,18 @@
R = TypeVar('R')


def schema_validator_cls() -> type[SchemaValidator]:
def create_schema_validator(
schema: CoreSchema, config: CoreConfig | None = None, plugin_settings: dict[str, Any] | None = None
) -> SchemaValidator:
"""Get the schema validator class.
Returns:
type[SchemaValidator]: If plugins are installed then return `PluggableSchemaValidator`,
otherwise return `SchemaValidator`.
"""
return PluggableSchemaValidator if plugins else SchemaValidator # type: ignore
if plugins:
return PluggableSchemaValidator(schema, config, plugin_settings) # type: ignore
return SchemaValidator(schema, config)


class _Plug:
Expand Down Expand Up @@ -99,10 +103,12 @@ def gather_calls(
class PluggableSchemaValidator:
"""Pluggable schema validator."""

def __init__(self, schema: CoreSchema, config: CoreConfig | None = None, _ignored: Any = None) -> None:
self.schema_validator = SchemaValidator(schema, config, _ignored)
def __init__(
self, schema: CoreSchema, config: CoreConfig | None = None, plugin_settings: dict[str, Any] | None = None
) -> None:
self.schema_validator = SchemaValidator(schema, config)

self.plug = _Plug(schema=schema, config=config, plugin_settings=_ignored)
self.plug = _Plug(schema, config, plugin_settings)

self.validate_json = self.plug(self.schema_validator.validate_json)
self.validate_python = self.plug(self.schema_validator.validate_python)
Expand Down
4 changes: 2 additions & 2 deletions pydantic/type_adapter.py
Expand Up @@ -20,7 +20,7 @@
JsonSchemaMode,
JsonSchemaValue,
)
from .plugin.schema_validator import schema_validator_cls
from .plugin.schema_validator import create_schema_validator

T = TypeVar('T')

Expand Down Expand Up @@ -175,7 +175,7 @@ def __init__(self, type: Any, *, config: ConfigDict | None = None, _parent_depth
try:
validator = _getattr_no_parents(type, '__pydantic_validator__')
except AttributeError:
validator = schema_validator_cls()(simplified_core_schema, core_config, config_wrapper.plugin_settings)
validator = create_schema_validator(simplified_core_schema, core_config, config_wrapper.plugin_settings)

serializer: SchemaSerializer
try:
Expand Down

0 comments on commit 92b2bce

Please sign in to comment.