RC2_STATE_AUGASSIGN_UNINIT
Cannot use augmented assignment on undeclared
self.<name>. Declare it in the class body, or assign a value before the augmented op.
What it means
You used an augmented operator (+=, -=, *=, …) on a self.X slot that doesn’t exist yet. Augmented operators read the current value, apply the operation, and write the result back — without a declaration there is no current value to read.
Why it happens
class Bad(Strategy): trade = Symbol() sizing = FixedCapital() # (no `count` declared in the class body)
def execute(self): self.count += 1 # ❌ — count has no declaration to start fromHow to fix it
Declare the state first. For scalars, either a class-body declaration or a first plain assignment will do.
Form A — class body:
class GoodA(Strategy): trade = Symbol() sizing = FixedCapital() count = 0
def execute(self): self.count += 1 # ✅ — initial value 0 from the class body if self.count > 5 and self.trade.is_flat: self.buy(self.trade)Form B — first plain assignment in execute(), augmented afterwards:
class GoodB(Strategy): trade = Symbol() sizing = FixedCapital()
def execute(self): self.count = self.count + 1 # the first assignment seeds the slot if self.count > 5 and self.trade.is_flat: self.buy(self.trade)Collections need the class-body declaration — there is no execute()-side discovery for them, because a bare first write carries no element type:
class GoodC(Strategy): trade = Symbol() sizing = FixedCapital() vals = [0.0]*5 counts = {"up": 0, "down": 0}
def execute(self): self.vals[0] += self.trade.close # ✅ self.counts["up"] += 1 # ✅ if self.trade.is_flat: self.buy(self.trade)Why not auto-init for augmented assignment?
self.x += 1 means “read x, add 1, write back”. If x doesn’t exist, the natural read returns the type default — but the type isn’t known yet. Defaulting to 0 (int) would be a footgun: a later self.x = 1.5 would mean the += site had been running on an int and silently truncating. Requiring the declaration removes the ambiguity instead of guessing at it.
Related
- Persistent State
- For Loops — augmented assignment pairs naturally with loop-driven buffers.
RC2_ASSIGN_LHS— the same idea for a plain subscript write into a collection that was never declared.RC2_STATE_REBIND_COLLECTION— a declared collection may be mutated, not re-assigned.