Skip to content

netherframe Preprocessor Guide

This page is the technical reference for the current nf_autoid contract in this repo.

If you want the practical user workflow first, start with Custom Codec And Autoscope.

Current Contract

The current preprocessor is narrow on purpose.

  • # coding: nf_autoid enables the codec for one file
  • one file gets one active source encoding; Python does not chain multiple codecs
  • Bind-annotated assignments get nf_id injected at import time
  • with ... as name bindings get nf_id injected when BindContext is present
  • @autoscope handles nf_scope; it is separate from the codec
  • plain assignments are not rewritten just because the file uses nf_autoid

That means the supported workflow is explicit and predictable. The codec is not trying to rewrite every possible construct-creation pattern in Python.

If you want to opt existing files into the codec quickly, use nf encode.

Minimal Working Pattern

#!/usr/bin/env python3
# coding: nf_autoid

from nf.core import App, Bind, BindContext, Output, Stack
from nf.providers.hashicorp.aws import AwsProvider, Vpc


with App() as app, Stack() as stack, BindContext:
    with AwsProvider(region="us-east-1") as aws, BindContext:
        vpc: Bind = Vpc(cidr_block="10.0.0.0/16")

    vpc_id: Bind = Output(value=vpc.output.id)

In that file:

  • app, stack, and aws get ids from BindContext
  • vpc and vpc_id get ids from Bind
  • the codec does not infer nf_scope; that is a separate concern

What Gets Rewritten

Bind Assignments

#!/usr/bin/env python3
# coding: nf_autoid
from nf.core import App, Bind, BindContext, Stack
from nf.providers.hashicorp.aws import AwsProvider, S3Bucket, Vpc


with App() as app, Stack() as stack, BindContext:
    with AwsProvider(region="us-east-1") as aws, BindContext:
        vpc: Bind = Vpc(cidr_block="10.0.0.0/16")
        logs_bucket: Bind = S3Bucket(bucket="preprocessor-guide-logs-example")

Those become direct constructor calls with nf_id injected from the assigned name.

BindContext with Bindings

# coding: nf_autoid
from nf.core import App, BindContext, Stack

with App() as app, Stack() as stack, BindContext:
    ...

Those bindings become explicit ids at import time as well.

@autoscope

The codec only helps with nf_id. When you also want child constructs to default to nf_scope=self, use @autoscope on App or Stack subclasses.

from nf.core import App, Bind, Stack, autoscope
from nf.providers.hashicorp.aws import Vpc


@autoscope
class Network(Stack):
    def __init__(self, *, nf_scope: App | None = None, nf_id: str | None = None) -> None:
        super().__init__(nf_scope=nf_scope, nf_id=nf_id)
        self.vpc: Bind = Vpc(cidr_block="10.0.0.0/16")

Once super().__init__ has run, @autoscope makes the stack the current scope until __init__ returns, as with self: would. It does not cover other methods, and a Construct subclass is not made a scope: resources belong to stacks, so those created in its body go to the stack it is created in. You still need to declare nf_scope and nf_id in the subclass constructor and pass them through super().__init__(nf_scope=nf_scope, nf_id=nf_id).

What Does Not Get Rewritten

These are outside the current codec contract, so each needs an explicit nf_id:

# coding: nf_autoid
from nf.core import App, BindContext, Stack
from nf.providers.hashicorp.aws import Vpc

resources: list[Vpc] = []
items: dict[int, Vpc] = {}
i = 0

with App(nf_id="demo") as app, Stack() as stack, BindContext:
    vpc = Vpc(nf_id="vpc", cidr_block="10.0.0.0/16")  # plain assignment
    resources.append(Vpc(nf_id="appended", cidr_block="10.1.0.0/16"))  # append
    items[i] = Vpc(nf_id=f"item-{i}", cidr_block="10.2.0.0/16")  # dynamic subscript
    a = b = Vpc(nf_id="chained", cidr_block="10.3.0.0/16")  # chained assignment

Some of those cases may still work through netherframe's normal runtime auto-detection, but they are not part of the codec's supported import-time behavior.

When To Stay Explicit

Use explicit nf_id or nf_scope when the source structure is not the stable name or parent you want.

Common cases:

  • loops that create many resources
  • comprehensions
  • helper functions outside an autoscoped __init__
  • cross-scope construction
  • cases where a public or externally meaningful id should not depend on the Python variable name

Registration And Execution

The nf CLI registers the codec before importing encoded modules, so standard CLI workflows already handle nf_autoid.

To add the header across existing files, you can use:

nf encode app.py
nf encode scripts/
nf encode "examples/**/*.py"

If you run encoded modules directly and hit:

LookupError: unknown encoding: nf_autoid

register the codec first:

from nf.codec.autoid import register_codec

register_codec()

Installed environments also ship a .pth file that auto-registers the codec in common setups.

Python's source loader chooses one encoding for the file before parsing.

  • # coding: nf_autoid and # encoding: nf_autoid are equivalent spellings
  • only the first recognized encoding cookie in the first two lines matters
  • additional encoding cookies later in the file are just comments
  • there is no native way to pass structured arguments through the encoding cookie

If you need two preprocessing passes, compose them inside one codec or one custom loader instead of trying to declare multiple coding: values.

How To Read The Rest Of This Topic