Skip to content

Commit

Permalink
feature: Support pydantic-core 2.10+ (#89)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
antonagestam authored Dec 17, 2023
1 parent e8fe2f0 commit 8432488
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 19 additions & 12 deletions src/immoney/_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
...


Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -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()),
),
Expand Down

0 comments on commit 8432488

Please sign in to comment.