redline
LowError handlingassert-for-validation

assert used for validation

assert used for validation. assert in shipped code. Python drops asserts under -O, so this check can vanish.

This is how the comment reads inline, under the flagged line.

Why it matters

Asserts are for invariants in tests and debugging. Under python -O they are removed, so input validation built on them disappears.

What to do

Raise a real exception (ValueError, TypeError) for bad input.

Examples

Flagged
assert user_id, 'missing user'
Better
if not user_id:
    raise ValueError('missing user id')

What it catches

It reads a pattern on each line, aware of strings and comments. Languages: Python. Scope: Shipped code only. These cases are the rule's own tests: the first set must be flagged and the second must not.

Flagged

assert x > 0

Left alone

tests/test_a.py
assert x > 0

Research

`assert` used for runtime validation in Python. Python drops asserts under `-O`, so the check disappears.

Turn it off or change it

In a config file for the CLI, or in the API's rules field:

{
  "rules": {
    "disable": [
      "assert-for-validation"
    ],
    "severity": {
      "assert-for-validation": "medium"
    }
  }
}

Read the source or report a false positive.