diff --git a/arranges/src/arranges/ranges.py b/arranges/src/arranges/ranges.py index 3fcfc82..2b227c3 100644 --- a/arranges/src/arranges/ranges.py +++ b/arranges/src/arranges/ranges.py @@ -2,6 +2,9 @@ from functools import lru_cache from typing import Any, Iterable +from pydantic import GetCoreSchemaHandler +from pydantic_core import CoreSchema, core_schema + from arranges.segment import Segment, range_idx from arranges.utils import inf, is_intlike, is_iterable, is_rangelike, try_hash @@ -269,11 +272,13 @@ def validate(cls, value: Any) -> "Ranges": return cls(value) @classmethod - def __get_validators__(cls): + def __get_pydantic_core_schema__( + cls, source_type: Any, handler: GetCoreSchemaHandler + ) -> CoreSchema: """ For automatic validation in pydantic """ - yield cls.validate + return core_schema.no_info_after_validator_function(cls, handler(Any)) @property def first(self): diff --git a/arranges/tests/range/serialize/test_serialize.py b/arranges/tests/range/serialize/test_serialize.py index c09635f..54219ff 100644 --- a/arranges/tests/range/serialize/test_serialize.py +++ b/arranges/tests/range/serialize/test_serialize.py @@ -10,10 +10,10 @@ class Model(BaseModel): def test_serialize(): model = Model(input_range="0:10,20:30,15:") - assert model.json() == '{"input_range": ":10,15:"}' + assert model.model_dump_json() == '{"input_range":":10,15:"}' def test_deserialize(): - model = Model.parse_raw('{"input_range": "0:10,20:30,15:"}') + model = Model.model_validate_json('{"input_range":"0:10,20:30,15:"}') assert model.input_range == ":10,15:" diff --git a/arranges/tests/range/serialize/test_validator.py b/arranges/tests/range/serialize/test_validator.py index c47008f..b735726 100644 --- a/arranges/tests/range/serialize/test_validator.py +++ b/arranges/tests/range/serialize/test_validator.py @@ -1,14 +1,12 @@ import pytest -from pydantic import BaseModel, ValidationError +from pydantic import BaseModel, ConfigDict, ValidationError from arranges import Ranges class ModelWithRange(BaseModel): range: Ranges - - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict(arbitrary_types_allowed=True) def test_working_range_str():