Preprocessor Quick Reference
Use this page as the cheat sheet for the current nf_autoid workflow.
For the full explanation, see Preprocessor Guide. For the user-facing workflow, see Custom Codec And Autoscope.
Minimal Setup
#!/usr/bin/env python3
# coding: nf_autoid
from nf.core import App, BindContext, Stack
with App() as app, Stack() as stack, BindContext:
...
Supported Rules
# coding: nf_autoidmust be on the first or second line- Python uses one active source encoding per file
name: Bind = ...injectsnf_id="name"BindContextinjectsnf_id="name"for each constructor bound withas namein the samewithstatement- Entering an
ApporStackwithwithsupplies that construct as the current parent scope @autoscopefills innf_scope; it does not replaceBindorBindContext
Not Rewritten By The Codec
- plain assignments like
vpc = Vpc(...) - append or list-building patterns
- subscript assignments like
items[i] = ... - chained assignments like
a = b = ... - dynamic cases where the stable id should come from explicit code instead
Good Defaults
- Use
nf encodeto add the header across files, directories, or quoted globs - Use
Bindfor assignment-based ids - Use
BindContextforwith ... as namebindings - Use
@autoscopeon reusableAppandStacksubclasses: their__init__creates children withnf_scope=self - Use explicit
nf_idfor loops, helpers, and dynamic creation - Use explicit
nf_scopewhen there is no active autoscope context
Type Checking
Type checkers read the source before the codec rewrites it, so they never see
the nf_id it injects or the nf_scope that @autoscope and with blocks
supply. Encoded files still type-check without relaxing any rule:
nf_scopeandnf_iddefault toNonein the signatures of generated constructs and ofStack,Output,VariableandProvider. Leaving either out is checked when the construct is created: a missingnf_id, or nonf_scopeand no current scope, raisesValidationErrorname: Bind = Vpc(...)declaresnamewith no particular type, so the checker accepts it but does not check hownameis used. Writename: Bind[Vpc] = Vpc(...)to keep the type- declare
nf_scopeandnf_idwith aNonedefault in your ownApp,StackandConstructsubclasses, so the codec's call sites type-check:
# coding: nf_autoid
from nf.core import App, Bind, Stack, autoscope
from nf.providers.hashicorp.aws import Vpc
@autoscope
class NetworkStack(Stack):
def __init__(self, *, nf_scope: App | None = None, nf_id: str | None = None, cidr: str) -> None:
super().__init__(nf_scope=nf_scope, nf_id=nf_id)
self.vpc: Bind[Vpc] = Vpc(cidr_block=cidr)
Providers generated by an older netherframe still require nf_scope and
nf_id: regenerate them (nf generate providers ...). Until then, relax the rule for
encoded files only, so that checks stay on elsewhere:
# pyproject.toml
[[tool.ty.overrides]]
include = ["src/infra/**/*.py"] # the files that start with `# coding: nf_autoid`
rules = { "missing-argument" = "ignore" }
With pyright, put # pyright: reportCallIssue=false at the top of each
encoded file.
Common Failure Modes
If ids are not being injected, check:
- the file actually has
# coding: nf_autoid - the assignment uses
: Bind - the
withbinding includesBindContext
If you see:
LookupError: unknown encoding: nf_autoid
register the codec before import:
from nf.codec.autoid import register_codec
register_codec()