From cd6fd812eb3b50039b53dc4931c5493658eaa3ce Mon Sep 17 00:00:00 2001 From: JonasFrey96 <56234378+JonasFrey96@users.noreply.github.com> Date: Wed, 12 Jul 2023 00:31:25 +0900 Subject: [PATCH] add confilct_resolver_max_attempts property (#272) * added confilct_resolver_max_attempts property * Simplify `max_attempts` attribute of ConflictResolver * Fix leftover `_max_attempts` in ConflictResolver * Fix leftover in simple_parsing/conflicts.py * Fix missing newline in simple_parsing/conflicts.py --------- Co-authored-by: Fabrice Normandin --- simple_parsing/conflicts.py | 7 ++++--- simple_parsing/parsing.py | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/simple_parsing/conflicts.py b/simple_parsing/conflicts.py index 315524d4..fb71ecde 100644 --- a/simple_parsing/conflicts.py +++ b/simple_parsing/conflicts.py @@ -61,6 +61,7 @@ def unflatten(possibly_related_wrappers: list[DataclassWrapper]) -> list[Datacla class ConflictResolver: def __init__(self, conflict_resolution=ConflictResolution.AUTO): self.conflict_resolution = conflict_resolution + self.max_attempts = 50 def resolve_and_flatten(self, wrappers: list[DataclassWrapper]) -> list[DataclassWrapper]: """Given the list of all dataclass wrappers, find and resolve any conflicts between fields. @@ -82,7 +83,7 @@ def resolve_and_flatten(self, wrappers: list[DataclassWrapper]) -> list[Dataclas conflict = self.get_conflict(wrappers_flat) # current and maximum number of attempts. When reached, raises an error. - cur_attempts, max_attempts = 0, 50 + cur_attempts = 0 while conflict: message: str = ( "The following wrappers are in conflict, as they share the " @@ -106,9 +107,9 @@ def resolve_and_flatten(self, wrappers: list[DataclassWrapper]) -> list[Dataclas conflict = self.get_conflict(wrappers_flat) cur_attempts += 1 - if cur_attempts == max_attempts: + if cur_attempts == self.max_attempts: raise ConflictResolutionError( - f"Reached maximum number of attempts ({max_attempts}) " + f"Reached maximum number of attempts ({self.max_attempts}) " "while trying to solve the conflicting argument names. " "This is either a bug, or there is something weird going " "on with your class hierarchy/argument names... \n" diff --git a/simple_parsing/parsing.py b/simple_parsing/parsing.py index 51d91f2e..ec81bdd1 100644 --- a/simple_parsing/parsing.py +++ b/simple_parsing/parsing.py @@ -976,6 +976,13 @@ def _fill_constructor_arguments_with_fields( return leftover_args, constructor_arguments + @property + def confilct_resolver_max_attempts(self) -> int: + return self._conflict_resolver.max_attempts + + @confilct_resolver_max_attempts.setter + def confilct_resolver_max_attempts(self, value: int): + self._conflict_resolver.max_attempts = value # TODO: Change the order of arguments to put `args` as the second argument. def parse(