Skip to content

Commit 45b9d95

Browse files
ashishenoypcopybara-github
authored andcommitted
Introduce immutable_fiddler as part of new flags API.
An immutable fiddler function is one that doesn't mutate the original `fdl.Buildable` object that's passed into it and instead returns a new `fdl.Buildable` built from the original `fdl.Buildable` after applying the transform `fn`. It is different from a mutable fiddler function that directly mutates the original `fdl.Buildable` object. Immutable fiddlers are useful when using various util functions such as `daglish.MemoizedTraversal.run()` that return a new `fdl.Buildable` object. PiperOrigin-RevId: 557925124
1 parent b5abdf1 commit 45b9d95

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

docs/flags_code_lab.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ Fiddle pattern includes:
6161
- **Base Configuration**: A base configuration function is a function without
6262
required arguments (a nullary function) that returns a `fdl.Buildable`.
6363
- **Fiddler**: A fiddler is a unary function that takes a `fdl.Buildable` and
64-
mutates it. (A fiddler returns `None`.)
64+
applies some transformations. If the fiddler returns `None`, then it is
65+
assumed the input `fdl.Buildable` was mutated by the fiddler, and the
66+
mutated input will be passed on to further flag operations. If the fiddler
67+
returns a non-`None` value (e.g., a new `fdl.Buildable` obtained from
68+
applying a `daglish` traversal routine), the return value of the fiddler is
69+
passed on instead.
6570

6671
## Life of a flag-augmented Fiddle program
6772

fiddle/_src/absl_flags/flags.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,21 @@ def _apply_fiddler(self, cfg: config.Buildable, expression: str):
8080
self.allow_imports,
8181
"Could not init a buildable from",
8282
)
83-
return fiddler(cfg, *call_expr.args, **call_expr.kwargs)
83+
84+
# An immutable fiddler function is one that doesn't mutate the original
85+
# `fdl.Buildable` object that's passed into it and instead returns a new
86+
# `fdl.Buildable` built from the original `fdl.Buildable` after applying the
87+
# transform `fiddler`. It is different from a mutable fiddler function that
88+
# directly mutates the original `fdl.Buildable` object. Immutable fiddlers
89+
# are useful when using various util functions such as
90+
# `daglish.MemoizedTraversal.run()` that return a new `fdl.Buildable`
91+
# object. Immutable fiddler functions return a non-None value.
92+
new_cfg = fiddler(cfg, *call_expr.args, **call_expr.kwargs)
93+
94+
# If the fiddler is immutable, then return the transformed `fdl.Buildable`
95+
# passed into the function. Else, return the mutated original
96+
# `fdl.Buildable` object.
97+
return new_cfg if new_cfg is not None else cfg
8498

8599
def parse(self, argument):
86100
parsed = self._parse(argument)
@@ -164,9 +178,9 @@ def main(argv) -> None:
164178
```
165179
166180
Invoking the above binary with:
167-
python3 -m path.to.my.binary --my_config config:base_config \
168-
--my_config fiddler:'set_attributes(value="float64")' --my_config \
169-
set:some_other_attr.enable=True
181+
python3 -m path.to.my.binary --my_config=config:base_config \
182+
--my_config=fiddler:'set_attributes(value="float64")' \
183+
--my_config=set:some_other_attr.enable=True
170184
171185
results in the `_MY_FLAG.value` set to the built config object with all the
172186
command line flags applied in the order they were passed in.

0 commit comments

Comments
 (0)