Skip to content

Commit

Permalink
fix: pydantic 2.10.x breaking change (#1095)
Browse files Browse the repository at this point in the history
* fix: breaking change with pydantic 2.10.x

* chore: test on gha

* fix: unify python and json validator schema

* fix: revert version checking logic

* fix: drop on push event on github actions

* fix: marker logic for pydantic v2.10

* fix: add test pydantic 2.10.4
  • Loading branch information
mdaffad authored Jan 2, 2025
1 parent 81da389 commit 687b4a7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/github-actions-tests.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Tests
on: [ pull_request ]
on:
pull_request:

jobs:
pre-commit:
Expand All @@ -16,7 +17,7 @@ jobs:
matrix:
python-version: [ "3.8", "3.9", "3.10", "3.11", "3.12", "3.13" ]
mongodb-version: [ "4.4", "5.0", "6.0", "7.0", "8.0" ]
pydantic-version: [ "1.10.18", "2.9.2" ]
pydantic-version: [ "1.10.18", "2.9.2" , "2.10.4"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
18 changes: 15 additions & 3 deletions beanie/odm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from beanie.odm.utils.parsing import parse_obj
from beanie.odm.utils.pydantic import (
IS_PYDANTIC_V2,
IS_PYDANTIC_V2_10,
get_field_type,
get_model_fields,
parse_object_as,
Expand Down Expand Up @@ -147,9 +148,8 @@ def _validate(cls, v):
def __get_pydantic_core_schema__(
cls, source_type: Type[Any], handler: GetCoreSchemaHandler
) -> CoreSchema:
return json_or_python_schema(
python_schema=no_info_plain_validator_function(cls._validate),
json_schema=no_info_plain_validator_function(
if not IS_PYDANTIC_V2_10:
return no_info_plain_validator_function(
cls._validate,
metadata={
"pydantic_js_input_core_schema": str_schema(
Expand All @@ -158,6 +158,18 @@ def __get_pydantic_core_schema__(
max_length=24,
)
},
serialization=plain_serializer_function_ser_schema(
lambda instance: str(instance),
return_schema=str_schema(),
when_used="json",
),
)
return no_info_plain_validator_function(
cls._validate,
json_schema_input_schema=str_schema(
pattern="^[0-9a-f]{24}$",
min_length=24,
max_length=24,
),
serialization=plain_serializer_function_ser_schema(
lambda instance: str(instance),
Expand Down
11 changes: 10 additions & 1 deletion beanie/odm/utils/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@

import beanie
from beanie.odm.fields import Link, LinkTypes
from beanie.odm.utils.pydantic import IS_PYDANTIC_V2, get_model_fields
from beanie.odm.utils.pydantic import (
IS_PYDANTIC_V2,
IS_PYDANTIC_V2_10,
get_model_fields,
)

SingleArgCallable = Callable[[Any], Any]
DEFAULT_CUSTOM_ENCODERS: MutableMapping[type, SingleArgCallable] = {
Expand All @@ -51,6 +55,11 @@

DEFAULT_CUSTOM_ENCODERS[Url] = str

if IS_PYDANTIC_V2_10:
from pydantic import AnyUrl

DEFAULT_CUSTOM_ENCODERS[AnyUrl] = str

BSON_SCALAR_TYPES = (
type(None),
str,
Expand Down
3 changes: 3 additions & 0 deletions beanie/odm/utils/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from pydantic import BaseModel

IS_PYDANTIC_V2 = int(pydantic.VERSION.split(".")[0]) >= 2
IS_PYDANTIC_V2_10 = (
IS_PYDANTIC_V2 and int(pydantic.VERSION.split(".")[1]) >= 10
)

if IS_PYDANTIC_V2:
from pydantic import TypeAdapter
Expand Down

0 comments on commit 687b4a7

Please sign in to comment.