Coverage for src/configuraptor/beautify.py: 100%
22 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-01-22 21:21 +0100
« prev ^ index » next coverage.py v7.2.7, created at 2024-01-22 21:21 +0100
1"""
2Add @beautify behavior to enhance configuraptor/TypedConfig classes automagically!
3"""
4import typing
6from .dump import asdict, asjson
8T = typing.TypeVar("T")
11def is_default(obj: typing.Any, prop: str) -> bool:
12 """
13 Check if the property of an object is set to its default value.
15 Args:
16 obj (typing.Any): The object to check.
17 prop (str): The property to check.
19 Returns:
20 bool: True if the property is set to its default value, False otherwise.
21 """
22 return getattr(obj, prop) is getattr(object, prop)
25def patch(cls: typing.Type[T]) -> None:
26 """
27 Patch the __str__ and __repr__ methods of a class if they are set to their default values.
29 Args:
30 cls (typing.Type[typing.Any]): The class to patch.
31 """
33 def _repr(self: T) -> str:
34 """
35 Custom __repr__ by configuraptor @beautify.
36 """
37 clsname = type(self).__name__
38 return f"<{clsname} {asdict(self)}>"
40 # if magic method is already set, don't overwrite it!
41 if is_default(cls, "__str__"):
42 cls.__str__ = asjson # type: ignore
44 if is_default(cls, "__repr__"):
45 cls.__repr__ = _repr # type: ignore
48@typing.overload
49def beautify(maybe_cls: typing.Type[T]) -> typing.Type[T]:
50 """
51 Overload function for the beautify decorator when used without parentheses.
52 """
55@typing.overload
56def beautify(maybe_cls: None = None) -> typing.Callable[[typing.Type[T]], typing.Type[T]]:
57 """
58 Overload function for the beautify decorator when used with parentheses.
59 """
62def beautify(
63 maybe_cls: typing.Type[T] | None = None,
64) -> typing.Type[T] | typing.Callable[[typing.Type[T]], typing.Type[T]]:
65 """
66 The beautify decorator. Enhances a class by patching its __str__ and __repr__ methods.
68 Args:
69 maybe_cls (typing.Type[T] | None, optional): The class to beautify. None when used with parentheses.
71 Returns:
72 The beautified class or the beautify decorator.
73 """
74 if maybe_cls:
75 patch(maybe_cls)
76 return maybe_cls
77 else:
78 return beautify