"""Copyright 2019 InmantaLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.Contact: code@inmanta.com"""# This file defines named type definition for the Inmanta code baseimportbuiltinsimportdatetimeimportuuidfromcollections.abcimportCoroutine,Generator,Mapping,SequencefromtypingimportTYPE_CHECKING,Any,Callable,NewType,Optional,Unionimportpydanticimporttyping_inspectfrominmanta.stable_apiimportstable_apiifTYPE_CHECKING:# Include imports from other modules here and use the quoted annotation in the definition to prevent import loopsfrominmanta.protocol.commonimportReturnValue# noqa: F401# Typing of dataclass.* methods relies entirely on the definition in typeshed that only exists during typechecking.# This ensures that our code works during typechecking and at runtime.ifnotTYPE_CHECKING:DataclassProtocol=objectelse:import_typeshedDataclassProtocol=_typeshed.DataclassInstancedefapi_boundary_datetime_normalizer(value:datetime.datetime)->datetime.datetime:ifvalue.tzinfoisNone:returnvalue.replace(tzinfo=datetime.timezone.utc)else:returnvalue@stable_apiclassDateTimeNormalizerModel(pydantic.BaseModel):""" A model that normalizes all datetime values to be timezone aware. Assumes that all naive timestamps represent UTC times. """@pydantic.field_validator("*",mode="after")@classmethoddefvalidator_timezone_aware_timestamps(cls:type,value:object)->object:""" Ensure that all datetime times are timezone aware. """ifisinstance(value,datetime.datetime):returnapi_boundary_datetime_normalizer(value)else:returnvalue
[docs]@stable_apiclassBaseModel(DateTimeNormalizerModel):""" Base class for all data objects in Inmanta """
# kept for backwards compatibilityStrictNonIntBool=pydantic.StrictBooltypeAsyncioGenerator[R]=Generator[object,None,R]"""Asyncio-compatible generator as returned from a sync function, e.g. __await__."""typeAsyncioCoroutine[R]=Coroutine[object,None,R]"""Coroutine for use with asyncio, where we don't care about yield and send types."""defissubclass(sub:type,super:Union[type,tuple[type,...]])->bool:""" Alternative issubclass implementation that interpretes instances of NewType for the first argument as their super type. """iftyping_inspect.is_new_type(sub):returnissubclass(sub.__supertype__,super)returnbuiltins.issubclass(sub,super)PrimitiveTypes=Optional[uuid.UUID|bool|int|float|datetime.datetime|str]typeSimpleTypes=BaseModel|PrimitiveTypesJsonType=dict[str,Any]ReturnTupple=tuple[int,Optional[JsonType]]typeStrictJson=dict[str,StrictJson]|list[StrictJson]|str|int|float|bool|NonetypeStrMapping[T]=Mapping[str,T]|Mapping[ResourceIdStr,T]|Mapping[ResourceVersionIdStr,T]typeSinglePageTypes=SimpleTypes|StrMapping[ArgumentTypes]# only simple types allowed within list args, not dicts or lists.# Typed as Sequence for necessity (covariance), though runtime checks and method overloads require list in practice.# This is an unfortunate limitation of the Python type system, related to str being a Sequence, among other things.# Luckily, list is the more conventional return type for method annotations, so as long as that convention is followed,# it should not cause any trouble. And if it does after all, the only consequence will be that paging will not be supported# through the Python client.typePageableTypes=Sequence[SimpleTypes]typeArgumentTypes=SinglePageTypes|PageableTypestypeReturnTypes=SinglePageTypes|PageableTypestypeMethodReturn=ReturnTypes|"ReturnValue[ReturnTypes]"typeMethodType=Callable[...,MethodReturn]typeApireturn=int|ReturnTupple|"ReturnValue[ReturnTypes]"|"ReturnValue[None]"|ReturnTypestypeWarnings=Optional[list[str]]typeHandlerType=Callable[...,AsyncioCoroutine[Apireturn]]ResourceVersionIdStr=NewType("ResourceVersionIdStr",str)# Part of the stable API""" The resource id with the version included."""ResourceIdStr=NewType("ResourceIdStr",str)# Part of the stable API""" The resource id without the version"""ResourceType=NewType("ResourceType",str)""" The type of the resource"""