Skip to content

Commit

Permalink
Fix issue when using altqq.Calculated on different named fields
Browse files Browse the repository at this point in the history
  • Loading branch information
baluyotraf committed May 21, 2024
1 parent ce317de commit 383b7a7
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/altqq/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
QUERY_ATTRIB = "__query__"


class Calculated:
"""Marker class for calculated values."""


@dataclass_transform()
class QueryMeta(type):
"""Metaclass for generating Query objects that the library supports.
Expand All @@ -34,12 +38,19 @@ def _check_query_attribute(dataclass: "QueryMeta", dct: Dict[str, Any]):

return False

@staticmethod
def _resolve_calculated_fields(dct: Dict[str, Any]):
for k, v in dct.items():
if v == Calculated:
dct[k] = dc.field(init=False)

def __new__(cls, name: str, bases: Tuple[type, ...], dct: Dict[str, Any]):
"""Creates a new class of the metaclass.
This wraps the newly created class with Pydantic Dataclass and checks
the `__query__` attribute.
"""
cls._resolve_calculated_fields(dct)
dataclass = super().__new__(cls, name, bases, dct)
if not cls._check_query_attribute(dataclass, dct):
raise ValueError(f"A string {QUERY_ATTRIB} must be provided")
Expand All @@ -54,6 +65,3 @@ class metaclass.
"""

__query__: ClassVar[str]


Calculated = dc.field(init=False)

0 comments on commit 383b7a7

Please sign in to comment.