Preprocessor Assignment Patterns
This document describes which Python patterns the nf_autoid preprocessor rewrites to inject nf_id. The rules are explicit: only a binding declared with Bind or BindContext is rewritten. The user-facing cheat sheet is Preprocessor Quick Reference.
The examples call preprocess_source, which the codec runs on a file's source at import time, and print the source it returns.
Rewritten
1. Annotated assignment with Bind
name: Bind = Construct(...) becomes name = Construct(..., nf_id="name"). The annotation is removed. An nf_id the call already has is kept. This works at module level, in a class body and in a function.
from nf.codec.autoid.preprocessor import preprocess_source
source = """
from nf.core import App, Bind, Stack
app: Bind = App()
named: Bind = App(nf_id="given")
class Network:
stack: Bind = Stack()
def build():
inner: Bind = Stack()
"""
processed = preprocess_source(source)
print(processed)
assert 'app = App(nf_id="app")' in processed
assert 'named = App(nf_id="given")' in processed
assert 'stack = Stack(nf_id="stack")' in processed
assert 'inner = Stack(nf_id="inner")' in processed
2. with statements with BindContext
Each constructor bound with as name in a with statement that also lists BindContext gets nf_id="name". BindContext itself is not a construct and is left as it is.
from nf.codec.autoid.preprocessor import preprocess_source
source = """
from nf.core import App, BindContext, Stack
with App() as app, Stack() as stack, BindContext:
pass
"""
processed = preprocess_source(source)
print(processed)
assert 'App(nf_id="app") as app' in processed
assert 'Stack(nf_id="stack") as stack' in processed
Not rewritten
These patterns keep the source as written, so the construct needs an explicit nf_id:
- plain assignments:
app = App() - attribute assignments:
self.app = App() - subscript assignments:
resources["vpc"] = Resource(...)andresources[i] = Resource(...) - chained assignments:
app1 = app2 = App() - tuple, list and star unpacking:
app1, app2 = App(), App() - walrus expressions:
if (app := App()): ... - calls that only pass a construct on:
resources.append(Resource(...)) - constructs inside list, dict or set literals, comprehensions, conditional expressions and lambdas
- a
withstatement withoutBindContext
from nf.codec.autoid.preprocessor import preprocess_source
source = """
from nf.core import App, Resource
app = App()
resources["vpc"] = Resource(nf_type="aws_vpc")
resources.append(Resource(nf_type="aws_subnet"))
first, second = App(), App()
apps = [App() for _ in range(3)]
with App() as unbound:
pass
"""
assert preprocess_source(source) == source
Workarounds
Pass the id explicitly:
from nf.core import App, Resource, Stack
app = App(nf_id="demo")
stack = Stack(nf_scope=app, nf_id="main")
resources = [Resource(nf_scope=stack, nf_type="aws_s3_bucket", nf_id=f"bucket-{i}") for i in range(3)]
assert [r.nf_id for r in resources] == ["bucket-0", "bucket-1", "bucket-2"]
Or bind each construct with Bind first, then collect them:
# coding: nf_autoid
from nf.core import App, Bind, BindContext, Resource, Stack
with App() as app, Stack() as stack, BindContext:
vpc: Bind = Resource(nf_scope=stack, nf_type="aws_vpc")
subnet: Bind = Resource(nf_scope=stack, nf_type="aws_subnet")
resources = {"vpc": vpc, "subnet": subnet}
assert vpc.nf_id == "vpc"
assert subnet.nf_id == "subnet"
Implementation details
The codec (netherframe.codec.autoid) is registered when the interpreter starts. For a file whose first or second line declares # coding: nf_autoid, it parses the source with libCST, rewrites the Bind and BindContext bindings above into plain constructor calls carrying nf_id, and compiles the result. No wrapper function is involved, so the rewritten code has no runtime overhead. Files without the declaration never import libCST.