openpilot is an open source driver assistance system. openpilot performs the functions of Automated Lane Centering and Adaptive Cruise Control for over 200 supported car makes and models.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

38 lines
851 B

# sentinel value to detect when to replace a field's default
from dataclasses import dataclass, field
auto = object()
def check_auto(name, bases, cls_dict):
default = 1
cls_annotations = cls_dict.get('__annotations__', {})
print('cls_annotations2', cls_annotations)
for name, val in cls_dict.items():
print('test', name, val)
if val == auto:
# cls_dict[name] = default
# cls_dict[name] = cls_annotations[name]
cls_dict[name] = field(default_factory=cls_annotations[name])
default += 1
cls = type(name, bases, cls_dict)
return cls
@dataclass(frozen=True)
class State(metaclass=check_auto):
test: int = auto
test2: str = auto
test3: bool = auto
s = State()
# print(s) # State(val_A=1, val_B=2, val_C=3)
# assert s.val_B == 2
#
# s = State(val_A=5)
# assert s.val_A == 5
# assert s.val_C == 3