From f2c085b4768069351fbd92cb994a31ac60f40fb1 Mon Sep 17 00:00:00 2001 From: Anton Agestam Date: Sun, 17 Dec 2023 15:31:52 +0100 Subject: [PATCH] feature: Support pydantic-core 2.10+ pydantic-core 2.10 deprecated some functions in the `pydantic_core.core_schema.*` namespace. In order to both be compatible with future (no deprecation errors), and have working type checking, there's no choice but to drop support for pydantic-core versions <2.10. Because this drops support for some pydantic-core versions, it is a breaking change. --- .pre-commit-config.yaml | 3 ++- setup.cfg | 3 +++ src/immoney/_pydantic.py | 31 +++++++++++++++++++------------ 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9006ecd..e1986ec 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -67,7 +67,8 @@ repos: - abcattrs==0.3.2 - typing-extensions==4.6.3 - hypothesis==6.54.4 - - pydantic==2.0b3 + - pydantic==2.5.2 + - pydantic-core==2.14.5 - types-babel==2.11.0.15 - repo: https://github.com/pre-commit/mirrors-prettier rev: "v2.7.1" diff --git a/setup.cfg b/setup.cfg index b76de54..4fb52de 100644 --- a/setup.cfg +++ b/setup.cfg @@ -40,6 +40,9 @@ immoney = py.typed [options.extras_require] pydantic = pydantic>=2.0.3 + # 2.10 deprecates some functions under pydantic.core_schema.*, forcing dropping + # support for prior versions. + pydantic-core>=2.10 babel = babel>=2.12.1 diff --git a/src/immoney/_pydantic.py b/src/immoney/_pydantic.py index 0a2a002..6b65b17 100644 --- a/src/immoney/_pydantic.py +++ b/src/immoney/_pydantic.py @@ -10,7 +10,6 @@ from typing import get_args from pydantic_core import core_schema -from pydantic_core.core_schema import GeneralValidatorFunction from . import Currency from . import Money @@ -86,12 +85,14 @@ def schema(currency_schema: core_schema.CoreSchema) -> core_schema.CoreSchema: @abc.abstractmethod def validator_from_registry( registry: CurrencyRegistry[Currency], - ) -> GeneralValidatorFunction: + ) -> core_schema.WithInfoValidatorFunction: ... @staticmethod @abc.abstractmethod - def validator_from_currency(currency: Currency) -> GeneralValidatorFunction: + def validator_from_currency( + currency: Currency, + ) -> core_schema.WithInfoValidatorFunction: ... @@ -124,7 +125,7 @@ def schema(currency_schema: core_schema.CoreSchema) -> core_schema.CoreSchema: @staticmethod def validator_from_registry( registry: CurrencyRegistry[C], - ) -> GeneralValidatorFunction: + ) -> core_schema.WithInfoValidatorFunction: def validate_money( value: MoneyDict | Money[Currency], *args: object, @@ -140,7 +141,9 @@ def validate_money( return validate_money @staticmethod - def validator_from_currency(currency: Currency) -> GeneralValidatorFunction: + def validator_from_currency( + currency: Currency, + ) -> core_schema.WithInfoValidatorFunction: def validate_money( value: MoneyDict | Money[Currency], *args: object, @@ -200,7 +203,7 @@ def schema(currency_schema: core_schema.CoreSchema) -> core_schema.CoreSchema: @staticmethod def validator_from_registry( registry: CurrencyRegistry[C], - ) -> GeneralValidatorFunction: + ) -> core_schema.WithInfoValidatorFunction: def validate_subunit_fraction( value: SubunitFractionDict | SubunitFraction[Currency], *args: object, @@ -217,7 +220,9 @@ def validate_subunit_fraction( return validate_subunit_fraction @staticmethod - def validator_from_currency(currency: Currency) -> GeneralValidatorFunction: + def validator_from_currency( + currency: Currency, + ) -> core_schema.WithInfoValidatorFunction: def validate_subunit_fraction( value: SubunitFractionDict | SubunitFraction[Currency], *args: object, @@ -268,7 +273,7 @@ def schema(currency_schema: core_schema.CoreSchema) -> core_schema.CoreSchema: @staticmethod def validator_from_registry( registry: CurrencyRegistry[C], - ) -> GeneralValidatorFunction: + ) -> core_schema.WithInfoValidatorFunction: def validate_overdraft( value: OverdraftDict | Overdraft[Currency], *args: object, @@ -284,7 +289,9 @@ def validate_overdraft( return validate_overdraft @staticmethod - def validator_from_currency(currency: Currency) -> GeneralValidatorFunction: + def validator_from_currency( + currency: Currency, + ) -> core_schema.WithInfoValidatorFunction: def validate_overdraft( value: OverdraftDict | Overdraft[Currency], *args: object, @@ -313,7 +320,7 @@ def build_generic_currency_schema( cls: type, source_type: type, adapter: type[GenericCurrencyAdapter[Any, Any]], -) -> core_schema.CoreSchema: +) -> core_schema.AfterValidatorFunctionSchema: if source_type is cls: # Not specialized allow any default Currency. validator = adapter.validator_from_registry(default_registry) @@ -334,7 +341,7 @@ def build_generic_currency_schema( validator = adapter.validator_from_currency(currency) schema = adapter.schema(currency_schema((currency,))) - return core_schema.general_after_validator_function( + return core_schema.with_info_after_validator_function( schema=schema, function=validator, serialization=core_schema.wrap_serializer_function_ser_schema( @@ -364,7 +371,7 @@ def validate_currency( return or_is_instance( cls=cls, - wrapped=core_schema.general_after_validator_function( + wrapped=core_schema.with_info_after_validator_function( function=validate_currency, schema=currency_schema(cls_registry.values()), ),