Skip to content

Commit

Permalink
fixed quality
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnnsrs committed Jan 31, 2024
1 parent 281507f commit 718413b
Showing 1 changed file with 49 additions and 6 deletions.
55 changes: 49 additions & 6 deletions rath/turms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@


class NotQueriedError(Exception):
"""An error that is raised if a nested attribute is not queried"""

pass


Expand Down Expand Up @@ -43,26 +45,67 @@ def get_nested_error(obj: Any, nested: List[str], above: List[str]) -> Any:
raise NotQueriedError(f"{nested} not queried. {above} was queried")


def get_attributes_or_error(self, *args) -> Any:
"""Get attributes or raise an error"""
def get_attributes_or_error(object: Any, *args: str) -> Any:
"""Get attributes or raise an error
Get attributes from an object or raise an error if the attribute is not present
. Nested attributes can be queried by using a dot as a delimiter.
Examples
--------
```python
@dataclass
class B:
c: int
@dataclass
class A:
a: int
b: B
a = A(a=1, b=B(c=2))
get_attributes_or_error(a, "a", "b.c") # (1, 2)
```
Parameters
----------
object : Any
The object to query
*args : str
The attributes to query (nested attribtues delimited by a dot)
Raises
------
NotQueriedError
The nested attribute was not queried
"""
returns = []
errors = []
for i in args:
if "." in i:
try:
returns.append(get_nested_error(self, i.split("."), []))
returns.append(get_nested_error(object, i.split("."), []))
continue
except NotQueriedError:
errors.append(i)
else:
if hasattr(self, i):
returns.append(getattr(self, i))
if hasattr(object, i):
returns.append(getattr(object, i))
else:
errors.append(i)

if len(errors) > 0:
raise NotQueriedError(
f"Required fields {errors} not queried on {self.__class__.__name__}"
f"Required fields {errors} not queried on {object.__class__.__name__}"
)

if len(args) == 1:
Expand Down

0 comments on commit 718413b

Please sign in to comment.