Skip to content

Commit

Permalink
update pydantic
Browse files Browse the repository at this point in the history
;
  • Loading branch information
bitplane committed Sep 24, 2023
1 parent b42a1b0 commit b13da81
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
9 changes: 7 additions & 2 deletions arranges/src/arranges/ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions arranges/tests/range/serialize/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:"
6 changes: 2 additions & 4 deletions arranges/tests/range/serialize/test_validator.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down

0 comments on commit b13da81

Please sign in to comment.