|
| 1 | +import typing as ty |
| 2 | +from functools import lru_cache |
| 3 | +from typing_extensions import Literal |
| 4 | + |
| 5 | +import attr |
| 6 | +from cattr.converters import _is_attrs_class, Converter |
| 7 | + |
| 8 | +from .patch import TypecatsCattrPatch |
| 9 | + |
| 10 | +C = ty.TypeVar("C") |
| 11 | + |
| 12 | + |
| 13 | +_MISSING = object() |
| 14 | + |
| 15 | + |
| 16 | +@lru_cache(128) |
| 17 | +def _get_factory_default(_attr): |
| 18 | + return _attr.default.factory() |
| 19 | + |
| 20 | + |
| 21 | +def _get_attr_default_value(attribute) -> ty.Any: |
| 22 | + if not isinstance(attribute.default, attr.Factory): # type: ignore |
| 23 | + return attribute.default |
| 24 | + return _get_factory_default(attribute) |
| 25 | + |
| 26 | + |
| 27 | +def _strip_attr_defaults( |
| 28 | + attrs_type: ty.Type, m: ty.Mapping[str, ty.Any] |
| 29 | +) -> ty.Dict[str, ty.Any]: |
| 30 | + """The idea here is that when you are using pure dicts, a key can be |
| 31 | + missing to indicate absence. But if you're dealing with typed |
| 32 | + objects, that's not possible since all keys are always present. So |
| 33 | + the only 'reasonable' way to determine what a 'union' means in a |
| 34 | + class-based world is to prefer non-default values to default values at |
| 35 | + all times, which attrs can tell us about. |
| 36 | + """ |
| 37 | + attr_defaults_by_name = { |
| 38 | + attribute.name: _get_attr_default_value(attribute) |
| 39 | + for attribute in attrs_type.__attrs_attrs__ |
| 40 | + } |
| 41 | + return { |
| 42 | + k: v |
| 43 | + for k, v in m.items() |
| 44 | + if k not in attr_defaults_by_name or v != attr_defaults_by_name[k] |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | +def _get_names_of_defaulted_attrs( |
| 49 | + unstructured: ty.Dict[str, ty.Any], attrs_obj: ty.Any |
| 50 | +) -> ty.Set[str]: |
| 51 | + """We must provide the partially unstructured version of the object in |
| 52 | + case this is a wildcat with keys that aren't defined as part of |
| 53 | + the Attr type. |
| 54 | + """ |
| 55 | + res: ty.Set[str] = set() |
| 56 | + for _attr in attrs_obj.__attrs_attrs__: |
| 57 | + if getattr(_attr.type, "__origin__", None) is Literal: |
| 58 | + # don't strip attributes annotated as Literals - they're requirements, not "defaults" |
| 59 | + continue |
| 60 | + if unstructured.get(_attr.name, _MISSING) == _get_attr_default_value(_attr): |
| 61 | + res.add(_attr.name) |
| 62 | + return res |
| 63 | + |
| 64 | + |
| 65 | +def _strip_attrs_defaults( |
| 66 | + unstructured_but_unclean: ty.Any, obj_to_unstructure: ty.Any |
| 67 | +) -> ty.Any: |
| 68 | + if _is_attrs_class(obj_to_unstructure.__class__): |
| 69 | + keys_to_strip = _get_names_of_defaulted_attrs( |
| 70 | + unstructured_but_unclean, obj_to_unstructure |
| 71 | + ) |
| 72 | + return { |
| 73 | + k: v for k, v in unstructured_but_unclean.items() if k not in keys_to_strip |
| 74 | + } |
| 75 | + return unstructured_but_unclean |
| 76 | + |
| 77 | + |
| 78 | +class StripAttrsDefaultsOnUnstructurePatch(TypecatsCattrPatch): |
| 79 | + def unstructure_patch( |
| 80 | + self, original_handler: ty.Callable, obj_to_unstructure: ty.Any |
| 81 | + ) -> ty.Any: |
| 82 | + rv = super().unstructure_patch(original_handler, obj_to_unstructure) |
| 83 | + return _strip_attrs_defaults(rv, obj_to_unstructure) |
| 84 | + |
| 85 | + |
| 86 | +_STRIP_DEFAULTS_CONVERTER = Converter() # create converter |
| 87 | +__PATCH = StripAttrsDefaultsOnUnstructurePatch(_STRIP_DEFAULTS_CONVERTER) # patch it |
| 88 | + |
| 89 | + |
| 90 | +def get_stripping_converter() -> Converter: |
| 91 | + return _STRIP_DEFAULTS_CONVERTER |
| 92 | + |
| 93 | + |
| 94 | +def unstruc_strip_defaults(obj: ty.Any) -> ty.Any: |
| 95 | + """This is the only thing anyone outside needs to worry about""" |
| 96 | + return _STRIP_DEFAULTS_CONVERTER.unstructure(obj) |
0 commit comments