"""Opaque CCAA tokens projected from the governed tax-residence catalogue.The community membership and the retired ISO aliases are filing facts. Thismodule keeps only the token type and compatibility-shaped parsing mechanics;the dated vocabulary is resolved by :mod:`ccaa_catalogue`."""from__future__importannotationsfromcollections.abcimportIteratorfromtypingimportSelffrompydanticimportGetCoreSchemaHandlerfrompydantic_coreimportCoreSchema,core_schemafrom...core.errors.hierarchyimportCoreValidationError,ProfileAnswerTypeError,pydantic_validation_boundaryclass_CCAAType(type):"""Expose registry-projected choices through the historical type surface."""def__iter__(cls:_CCAAType)->Iterator[CCAA]:from..calculations.registry.ccaa_catalogueimportresolve_ccaa_cataloguereturniter(resolve_ccaa_catalogue().choices)def__getattr__(cls,name:str)->CCAA:ifname.startswith("__")andname.endswith("__"):raiseAttributeError(name)from..calculations.registry.ccaa_catalogueimportresolve_ccaa_cataloguetry:returnresolve_ccaa_catalogue().require_member_name(name)except(KeyError,ValueError)asexc:raiseAttributeError(name)fromexc
[docs]classCCAA(str,metaclass=_CCAAType):"""Registry-projected ordinary common-regime CCAA token. Instantiation is deliberately fail-closed: arbitrary strings cannot become CCAA values without passing through the selected dated facts catalogue. ``from_registry`` is private to the typed projection module. """__slots__=()def__new__(cls,value:object,*,_registry_validated:bool=False)->Self:"""Construct only registry-projected CCAA tokens."""if_registry_validated:ifnotisinstance(value,str)ornotvalue:raiseValueError("CCAA token must be a non-empty string")returnstr.__new__(cls,value)from..calculations.registry.ccaa_catalogueimportresolve_ccaa_cataloguefrom..calculations.registry.errorsimportRegistryValidationErrortry:returncls.from_registry(str(resolve_ccaa_catalogue().require(value)))exceptRegistryValidationErrorasexc:raiseValueError(str(exc))fromexc
[docs]@classmethoddeffrom_registry(cls,value:str)->Self:"""Construct the typed value from its canonical registry token."""returncls(value,_registry_validated=True)
@classmethoddef_require_registry_token(cls,value:object)->Self:ifisinstance(value,cls):returnvalueifisinstance(value,str):# A persisted token is projected back through the dated catalogue.returncls(value)raiseCoreValidationError("CCAA must be a registry-projected token")@classmethoddef__get_pydantic_core_schema__(cls,source_type:type[object],handler:GetCoreSchemaHandler,)->CoreSchema:"""Accept only a projected CCAA token and serialize it as text."""delsource_type,handlerreturncore_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(),)
[docs]@classmethoddeffrom_iso_code(cls,code:str)->Self:"""Resolve one of the registry-declared three-letter aliases."""from..calculations.registry.ccaa_catalogueimportresolve_ccaa_cataloguereturncls.from_registry(str(resolve_ccaa_catalogue().from_iso_code(code)))
[docs]@classmethoddeffrom_label(cls,label:str)->Self:"""Resolve a canonical token, normalized label, or ISO alias."""from..calculations.registry.ccaa_catalogueimportresolve_ccaa_cataloguefrom..calculations.registry.errorsimportRegistryValidationErrortry:returncls.from_registry(str(resolve_ccaa_catalogue().from_label(label)))except(KeyError,RegistryValidationError)asexc:raiseProfileAnswerTypeError(str(exc))fromexc
@propertydefvalue(self)->str:"""Return the canonical token used by profile and registry bindings."""returnstr(self)@propertydefname(self)->str:"""Return the canonical token as a diagnostic name."""returnstr(self)