fix: replace mutable default arguments in trainer.py

Using mutable default arguments (e.g. `= []`) in Python function
signatures is a well-known anti-pattern (W0102). The default list is
shared across all calls, which can lead to unexpected behavior if the
list is ever mutated in place.

Replace `= []` with `= None` and add explicit `None` guards for
`__move_components_to_device` and `__move_components_to_cpu`.
This commit is contained in:
Mr-Neutr0n 2026-02-12 00:12:48 +05:30
parent 7a1af71545
commit c84ebba304

View File

@ -713,7 +713,9 @@ class Trainer:
else:
raise ValueError(f"Invalid mixed precision: {self.args.mixed_precision}")
def __move_components_to_device(self, dtype, ignore_list: List[str] = []):
def __move_components_to_device(self, dtype, ignore_list: List[str] = None):
if ignore_list is None:
ignore_list = []
ignore_list = set(ignore_list)
components = self.components.model_dump()
for name, component in components.items():
@ -723,7 +725,9 @@ class Trainer:
self.components, name, component.to(self.accelerator.device, dtype=dtype)
)
def __move_components_to_cpu(self, unload_list: List[str] = []):
def __move_components_to_cpu(self, unload_list: List[str] = None):
if unload_list is None:
unload_list = []
unload_list = set(unload_list)
components = self.components.model_dump()
for name, component in components.items():