SKY-Q302 · Quality, structure and performance
Deep nesting
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-adviceor when the rule is named in[tool.skylos.gate] rules. - Finding type
- advice
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
"""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 seatsWhat Skylos leaves alone
"""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 seatsWhy 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
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
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
- 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. - Python adds a level for
if,for,async for,whileandmatch(anelsebody sits at the same depth as itsifbody; anelifadds nothing).withadds no level. Atrywritten directly in the function body, or inside a top-levelwith, adds no level; atrydeeper down adds one. - TypeScript and JavaScript add a level for
if(notelse if), every loop,switchandtry. Java adds one forif, loops,switchandtry; Go forif,for,switch, type switches andselect. - Reports a depth above the limit. Python reads
nestingunder[tool.skylos](default 3). TS/JS, Java and Go always use 4 and ignore that setting. - Python skips test code,
examplesandbenchmarksfolders, a top-leveldocsfolder and the rootsetup.py, as SKY-Q301 does. Minified JavaScript is not scanned. - 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.
"""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 seatsWhen 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
withblock adds no level in Python, but atryinside anifdoes, so wrapping an existing block intrycan 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
defline (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 = 4under[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.