def __getattribute__(self, name: str) -> Any: values = super().__getattribute__("values") if name in values: return values[name] cls = super().__getattribute__("cls") fields = object_fields(cls, deserialization=True) if name in fields: if fields[name].required: raise NonTrivialDependency(name) return fields[name].get_default() if name == "__class__": return cls if name == "__dict__": return {**values, FIELDS_SET_ATTR: set(values)} if name == FIELDS_SET_ATTR: return set(values) if hasattr(cls, name): member = getattr(cls, name) # for classmethod (staticmethod are not handled) if isinstance(member, MethodType): return member if isinstance(member, FunctionType): return partial(member, self) if isinstance(member, property): return member.fget(self) # type: ignore return member raise NonTrivialDependency(name)
def test_alias(): assert {name: field.alias for name, field in object_fields(Data).items()} == { "not_aliased": "not_aliased", "not_prefixed": "not_overridden", "prefixed": "prefixed_prefixed", "prefixed_alias": "prefixed_alias", }
def test_invalid_flattened(): with raises(TypeError): list( get_deserialization_flattened_aliases( BadData, object_fields(BadData)["field"]))
def test_flattened_aliases(): aliases = get_deserialization_flattened_aliases( Data, object_fields(Data)["field"], settings.deserialization.default_conversion) assert set(aliases) == {"a", "g", "h", "i", "e"}