# SPDX-License-Identifier: MITimportcopyfrom._compatimportPY_3_9_PLUS,get_generic_basefrom._makeimport_OBJ_SETATTR,NOTHING,fieldsfrom.exceptionsimportAttrsAttributeNotFoundErrordefasdict(inst,recurse=True,filter=None,dict_factory=dict,retain_collection_types=False,value_serializer=None,):""" Return the *attrs* attribute values of *inst* as a dict. Optionally recurse into other *attrs*-decorated classes. Args: inst: Instance of an *attrs*-decorated class. recurse (bool): Recurse into classes that are also *attrs*-decorated. filter (~typing.Callable): A callable whose return code determines whether an attribute or element is included (`True`) or dropped (`False`). Is called with the `attrs.Attribute` as the first argument and the value as the second argument. dict_factory (~typing.Callable): A callable to produce dictionaries from. For example, to produce ordered dictionaries instead of normal Python dictionaries, pass in ``collections.OrderedDict``. retain_collection_types (bool): Do not convert to `list` when encountering an attribute whose type is `tuple` or `set`. Only meaningful if *recurse* is `True`. value_serializer (typing.Callable | None): A hook that is called for every attribute or dict key/value. It receives the current instance, field and value and must return the (updated) value. The hook is run *after* the optional *filter* has been applied. Returns: Return type of *dict_factory*. Raises: attrs.exceptions.NotAnAttrsClassError: If *cls* is not an *attrs* class. .. versionadded:: 16.0.0 *dict_factory* .. versionadded:: 16.1.0 *retain_collection_types* .. versionadded:: 20.3.0 *value_serializer* .. versionadded:: 21.3.0 If a dict has a collection for a key, it is serialized as a tuple. """attrs=fields(inst.__class__)rv=dict_factory()forainattrs:v=getattr(inst,a.name)iffilterisnotNoneandnotfilter(a,v):continueifvalue_serializerisnotNone:v=value_serializer(inst,a,v)ifrecurseisTrue:ifhas(v.__class__):rv[a.name]=asdict(v,recurse=True,filter=filter,dict_factory=dict_factory,retain_collection_types=retain_collection_types,value_serializer=value_serializer,)elifisinstance(v,(tuple,list,set,frozenset)):cf=v.__class__ifretain_collection_typesisTrueelselistitems=[_asdict_anything(i,is_key=False,filter=filter,dict_factory=dict_factory,retain_collection_types=retain_collection_types,value_serializer=value_serializer,)foriinv]try:rv[a.name]=cf(items)exceptTypeError:ifnotissubclass(cf,tuple):raise# Workaround for TypeError: cf.__new__() missing 1 required# positional argument (which appears, for a namedturle)rv[a.name]=cf(*items)elifisinstance(v,dict):df=dict_factoryrv[a.name]=df((_asdict_anything(kk,is_key=True,filter=filter,dict_factory=df,retain_collection_types=retain_collection_types,value_serializer=value_serializer,),_asdict_anything(vv,is_key=False,filter=filter,dict_factory=df,retain_collection_types=retain_collection_types,value_serializer=value_serializer,),)forkk,vvinv.items())else:rv[a.name]=velse:rv[a.name]=vreturnrvdef_asdict_anything(val,is_key,filter,dict_factory,retain_collection_types,value_serializer,):""" ``asdict`` only works on attrs instances, this works on anything. """ifgetattr(val.__class__,"__attrs_attrs__",None)isnotNone:# Attrs class.rv=asdict(val,recurse=True,filter=filter,dict_factory=dict_factory,retain_collection_types=retain_collection_types,value_serializer=value_serializer,)elifisinstance(val,(tuple,list,set,frozenset)):ifretain_collection_typesisTrue:cf=val.__class__elifis_key:cf=tupleelse:cf=listrv=cf([_asdict_anything(i,is_key=False,filter=filter,dict_factory=dict_factory,retain_collection_types=retain_collection_types,value_serializer=value_serializer,)foriinval])elifisinstance(val,dict):df=dict_factoryrv=df((_asdict_anything(kk,is_key=True,filter=filter,dict_factory=df,retain_collection_types=retain_collection_types,value_serializer=value_serializer,),_asdict_anything(vv,is_key=False,filter=filter,dict_factory=df,retain_collection_types=retain_collection_types,value_serializer=value_serializer,),)forkk,vvinval.items())else:rv=valifvalue_serializerisnotNone:rv=value_serializer(None,None,rv)returnrvdefastuple(inst,recurse=True,filter=None,tuple_factory=tuple,retain_collection_types=False,):""" Return the *attrs* attribute values of *inst* as a tuple. Optionally recurse into other *attrs*-decorated classes. Args: inst: Instance of an *attrs*-decorated class. recurse (bool): Recurse into classes that are also *attrs*-decorated. filter (~typing.Callable): A callable whose return code determines whether an attribute or element is included (`True`) or dropped (`False`). Is called with the `attrs.Attribute` as the first argument and the value as the second argument. tuple_factory (~typing.Callable): A callable to produce tuples from. For example, to produce lists instead of tuples. retain_collection_types (bool): Do not convert to `list` or `dict` when encountering an attribute which type is `tuple`, `dict` or `set`. Only meaningful if *recurse* is `True`. Returns: Return type of *tuple_factory* Raises: attrs.exceptions.NotAnAttrsClassError: If *cls* is not an *attrs* class. .. versionadded:: 16.2.0 """attrs=fields(inst.__class__)rv=[]retain=retain_collection_types# Very long. :/forainattrs:v=getattr(inst,a.name)iffilterisnotNoneandnotfilter(a,v):continueifrecurseisTrue:ifhas(v.__class__):rv.append(astuple(v,recurse=True,filter=filter,tuple_factory=tuple_factory,retain_collection_types=retain,))elifisinstance(v,(tuple,list,set,frozenset)):cf=v.__class__ifretainisTrueelselistitems=[(astuple(j,recurse=True,filter=filter,tuple_factory=tuple_factory,retain_collection_types=retain,)ifhas(j.__class__)elsej)forjinv]try:rv.append(cf(items))exceptTypeError:ifnotissubclass(cf,tuple):raise# Workaround for TypeError: cf.__new__() missing 1 required# positional argument (which appears, for a namedturle)rv.append(cf(*items))elifisinstance(v,dict):df=v.__class__ifretainisTrueelsedictrv.append(df(((astuple(kk,tuple_factory=tuple_factory,retain_collection_types=retain,)ifhas(kk.__class__)elsekk),(astuple(vv,tuple_factory=tuple_factory,retain_collection_types=retain,)ifhas(vv.__class__)elsevv),)forkk,vvinv.items()))else:rv.append(v)else:rv.append(v)returnrviftuple_factoryislistelsetuple_factory(rv)
[docs]defhas(cls):""" Check whether *cls* is a class with *attrs* attributes. Args: cls (type): Class to introspect. Raises: TypeError: If *cls* is not a class. Returns: bool: """attrs=getattr(cls,"__attrs_attrs__",None)ifattrsisnotNone:returnTrue# No attrs, maybe it's a specialized generic (A[str])?generic_base=get_generic_base(cls)ifgeneric_baseisnotNone:generic_attrs=getattr(generic_base,"__attrs_attrs__",None)ifgeneric_attrsisnotNone:# Stick it on here for speed next time.cls.__attrs_attrs__=generic_attrsreturngeneric_attrsisnotNonereturnFalse
[docs]defassoc(inst,**changes):""" Copy *inst* and apply *changes*. This is different from `evolve` that applies the changes to the arguments that create the new instance. `evolve`'s behavior is preferable, but there are `edge cases`_ where it doesn't work. Therefore `assoc` is deprecated, but will not be removed. .. _`edge cases`: https://github.com/python-attrs/attrs/issues/251 Args: inst: Instance of a class with *attrs* attributes. changes: Keyword changes in the new copy. Returns: A copy of inst with *changes* incorporated. Raises: attrs.exceptions.AttrsAttributeNotFoundError: If *attr_name* couldn't be found on *cls*. attrs.exceptions.NotAnAttrsClassError: If *cls* is not an *attrs* class. .. deprecated:: 17.1.0 Use `attrs.evolve` instead if you can. This function will not be removed du to the slightly different approach compared to `attrs.evolve`, though. """new=copy.copy(inst)attrs=fields(inst.__class__)fork,vinchanges.items():a=getattr(attrs,k,NOTHING)ifaisNOTHING:msg=f"{k} is not an attrs attribute on {new.__class__}."raiseAttrsAttributeNotFoundError(msg)_OBJ_SETATTR(new,k,v)returnnew
[docs]defresolve_types(cls,globalns=None,localns=None,attribs=None,include_extras=True):""" Resolve any strings and forward annotations in type annotations. This is only required if you need concrete types in :class:`Attribute`'s *type* field. In other words, you don't need to resolve your types if you only use them for static type checking. With no arguments, names will be looked up in the module in which the class was created. If this is not what you want, for example, if the name only exists inside a method, you may pass *globalns* or *localns* to specify other dictionaries in which to look up these names. See the docs of `typing.get_type_hints` for more details. Args: cls (type): Class to resolve. globalns (dict | None): Dictionary containing global variables. localns (dict | None): Dictionary containing local variables. attribs (list | None): List of attribs for the given class. This is necessary when calling from inside a ``field_transformer`` since *cls* is not an *attrs* class yet. include_extras (bool): Resolve more accurately, if possible. Pass ``include_extras`` to ``typing.get_hints``, if supported by the typing module. On supported Python versions (3.9+), this resolves the types more accurately. Raises: TypeError: If *cls* is not a class. attrs.exceptions.NotAnAttrsClassError: If *cls* is not an *attrs* class and you didn't pass any attribs. NameError: If types cannot be resolved because of missing variables. Returns: *cls* so you can use this function also as a class decorator. Please note that you have to apply it **after** `attrs.define`. That means the decorator has to come in the line **before** `attrs.define`. .. versionadded:: 20.1.0 .. versionadded:: 21.1.0 *attribs* .. versionadded:: 23.1.0 *include_extras* """# Since calling get_type_hints is expensive we cache whether we've# done it already.ifgetattr(cls,"__attrs_types_resolved__",None)!=cls:importtypingkwargs={"globalns":globalns,"localns":localns}ifPY_3_9_PLUS:kwargs["include_extras"]=include_extrashints=typing.get_type_hints(cls,**kwargs)forfieldinfields(cls)ifattribsisNoneelseattribs:iffield.nameinhints:# Since fields have been frozen we must work around it._OBJ_SETATTR(field,"type",hints[field.name])# We store the class we resolved so that subclasses know they haven't# been resolved.cls.__attrs_types_resolved__=cls# Return the class so you can use it as a decorator too.returncls