From 47c930f0b7d57e787f57db00b4f03d0862324949 Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Tue, 31 Mar 2026 18:01:53 +0100 Subject: [PATCH] Work around Pydantic < 2.10 error builtins.NotImplementedError: Cannot check isinstance when validating from json, use a JsonOrPython validator instead. --- synapse/types/__init__.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/synapse/types/__init__.py b/synapse/types/__init__.py index 90d7c0ed72..741e7462ce 100644 --- a/synapse/types/__init__.py +++ b/synapse/types/__init__.py @@ -134,7 +134,22 @@ class AbsentType(Enum): def __get_pydantic_core_schema__( cls, source_type: object, handler: GetCoreSchemaHandler ) -> CoreSchema: - return pydantic_core.core_schema.is_instance_schema(cls) + def _reject_from_json(v: object) -> "AbsentType": + """ + Reject the JSON value, no matter what it is, since absent values + are meant to be ... absent, thus have nothing they can be deserialised + from. + """ + raise ValueError("AbsentType cannot be deserialized from JSON") + + # `json_or_python_schema` wrapper needed for Pydantic < 2.10 + # but can be replaced with just the `is_instance_schema` after that version. + return pydantic_core.core_schema.json_or_python_schema( + json_schema=pydantic_core.core_schema.no_info_plain_validator_function( + _reject_from_json + ), + python_schema=pydantic_core.core_schema.is_instance_schema(cls), + ) def __copy__(self) -> "AbsentType": """