Skip to content

Scope Precedence Rules in netherframe

The Precedence Order

When determining the scope (parent) for a construct, netherframe follows this strict precedence order:

1. Explicit Scope Parameter (Highest Priority)

If you provide an explicit scope parameter, it ALWAYS wins:

@autoscope
class MyStack(Stack):
    def __init__(self):
        super().__init__()
        # Even though @autoscope provides 'self' as context...
        # This resource becomes a child of other_stack, NOT self!
        self.resource = Resource(...)("id", scope=other_stack)

2. Auto-Detection (scope=None or not provided)

When scope is None or not provided, auto-detection occurs:

# These are equivalent - both trigger auto-detection:
Resource(...)("id")  # No scope parameter
Resource(...)("id", scope=None)  # Explicit None

Auto-detection checks the context stack (populated by @autoscope or with statements).

3. Context Stack (from @autoscope or with)

The context stack is checked from innermost to outermost:

@autoscope
class MyStack(Stack):  # @autoscope pushes 'self' onto context
    def __init__(self):
        super().__init__()

        # Uses self from @autoscope
        self.r1 = Resource(...)("r1")

        with other_stack:  # Temporarily overrides @autoscope
            # Uses other_stack (innermost context)
            self.r2 = Resource(...)("r2")

        # Back to using self from @autoscope
        self.r3 = Resource(...)("r3")

Code Implementation

The precedence is implemented in Construct.__call__() (base.py:120-122):

def __call__(self, nf_id, scope=None):
    # Auto-detect scope if not provided
    if scope is None and self.__class__.__name__ != "App":
        scope = self._detect_scope()  # Checks context stack

    # ... use the scope (either explicit or detected)

Real-World Examples

Example 1: Mixed Scopes in One Stack

app = App()("app")
stack1 = Stack()("stack1", scope=app)
stack2 = Stack()("stack2", scope=app)


@autoscope
class MyStack(Stack):
    def setup_resources(self):
        # Child of MyStack (from @autoscope)
        self.vpc = Resource(type="aws_vpc")("vpc")

        # Child of stack1 (explicit scope wins!)
        self.bucket = Resource(type="aws_s3_bucket")("bucket", scope=stack1)

        # Child of MyStack again (auto-detect)
        self.subnet = Resource(type="aws_subnet")("subnet", scope=None)


stack3 = MyStack()("stack3", scope=app)
stack3.setup_resources()

# Results:
# vpc.nf_path = "app/stack3/aws_vpc/vpc"        (in stack3)
# bucket.nf_path = "app/stack1/aws_s3_bucket/bucket"  (in stack1!)
# subnet.nf_path = "app/stack3/aws_subnet/subnet"     (in stack3)

Example 2: Template vs Bound Context

@autoscope
class MyStack(Stack):
    def __init__(self):
        super().__init__()

        # self is not bound yet here
        # This creates a deferred child (parent adoption later)
        self.vpc = Resource(type="aws_vpc")

        # This binds to external_stack immediately
        self.bucket = Resource(type="aws_s3_bucket")("bucket", scope=external_stack)


# After binding MyStack:
# - vpc gets adopted and bound by MyStack
# - bucket remains a child of external_stack

Example 3: Context Manager Stacking

@autoscope
class Stack1(Stack):
    pass


stack1 = Stack1()("s1", scope=app)
stack2 = Stack()("s2", scope=app)

with stack1:  # Push stack1 onto context
    r1 = Resource(...)("r1")  # Parent: stack1

    with stack2:  # Push stack2 (now innermost)
        r2 = Resource(...)("r2")  # Parent: stack2
        r3 = Resource(...)("r3", scope=stack1)  # Parent: stack1 (explicit!)

    r4 = Resource(...)("r4")  # Parent: stack1 (stack2 popped)

Important Rules

✅ What Works

  1. Explicit scope to any bound construct

    Resource(...)("id", scope=any_bound_stack)  # Works!
    

  2. Auto-detection with @autoscope or context managers

    @autoscope
    class MyStack(Stack):
        def __init__(self):
            self.r = Resource(...)("r")  # Auto-detects self
    

  3. Mixing bound and deferred resources

    self.r1 = Resource(...)  # Deferred, adopted later
    self.r2 = Resource(...)("r2", scope=bound_stack)  # Bound immediately
    

❌ What Doesn't Work

  1. Explicit scope to a not-yet-bound construct

    template_stack = Stack()  # Not bound yet
    Resource(...)("id", scope=template_stack)  # ERROR! No bound child registry
    

  2. No context and no explicit scope

    # Outside any context, no explicit scope
    Resource(...)("id")  # ERROR! Can't detect scope
    

Summary

The precedence rules are simple and predictable:

  1. Explicit wins: If you specify scope=something, that's what you get
  2. None means auto: scope=None triggers context detection
  3. Context stacks: Innermost context wins (@autoscope, with statements)
  4. Parent adoption: Deferred resources assigned to self.attr get adopted

This design allows maximum flexibility while maintaining predictable behavior. You can: - Let @autoscope handle everything automatically - Override selectively with explicit scopes - Mix patterns as needed for your use case