Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Support pydantic-core 2.10+ #89

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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