Skip to content

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_autoid must be on the first or second line
  • Python uses one active source encoding per file
  • name: Bind = ... injects nf_id="name"
  • BindContext injects nf_id="name" for each constructor bound with as name in the same with statement
  • Entering an App or Stack with with supplies that construct as the current parent scope
  • @autoscope fills in nf_scope; it does not replace Bind or BindContext

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 encode to add the header across files, directories, or quoted globs
  • Use Bind for assignment-based ids
  • Use BindContext for with ... as name bindings
  • Use @autoscope on reusable App and Stack subclasses: their __init__ creates children with nf_scope=self
  • Use explicit nf_id for loops, helpers, and dynamic creation
  • Use explicit nf_scope when 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_scope and nf_id default to None in the signatures of generated constructs and of Stack, Output, Variable and Provider. Leaving either out is checked when the construct is created: a missing nf_id, or no nf_scope and no current scope, raises ValidationError
  • name: Bind = Vpc(...) declares name with no particular type, so the checker accepts it but does not check how name is used. Write name: Bind[Vpc] = Vpc(...) to keep the type
  • declare nf_scope and nf_id with a None default in your own App, Stack and Construct subclasses, 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 with binding includes BindContext

If you see:

LookupError: unknown encoding: nf_autoid

register the codec before import:

from nf.codec.autoid import register_codec

register_codec()

Next Docs