Last active
March 20, 2022 15:43
-
-
Save ZechCodes/a09ec511bafdbf1d9f734a4288c96ec2 to your computer and use it in GitHub Desktop.
Apply Mutations on Dataclass Fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
import json | |
from dataclasses import dataclass, MISSING, field as _field | |
from typing import Any, Callable | |
class MutationDescriptor: | |
def __init__(self, mutator: Callable[[Any], Any]): | |
self._mutator = mutator | |
self._attr = None | |
def __set_name__(self, owner, name): | |
self._attr = name | |
def __set__(self, instance, value): | |
instance.__dict__[self._attr] = self._mutator(value) | |
def field(*, default=MISSING, default_factory=MISSING, init=True, repr=True, | |
hash=None, compare=True, metadata=None, kw_only=MISSING, mutation=MISSING): | |
kwargs = locals() # Gets the field args while maintaining IDE autocomplete/type hinting | |
del kwargs["mutation"] | |
if mutation is not MISSING: | |
if not callable(mutation): | |
raise TypeError(f"Mutation callable '{mutation!r}' is not a callable.") | |
kwargs["default"] = MutationDescriptor(mutation) | |
return _field(**kwargs) | |
@dataclass | |
class Hello: | |
a: int = field(mutation=lambda x: x ** 2) | |
b: dict = field(mutation=json.dumps) | |
print(Hello(4, {"a": "b"})) |
This is one honking great idea.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by @rednafi's tweet