Skip to content

Commit

Permalink
Initialize mutable values using __post__init__
Browse files Browse the repository at this point in the history
Faust doesn't handle using mutable defaults, all instances share
the same value!
  • Loading branch information
cjh1 committed Mar 19, 2024
1 parent 593661d commit 6348b7c
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions backend/faust/scan_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,20 @@ class ScanStatus(faust.Record, coerce=True):
created: Optional[str] = None
uuid: Optional[str] = None
host: Optional[str] = None
paths: List[str] = []
paths: List[str] = None
# map of path to progress for receiver
progress_by_path: Dict[str, int] = {}
progress_by_path: Dict[str, int] = None
modified: Optional[datetime] = None

# We have to initialize the list and dict in __post_init__ as unlike
# pydantic, faust does not deal with mutable default values!
def __post_init__(self):
if self.paths is None:
self.paths = []

if self.progress_by_path is None:
self.progress_by_path = {}

def progress(self):
try:
return round(sum(self.progress_by_path.values()) / 4)
Expand Down

0 comments on commit 6348b7c

Please sign in to comment.