Skip to content

Commit

Permalink
fix:refactored pydantic extension registration (#1152)
Browse files Browse the repository at this point in the history
* refactored pydantic extension registration

* delete data_quality/pydantic_validators, content moved to plugins/pydantic_extensions

* added pydantic 2.0 requirements to docstrings

---------

Co-authored-by: zilto <tjean@DESKTOP-V6JDCS2>
  • Loading branch information
zilto and zilto authored Sep 23, 2024
1 parent c6dcd00 commit 1365e88
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 27 deletions.
17 changes: 0 additions & 17 deletions hamilton/data_quality/default_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,23 +508,6 @@ def _append_pandera_to_default_validators():
_append_pandera_to_default_validators()


def _append_pydantic_to_default_validators():
"""Utility method to append pydantic validators as needed"""
try:
import pydantic # noqa: F401
except ModuleNotFoundError:
logger.debug(
"Cannot import pydantic from pydantic_validators. Run pip install sf-hamilton[pydantic] if needed."
)
return
from hamilton.data_quality import pydantic_validators

AVAILABLE_DEFAULT_VALIDATORS.extend(pydantic_validators.PYDANTIC_VALIDATORS)


_append_pydantic_to_default_validators()


def resolve_default_validators(
output_type: Type[Type],
importance: str,
Expand Down
6 changes: 3 additions & 3 deletions hamilton/plugins/h_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def __init__(
importance: str = dq_base.DataValidationLevel.WARN.value,
target: fm_base.TargetType = None,
):
"""Specific output-checker for pydantic models. This decorator utilizes the output type of
the function, which can be any subclass of pydantic.BaseModel. The function output must
be declared with a type hint.
"""Specific output-checker for pydantic models (requires ``pydantic>=2.0``).
This decorator utilizes the output type of the function, which can be any subclass of pydantic.BaseModel.
The function output must be declared with a type hint.
:param model: The pydantic model to use for validation. If this is not provided, then the output type of the function is used.
:param importance: Importance level (either "warn" or "fail") -- see documentation for check_output for more details.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
from typing import Any, Type

from pydantic import BaseModel, TypeAdapter, ValidationError

from hamilton.data_quality import base
from hamilton.data_quality import base, default_validators
from hamilton.htypes import custom_subclass_check

try:
import pydantic # noqa: F401
except ModuleNotFoundError as e:
raise NotImplementedError(
"Cannot import `pydantic` from `pydantic_validators`. Run pip install 'sf-hamilton[pydantic]' if needed."
) from e

try:
from pydantic import BaseModel, TypeAdapter, ValidationError
except ImportError as e:
raise NotImplementedError(
"`pydantic>=2.0` required to use `pydantic_validators`. Run pip install 'sf-hamilton[pydantic]' if needed."
) from e


COLUMN_FRIENDLY_DF_TYPE = False


class PydanticModelValidator(base.BaseDefaultValidator):
"""Pydantic model compatibility validator
"""Pydantic model compatibility validator (requires ``pydantic>=2.0``)
Note that this validator uses pydantic's strict mode, which does not allow for
coercion of data. This means that if an object does not exactly match the reference
Expand Down Expand Up @@ -57,4 +72,10 @@ def name(cls) -> str:
return "pydantic_validator"


PYDANTIC_VALIDATORS = [PydanticModelValidator]
def register_validators():
"""Utility method to append pydantic validators as needed"""
validators = [PydanticModelValidator]
default_validators.AVAILABLE_DEFAULT_VALIDATORS.extend(validators)


register_validators()
1 change: 1 addition & 0 deletions hamilton/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"kedro",
"huggingface",
"mlflow",
"pydantic",
]
HAMILTON_EXTENSIONS: Tuple[ExtensionName, ...] = get_args(ExtensionName)
HAMILTON_AUTOLOAD_ENV = "HAMILTON_AUTOLOAD_EXTENSIONS"
Expand Down
1 change: 0 additions & 1 deletion tests/integrations/pydantic/requirements.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import pytest
from pydantic import BaseModel, ValidationError

from hamilton.data_quality.pydantic_validators import PydanticModelValidator
from hamilton.function_modifiers import check_output
from hamilton.node import Node
from hamilton.plugins import h_pydantic
from hamilton.plugins.pydantic_extensions import PydanticModelValidator


def test_basic_pydantic_validator_passes():
Expand Down

0 comments on commit 1365e88

Please sign in to comment.