From 8d6ac806b962286f6b1b7eb732b40d54ae66fcb7 Mon Sep 17 00:00:00 2001 From: Antonyjin Date: Fri, 27 Sep 2024 22:45:12 +0900 Subject: [PATCH] Fix: Refactor ItemHash to align with Pydantic v2 validation schema. Replaced `__get_pydantic_core_schema__` with a more efficient schema handling using `core_schema.str_schema()` and custom validation for ItemHash. --- aleph_message/models/item_hash.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/aleph_message/models/item_hash.py b/aleph_message/models/item_hash.py index e029416..7037a99 100644 --- a/aleph_message/models/item_hash.py +++ b/aleph_message/models/item_hash.py @@ -2,7 +2,8 @@ from functools import lru_cache from ..exceptions import UnknownHashError - +from pydantic_core import CoreSchema, core_schema +from pydantic import GetCoreSchemaHandler class ItemType(str, Enum): """Item storage options""" @@ -45,18 +46,21 @@ def __new__(cls, value: str): return obj @classmethod - def __get_validators__(cls): - # one or more validators may be yielded which will be called in the - # order to validate the input, each validator will receive as an input - # the value returned from the previous validator - yield cls.validate + def __get_pydantic_core_schema__(cls, source, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema: + # This function validates the input after the initial type validation (as a string). + # The returned value from this function will be used as the final validated value. + + # Return a string schema and add a post-validation function to convert to ItemHash + return core_schema.no_info_after_validator_function( + cls.validate, + core_schema.str_schema() + ) @classmethod def validate(cls, v): if not isinstance(v, str): raise TypeError("Item hash must be a string") - - return cls(v) + return cls(v) # Convert to ItemHash def __repr__(self): return f""