Metadata-Version: 2.4
Name: blazon-pylint-extensions
Version: 0.0.2
Summary: Additional Pylint checks for readable Python code
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: astroid<5,>=3.3
Requires-Dist: pylint<5,>=3.3
Provides-Extra: test
Requires-Dist: pytest<9,>=8.4; extra == "test"

# blazon-pylint-extensions

Additional Pylint checks for keeping Python code flat and readable.

## Requirements

- Python 3.10 or newer
- Pylint 3.3 or newer, below 5

## Installation

```sh
pip install blazon-pylint-extensions
```

## Configuration

Load the plugin in your Pylint configuration, such as `pyproject.toml`:

```toml
[tool.pylint.main]
load-plugins = ["blazon_pylint_extensions"]
```

To run only these checks, disable Pylint's other messages and enable the three plugin messages:

```sh
pylint --disable=all --enable=blazon-nested-if,blazon-nested-for,blazon-nested-while src/
```

## Rules

### No nested `if`

Message: `blazon-nested-if` (`E9501`)

Reports an `if` inside another `if`, including through loops, `try` blocks, or a `match` statement.
An `if` inside a `match` case is reported as well. Use an `elif` branch instead of an `if` as the
only statement in an `else` branch.

```python
# Reported: nested if
if user.is_active:
    if user.is_admin:  # blazon-nested-if
        grant_access(user)

# Reported: an if inside a match case
match event.kind:
    case "signup":
        if event.user.is_verified:  # blazon-nested-if
            welcome(event.user)

# Allowed: an elif chain is one conditional
if first_condition:
    first_action()
elif second_condition:
    second_action()
else:
    fallback()

# Allowed: use a guard clause to keep the checks at one level
if not user.is_active:
    return

if user.is_admin:
    grant_access(user)
```

Functions, async functions, and classes declared inside a conditional start a fresh nesting scope.

### No nested `for`

Message: `blazon-nested-for` (`E9502`)

Reports a `for` or `async for` inside an `if`, another `for`/`async for`, or a `while`. This catches
loop nesting that can obscure intent or increase work as the input grows. A `for` inside a `match`
case is allowed; the PHPStan rule this check follows does not treat `switch` as a nesting context
for `foreach`.

```python
# Reported: nested for loops
for order in orders:
    for line in order.lines:  # blazon-nested-for
        total += line.price

# Reported: a for inside an if
if order.is_paid:
    for line in order.lines:  # blazon-nested-for
        ship(line)

# Reported: a for inside a while
while batch := queue.next_batch():
    for job in batch:  # blazon-nested-for
        run(job)

# Allowed: a guard clause keeps the loop outside the conditional
if not order.is_paid:
    return

for line in order.lines:
    ship(line)

# Allowed: an if inside a for is not a nested-for finding
for item in items:
    if item.is_valid:
        process(item)
```

`async for` follows the same rule. Function, async function, and class bodies start fresh scopes.

### No nested `while`

Message: `blazon-nested-while` (`E9503`)

Reports a `while` inside an `if`, a `for`/`async for`, or another `while`.

```python
# Reported: nested while loops
while page := api.next_page():
    while item := page.next_item():  # blazon-nested-while
        import_item(item)

# Reported: a while inside a for
for feed in feeds:
    while entry := feed.read():  # blazon-nested-while
        store(entry)

# Reported: a while inside an if
if queue.is_open:
    while job := queue.pop():  # blazon-nested-while
        run(job)

# Allowed: an if inside a while is not a nested-while finding
while job := queue.pop():
    if job.is_valid:
        run(job)
```

Use a guard clause to move the loop out of an `if`, or extract the inner loop into a function with a
descriptive name. Function, async function, and class bodies start fresh scopes.

### Scope behavior

- Each offending statement is reported once at its own line. Every nested level is reported.
- `elif` chains are treated as one conditional; `else` branches remain part of their enclosing `if`.
- Python loop `else` clauses are outside the loop body for these checks.
- A nested function, async function, or class starts a fresh scope.
- Python `for` and `async for` correspond to PHP `foreach`; Python has no `do`/`while` statement.

## Development

Install the test dependencies and run the unit tests:

```sh
pip install -e '.[test]'
pytest
```
