From fd2a3e75673d775aa08a602604db35ce2fd6eb09 Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Wed, 13 May 2026 17:52:44 +0100 Subject: [PATCH] Describe __get_pydantic_core_schema__ --- synapse/types/__init__.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/synapse/types/__init__.py b/synapse/types/__init__.py index 741e7462ce..62084ef119 100644 --- a/synapse/types/__init__.py +++ b/synapse/types/__init__.py @@ -134,6 +134,20 @@ class AbsentType(Enum): def __get_pydantic_core_schema__( cls, source_type: object, handler: GetCoreSchemaHandler ) -> CoreSchema: + """ + This function is checked for and used by Pydantic when + attempting to deserialise/validate a field of this type. + + As the `Absent` type has no valid value when deserialising + from JSON (as that's the point; `Absent` is a marker representing + a lack of any JSON value), we always reject any value. + Instead of deserialising from this type, we rely on the struct class + we are in having field defaults that provide an `Absent`, which does not + go through the JSON validation. + + When validating Python, we accept the absent marker itself. + """ + def _reject_from_json(v: object) -> "AbsentType": """ Reject the JSON value, no matter what it is, since absent values