Skip to main content

SKY-UC001 · Dead code

Unreachable Python code

Medium

The catalog gives no severity for this rule; the detector always reports MEDIUM.

Python statements that can never run. Typical cases are code after a return, raise, break or continue in the same block, the body of an if whose condition is always false, and a loop over an empty literal.

Languages
Python
Turned on by
skylos . --quality (or -a)
Quality gate
counted toward the gate limits (full and diff scans). Reported in the quality list, so it counts toward max_quality (10 by default), not max_dead_code.
Finding type
defect
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

The line was written to do something, and it silently does nothing. In the example below the rejected coupon is never logged because the call sits after the raise, so support cannot see why customers' coupons failed.

What Skylos flags

shop/coupons.py · PythonReported on line 10
"""Coupon handling.""" from shop.errors import CouponExpiredfrom shop.log import log_rejected_coupon  def apply_coupon(order, coupon):    if coupon.expired:        raise CouponExpired(coupon.code)        log_rejected_coupon(order, coupon)    order.discount = coupon.amount

What Skylos leaves alone

shop/refunds.py · Python
"""Refunds from the bank, not available yet."""  def iter_refunds(order):    """An empty generator until the bank API ships."""    return    yield

Why Skylos leaves it alone

The lone yield never runs, but its presence makes iter_refunds a generator, so callers can loop over it and get nothing. Skylos keeps a function's only yield (or yield None) for this idiom.

How Skylos decides

  1. Runs with --quality on Python files. Looks at the statement blocks of modules, functions, classes, if, loops, with and try bodies.
  2. Reports the first statement that follows a return, raise, break or continue in the same block, or an if/else whose branches all end that way, or a while True loop with no break.
  3. Reports the first statement of an if body whose condition is always false, of an else after a condition that is always true, of a while whose condition is always false, and of a for over an empty literal ([], (), {}, ""). A condition counts only when it is built from constants, not, and and or; sys.version_info checks within the project's requires-python range are left alone.
  4. Skips a function's only yield or yield None when it is the unreachable statement. Reports MEDIUM on the unreachable line.

How to fix it

Move the statement to where it can run, here before the raise, or delete it if it is no longer needed.

shop/coupons.py · Python · the fix, against the flagged file
"""Coupon handling.""" from shop.errors import CouponExpiredfrom shop.log import log_rejected_coupon  def apply_coupon(order, coupon):    if coupon.expired:Removed:         raise CouponExpired(coupon.code)        log_rejected_coupon(order, coupon)Added:         raise CouponExpired(coupon.code)    order.discount = coupon.amount

When it can be wrong

  • Code kept under if False: on purpose, for example to switch a block off during debugging, is reported.
  • It misses code after a call that never returns, such as sys.exit() or a function that always raises.

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

  • Put the marker on the unreachable line.

    Python
    log_rejected_coupon(order, coupon)  # skylos: ignore[SKY-UC001] kept for #77
  • [tool.skylos] ignore = ["SKY-UC001"] turns the rule off for the project.

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