Skip to main content

SKY-Q302 · Quality, structure and performance

Deep nesting

Varies

The catalog gives no severity. Python reports MEDIUM up to two levels over the limit and HIGH beyond that; TypeScript, JavaScript, Java and Go always report MEDIUM.

Nesting depth is how many blocks (if, loops, try, switch) sit inside each other at the deepest point of a function. SKY-Q302 reports a function nested deeper than 3 levels in Python, or 4 in TS/JS, Java and Go.

Languages
Python, TypeScript, JavaScript, Java, Go
Quality gate
counted toward the gate limits (full and diff scans). Hidden when it is the only metric reported for the function. In Python and JS/TS it is listed when SKY-Q306 also reports the function, in Go and Java when another function metric does; the metrics then share one entry, led by the most severe, which the gate counts once. Otherwise it is counted only with --all-advice or when the rule is named in [tool.skylos.gate] rules.
Finding type
advice
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

Code four or five levels deep is hard to read and easy to break: the reader has to hold every enclosing condition in mind, and adding a case often goes into the wrong block.

What Skylos flags

app/billing.py · PythonReported on line 4
"""Stripe webhook handling for seat-based plans."""  def apply_invoice(event, seats):    if event["type"] == "invoice.paid":        for line in event["data"]["object"]["lines"]["data"]:            if line["price"]["recurring"]:                for _ in range(line["quantity"]):                    seats.activate(line["subscription"])    return seats

What Skylos leaves alone

app/billing.py · Python
"""Stripe webhook handling for seat-based plans.""" import logging log = logging.getLogger(__name__)  def apply_invoice(event, seats):    if event["type"] != "invoice.paid":        return seats    try:        for line in event["data"]["object"]["lines"]["data"]:            if line["price"]["recurring"]:                for _ in range(line["quantity"]):                    seats.activate(line["subscription"])    except KeyError:        log.warning("invoice event %s has no line items", event.get("id"))    return seats

Why Skylos leaves it alone

The event check is now a guard clause that returns early, and a try written directly in the function body does not add a level. The deepest line sits 3 levels down, which is the limit, not over it.

More languages

TypeScript

Flagged

src/cart/coupons.ts · TypeScriptReported on line 9
type Item = { sku: string; quantity: number; discount: number };type Coupon = {  sku: string;  amount: number;  minQuantity: number;  expiresAt: number;}; export function applyCoupons(items: Item[], coupons: Coupon[], now: number) {  for (const item of items) {    for (const coupon of coupons) {      if (coupon.sku === item.sku) {        if (coupon.expiresAt > now) {          if (item.quantity >= coupon.minQuantity) {            item.discount = coupon.amount;          }        }      }    }  }}

Not flagged

src/cart/coupons.ts · TypeScript
type Item = { sku: string; quantity: number; discount: number };type Coupon = {  sku: string;  amount: number;  minQuantity: number;  expiresAt: number;}; export function applyCoupons(items: Item[], coupons: Coupon[], now: number) {  for (const item of items) {    for (const coupon of coupons) {      if (coupon.sku === item.sku && coupon.expiresAt > now) {        if (item.quantity >= coupon.minQuantity) {          item.discount = coupon.amount;        }      }    }  }}

Why Skylos leaves it alone

Joining two checks with && leaves 4 levels. In TS/JS the limit is 4, so this is not reported, although the same depth in Python would be.

How Skylos decides

  1. Runs with --quality. Measures the deepest block inside each function or method. Nested functions, classes and lambdas are not counted here; each nested function is measured on its own.
  2. Python adds a level for if, for, async for, while and match (an else body sits at the same depth as its if body; an elif adds nothing). with adds no level. A try written directly in the function body, or inside a top-level with, adds no level; a try deeper down adds one.
  3. TypeScript and JavaScript add a level for if (not else if), every loop, switch and try. Java adds one for if, loops, switch and try; Go for if, for, switch, type switches and select.
  4. Reports a depth above the limit. Python reads nesting under [tool.skylos] (default 3). TS/JS, Java and Go always use 4 and ignore that setting.
  5. Python skips test code, examples and benchmarks folders, a top-level docs folder and the root setup.py, as SKY-Q301 does. Minified JavaScript is not scanned.
  6. Reports on the function's first line. Python reports MEDIUM up to two levels over the limit and HIGH beyond; the other languages report MEDIUM.

How to fix it

Return early for the cases you do not handle, and move the inner loop into a helper named after what it does.

app/billing.py · Python · the fix, against the flagged file
"""Stripe webhook handling for seat-based plans."""  Added: def _activate_line(line, seats):Added:     if not line["price"]["recurring"]:Added:         returnAdded:     for _ in range(line["quantity"]):Added:         seats.activate(line["subscription"])Added:  Added:  def apply_invoice(event, seats):Removed:     if event["type"] == "invoice.paid":Removed:         for line in event["data"]["object"]["lines"]["data"]:Removed:             if line["price"]["recurring"]:Removed:                 for _ in range(line["quantity"]):Removed:                     seats.activate(line["subscription"])Added:     if event["type"] != "invoice.paid":Added:         return seatsAdded:     for line in event["data"]["object"]["lines"]["data"]:Added:         _activate_line(line, seats)    return seats

When it can be wrong

  • Python and the other languages use different limits (3 and 4), and only the Python one can be changed, so the same code can be reported in one language and not in another.
  • A with block adds no level in Python, but a try inside an if does, so wrapping an existing block in try can tip a function over the limit.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • On the def line (the reported line):

    Python
    def apply_invoice(event, seats):  # skylos: ignore[SKY-Q302] mirrors the payload
  • [tool.skylos] ignore = ["SKY-Q302"] turns the rule off for the project; nesting = 4 under [tool.skylos] raises the Python limit instead. A baseline accepts existing findings, but it records the absolute file path and the line, so an entry stops matching when the checkout moves or the line shifts.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.