Preprocessor Assignment Pattern Support
This document lists which Python assignment patterns the nf_autoid preprocessor rewrites.
The codec only rewrites explicit bindings; a plain assignment is never rewritten. For the
full rules see the Preprocessor Quick Reference.
Supported Patterns
1. Annotated assignment with Bind
name: Bind = Construct(...) injects nf_id="name".
# coding: nf_autoid
from nf.core import App, Bind, BindContext, Stack
from nf.providers.hashicorp.aws import Vpc
with App() as app, Stack() as stack, BindContext:
vpc: Bind = Vpc(cidr_block="10.0.0.0/16") # nf_id: "vpc"
assert vpc.nf_id == "vpc"
2. Attribute assignment
The attribute name becomes the id. Bind[Vpc] keeps the type for type checkers.
# coding: nf_autoid
from nf.core import App, Bind, Stack
from nf.providers.hashicorp.aws import Vpc
class Network:
def __init__(self, stack: Stack) -> None:
self.vpc: Bind[Vpc] = Vpc(nf_scope=stack, cidr_block="10.0.0.0/16") # nf_id: "vpc"
with App(nf_id="net") as app, Stack(nf_id="main") as main:
network = Network(main)
assert network.vpc.nf_id == "vpc"
3. Assignment in various contexts
Functions, class bodies and methods all work, as long as the assignment is annotated with Bind.
# coding: nf_autoid
from nf.core import App, Bind
def create() -> App:
local_app: Bind = App() # nf_id: "local_app"
return local_app
class MyClass:
class_app: Bind = App() # nf_id: "class_app"
def __init__(self) -> None:
self.instance_app: Bind = App() # nf_id: "instance_app"
assert create().nf_id == "local_app"
assert MyClass.class_app.nf_id == "class_app"
assert MyClass().instance_app.nf_id == "instance_app"
4. with ... as name and BindContext
Each constructor bound with as name in a with statement that includes BindContext gets
nf_id="name".
# coding: nf_autoid
from nf.core import App, BindContext, Stack
with App() as app, Stack() as stack, BindContext:
pass
assert (app.nf_id, stack.nf_id) == ("app", "stack")
Not Rewritten
For each of these the id must be given with nf_id=; without it, creating the construct raises
ValidationError.
1. Plain assignment
app = App() # no annotation: no id
2. Conditional expression
app: Bind = App() if condition else None # the call is not the whole value
3. Walrus operator (assignment expression)
if app := App():
pass
4. Chained assignment
app1 = app2 = App()
5. Tuple, list and star unpacking
app1, app2 = App(), App()
[app3, app4] = [App(), App()]
first, *rest, last = App(), App(), App()
6. Subscript assignment
apps["key"] = App() # no id from the dictionary key
apps[0] = App() # no id from the list index
7. Comprehensions
apps = [App() for _ in range(3)] # items get no ids
app_dict = {f"app_{i}": App() for i in range(3)} # values get no ids
Workarounds
Several constructs
Bind each one:
# coding: nf_autoid
from nf.core import App, Bind
app1: Bind = App()
app2: Bind = App()
assert (app1.nf_id, app2.nf_id) == ("app1", "app2")
Loops, comprehensions and conditions
Give the id explicitly:
from nf.core import App
apps = [App(nf_id=f"app_{i}") for i in range(3)]
maybe_app = App(nf_id="maybe") if len(apps) > 2 else None
assert [a.nf_id for a in apps] == ["app_0", "app_1", "app_2"]
The same instance under two names
Plain assignment is fine when no new construct is made:
# coding: nf_autoid
from nf.core import App, Bind
app1: Bind = App()
app2 = app1
assert app2 is app1
Design Decisions
The preprocessor rewrites only bindings whose intent is unambiguous: an annotation with Bind,
or as name with BindContext. A plain assignment is not rewritten because the same
statement shape is used for values that are not constructs. Patterns such as tuple unpacking
are not supported because:
- Ambiguity: It is unclear how to map multiple values to multiple names.
- Complexity: Supporting every edge case would complicate the preprocessor.
- Alternatives: Explicit
nf_idis always available as a fallback.