Skip to content

Commit

Permalink
add confilct_resolver_max_attempts property (#272)
Browse files Browse the repository at this point in the history
* 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 <fabrice.normandin@gmail.com>
  • Loading branch information
JonasFrey96 and lebrice committed Jul 11, 2023
1 parent d3b704a commit cd6fd81
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 4 additions & 3 deletions simple_parsing/conflicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 "
Expand All @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions simple_parsing/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit cd6fd81

Please sign in to comment.