Source code for cadrumo.domain.contribuyente.tax_residence
"""Where a taxpayer is tax-resident, and how that has changed.These models and the region parser were defined directly in the packagenamespace, which is why deleting an export map could not make it inert.They live in a module of their own now."""from__future__importannotationsfromdatetimeimportdatefromtypingimportcastfrompydanticimportBaseModel,ConfigDict,Field,field_validatorfrom...core.errors.hierarchyimportpydantic_validation_boundaryfrom...core.parsing.datesimportparse_iso8601_dateas_parse_iso8601_datefrom...core.text_foldimportfold_diacriticsas_fold_diacriticsfrom..calculations.registry.ccaa_catalogueimportresolve_ccaa_cataloguefrom..calculations.registry.errorsimportRegistryValidationErrorfrom.ccaaimportCCAAfrom.constantsimportSUPPORTED_PROFILE_SCHEMA_VERSION,ProfileSchemaVersionfrom.errorsimportForalRegimeError,TaxResidenceProfileError
[docs]classTaxResidenceProfile(BaseModel,frozen=True,strict=True):"""The operator's current ordinary CCAA tax residence for RENTA."""model_config=ConfigDict(extra="forbid",validate_assignment=True)schema_version:ProfileSchemaVersion=Field(default=SUPPORTED_PROFILE_SCHEMA_VERSION)ccaa:CCAAtax_residence_since:date|None=Nonetax_residence_change_history:tuple[ResidenceChange,...]=()@field_validator("ccaa",mode="before")@classmethod@pydantic_validation_boundarydef_parse_ccaa(cls,value:object)->object:ifisinstance(value,str):returnparse_tax_region(value)returnvalue@field_validator("tax_residence_change_history",mode="before")@classmethod@pydantic_validation_boundarydef_parse_change_history(cls,value:object)->object:ifisinstance(value,list):# CAST-RATIONALE-TAX-RESIDENCE-CHANGE-HISTORY: isinstance narrows to# list but not its element type; pydantic re-validates each element# against the field's declared item type after this coercion.# nosemgrep: no-cast-in-domain-applicationreturntuple(cast(list[object],value))returnvalue@field_validator("tax_residence_since",mode="before")@classmethod@pydantic_validation_boundarydef_parse_since(cls,value:object)->object:ifisinstance(value,str):return_parse_iso8601_date(value)returnvalue
[docs]defparse_tax_region(raw:str)->CCAA:"""Parse a CLI/user tax-region token through the dated CCAA catalogue."""normalized=_normalize_region_token(raw)catalogue=resolve_ccaa_catalogue()ifcatalogue.is_foral_alias(normalized):raiseForalRegimeError(raw)try:returncatalogue.require(normalized)exceptRegistryValidationErrorasexc:valid=", ".join(sorted(str(ccaa)forccaaincatalogue.choices))raiseTaxResidenceProfileError(f"unknown tax-region {raw!r}; valid CCAA values: {valid}",context={"tax_region":raw},)fromexc