"""Opaque text tokens whose membership a registry projection establishes.A registry-projected vocabulary carries no closed Python member list: the datedregistry owns which tokens exist and what they mean. These bases hold only thestructural contract every such token shares, so a vocabulary type declares itsdocstring and nothing else unless its diagnostics or boundary differ.- :class:`StrictRegistryToken` admits only already projected tokens at a typed model boundary and serializes them as text.- :class:`TextProjectedRegistryToken` projects a non-empty text value at the model boundary."""from__future__importannotationsfromtypingimportClassVar,SelffrompydanticimportGetCoreSchemaHandlerfrompydantic_coreimportCoreSchema,core_schemafrom.errors.hierarchyimportCoreValidationError,pydantic_validation_boundary
[docs]classRegistryToken(str):"""Text token constructible only at a registry projection boundary."""__slots__=()#: Names the token in diagnostics; the class name when unset._vocabulary_label:ClassVar[str|None]=None#: Names the authority in the refusal raised for unprojected construction._projection_source:ClassVar[str]="facts registry"#: Replaces the default empty-token refusal when the vocabulary words it differently._empty_value_message:ClassVar[str|None]=None@classmethoddef_diagnostic_label(cls)->str:returncls._vocabulary_labelorcls.__name__def__new__(cls,value:str,*,_registry_validated:bool=False)->Self:"""Construct only when called by the registry projection boundary."""ifnot_registry_validated:raiseTypeError(f"{cls._diagnostic_label()} tokens must be projected from the {cls._projection_source}")ifnotisinstance(value,str)ornotvalue:raiseValueError(cls._empty_value_messageorf"{cls._diagnostic_label()} token must be a non-empty string")returnstr.__new__(cls,value)
[docs]@classmethoddeffrom_registry(cls,value:str)->Self:"""Construct the typed value from its canonical registry token."""returncls(value,_registry_validated=True)
@propertydefvalue(self)->str:"""Return the canonical registry token text."""returnstr(self)
[docs]classStrictRegistryToken(RegistryToken):"""Registry token a typed model accepts only once it has been projected."""__slots__=()#: The error a typed model boundary raises for an unprojected value._refusal_error:ClassVar[type[Exception]]=CoreValidationError@classmethoddef_require_registry_token(cls,value:object)->Self:ifisinstance(value,cls):returnvalueraisecls._refusal_error(f"{cls._diagnostic_label()} must be a registry-projected token")@classmethoddef__get_pydantic_core_schema__(cls,_source_type:object,_handler:GetCoreSchemaHandler,)->CoreSchema:"""Accept only a projected token and serialize it as text."""returncore_schema.no_info_plain_validator_function(pydantic_validation_boundary(cls._require_registry_token),json_schema_input_schema=core_schema.str_schema(),serialization=core_schema.to_string_ser_schema(),)@propertydefname(self)->str:"""Return the canonical registry token for diagnostics."""returnstr(self)
[docs]classTextProjectedRegistryToken(RegistryToken):"""Registry token a typed model projects from its non-empty text form."""__slots__=()#: Names the token in the refusals raised at the model boundary._boundary_subject:ClassVar[str]@classmethoddef__get_pydantic_core_schema__(cls,_source_type:object,_handler:GetCoreSchemaHandler,)->CoreSchema:"""Project the text token through the registry boundary."""returncore_schema.no_info_after_validator_function(pydantic_validation_boundary(cls._project_pydantic),core_schema.str_schema(min_length=1),)@classmethoddef_project_pydantic(cls,value:object)->Self:ifisinstance(value,cls):returnvalueifisinstance(value,str):try:returncls.from_registry(value.strip())except(TypeError,ValueError)asexc:raiseValueError(f"{cls._boundary_subject} must be a non-empty structural token")fromexcraiseTypeError(f"{cls._boundary_subject} must be a string token")