parent
7795697983
commit
9ef0fab1e2
1 changed files with 18 additions and 29 deletions
@ -1,44 +1,33 @@ |
|||||||
# import attr |
# import attr |
||||||
from dataclasses import dataclass, field |
from dataclasses import dataclass, field |
||||||
from typing import Any, List |
from typing import Any, List |
||||||
|
from enum import auto |
||||||
|
|
||||||
class AutoDefault: |
auto_obj = object() |
||||||
"""Marker class for fields that should automatically get a default.""" |
|
||||||
def __init__(self, factory: Any = None): |
|
||||||
self.factory = factory |
|
||||||
|
|
||||||
def auto_factory(factory=None): |
|
||||||
"""Function to return AutoDefault instance.""" |
|
||||||
return AutoDefault(factory=factory) |
|
||||||
|
|
||||||
def apply_auto_defaults(cls): |
def apply_auto_defaults(cls): |
||||||
cls_annotations = cls.__annotations__ |
cls_annotations = cls.__annotations__ |
||||||
for name, typ in cls_annotations.items(): |
for name, typ in cls_annotations.items(): |
||||||
current_value = getattr(cls, name, None) |
current_value = getattr(cls, name, None) |
||||||
if isinstance(current_value, AutoDefault): |
if current_value is auto_obj: |
||||||
if current_value.factory is not None: |
|
||||||
setattr(cls, name, field(default_factory=current_value.factory)) |
|
||||||
else: |
|
||||||
# Handle specific default types here or raise an error |
|
||||||
setattr(cls, name, field(default_factory=typ)) |
setattr(cls, name, field(default_factory=typ)) |
||||||
# if typ == bool: |
|
||||||
# setattr(cls, name, attr.field(default=False)) |
|
||||||
# elif typ == list: |
|
||||||
# setattr(cls, name, attr.field(factory=list)) |
|
||||||
# elif typ == dict: |
|
||||||
# setattr(cls, name, attr.field(factory=dict)) |
|
||||||
# else: |
|
||||||
# raise ValueError(f"No default or factory defined for type {typ}") |
|
||||||
return cls |
return cls |
||||||
|
|
||||||
|
|
||||||
|
def auto_factory(): |
||||||
|
return auto_obj |
||||||
|
|
||||||
|
|
||||||
@dataclass |
@dataclass |
||||||
@apply_auto_defaults |
@apply_auto_defaults |
||||||
class CarControl: |
class CarControl: |
||||||
enabled: bool = auto_factory() # Automatically get a default of False |
enabled: bool = auto_factory() |
||||||
pts: list[int] = auto_factory() # Automatically get an empty list as the default |
pts: list[int] = auto_factory() |
||||||
|
logMonoTime: list[int] = field(default_factory=lambda: [1, 2, 3]) |
||||||
|
|
||||||
|
|
||||||
# This will now work with default values set by the decorator |
# This will now work with default values set by the decorator |
||||||
car_control_instance = CarControl(enabled=True,) |
car_control_instance = CarControl() |
||||||
print(car_control_instance.enabled) # Should print False |
print(car_control_instance.enabled) # Should print False |
||||||
print(car_control_instance.pts) # Should print [] |
print(car_control_instance.pts) # Should print [] |
||||||
|
Loading…
Reference in new issue