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.
 
 
 
 
 
 

40 lines
1.5 KiB

class Auto:
"""A placeholder class to denote an automatically assigned default value."""
def __repr__(self):
return "<Auto>"
from dataclasses import dataclass, field, fields as dc_fields
from typing import Any, List
def apply_auto_defaults(cls):
for f in dc_fields(cls):
# Check if the field's default is an instance of Auto
if isinstance(f.default, Auto):
# Determine the appropriate default factory based on type hints
if f.type == bool:
default_factory = bool
elif f.type == list:
default_factory = list
elif f.type == int:
default_factory = int
else:
raise TypeError(f"Unsupported field type for auto-default: {f.type}")
# Replace the placeholder with an actual dataclass field with default_factory
setattr(cls, f.name, field(default_factory=default_factory))
return cls
@apply_auto_defaults
@dataclass
class CarControl:
enabled: bool = Auto() # Auto will be replaced with field(default_factory=bool)
speed: int = Auto() # Auto will be replaced with field(default_factory=int)
tags: List[str] = Auto() # Auto will be replaced with field(default_factory=list)
# This will instantiate the dataclass with the fields set to their default types
car_control = CarControl()
print(car_control.enabled) # Expected: False
print(car_control.speed) # Expected: 0
print(car_control.tags) # Expected: []