Skip to content

Commit

Permalink
Renamed preserve to overwrite
Browse files Browse the repository at this point in the history
Renamed the `preserve` property of `VariableAssignmentCommand` to
overwrite, changed its default to True, and reversed how it was used
that existing values are now preserved when it is not set.
  • Loading branch information
mwootendev committed Dec 23, 2023
1 parent 27e0c5c commit e4a3b2e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/dynamicprompts/commands/variable_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class VariableAssignmentCommand(Command):
name: str
value: Command
immediate: bool
preserve: bool = False
overwrite: bool = True
sampling_method = None


Expand Down
4 changes: 2 additions & 2 deletions src/dynamicprompts/parser/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def _configure_variable_assignment(
+ OPT_WS
+ var_name("name")
+ OPT_WS
+ pp.Opt(pp.Literal("?"))("preserve")
+ pp.Opt(pp.Literal("?"))("preserve_existing_value")
+ pp.Literal("=")
+ pp.Opt(pp.Literal("!"))("immediate")
+ OPT_WS
Expand Down Expand Up @@ -402,7 +402,7 @@ def _parse_variable_assignment_command(
return VariableAssignmentCommand(
name=parts["name"],
value=parts["value"],
preserve=("preserve" in parts),
overwrite=("preserve_existing_value" not in parts),
immediate=("immediate" in parts),
)

Expand Down
2 changes: 1 addition & 1 deletion src/dynamicprompts/sampling_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def process_variable_assignment(
self,
command: VariableAssignmentCommand,
) -> Command:
if command.preserve and command.name in self.variables:
if not command.overwrite and command.name in self.variables:
return self.variables[command.name]
if command.immediate:
if isinstance(command.value, LiteralCommand):
Expand Down
17 changes: 7 additions & 10 deletions tests/parser/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,17 +390,14 @@ def test_alternative_wildcard_wrap(self, wildcard_wrap: str, template: str):
assert variant.values[1].literal == "B"
assert variant.values[2].wildcard == "some/wildcard"

@pytest.mark.parametrize("immediate", (False, True), ids=("delayed", "immediate"))
@pytest.mark.parametrize(
("immediate", "preserve"),
[
(False, False),
(False, True),
(True, False),
(True, True),
],
"overwrite",
(False, True),
ids=("preserve existing value", "overwrite existing value"),
)
def test_variable_commands(self, immediate: bool, preserve: bool):
op = "?" if preserve else ""
def test_variable_commands(self, immediate: bool, overwrite: bool):
op = "?" if not overwrite else ""
op += "=!" if immediate else "="
sequence = cast(
SequenceCommand,
Expand All @@ -411,7 +408,7 @@ def test_variable_commands(self, immediate: bool, preserve: bool):
assert isinstance(ass, VariableAssignmentCommand)
assert ass.name == "animal"
assert ass.value == LiteralCommand("cat")
assert ass.preserve == preserve
assert ass.overwrite == overwrite
assert ass.immediate == immediate
acc = sequence[2]
assert isinstance(acc, VariableAccessCommand)
Expand Down

0 comments on commit e4a3b2e

Please sign in to comment.