Spaces:
Sleeping

File size: 1,597 Bytes
4d67f4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Clean-code fixture for the analyzer false-positive-rate benchmark.

This module is intentionally well-written: fully type-hinted public functions,
no mutable defaults, specific exception handling, no globals, no bare asserts,
and shallow nesting.  The analyzer should report zero target-class issues here.
"""

from __future__ import annotations


def add(a: int, b: int) -> int:
    """Return the sum of two integers."""
    return a + b


def safe_divide(numerator: float, denominator: float) -> float | None:
    """Divide two numbers, returning None on division by zero."""
    try:
        return numerator / denominator
    except ZeroDivisionError:
        return None


def accumulate(items: list[int] | None = None) -> list[int]:
    """Append to a fresh list, avoiding the mutable-default pitfall."""
    if items is None:
        items = []
    items.append(0)
    return items


def categorize(value: int) -> str:
    """Classify a value using flat guard clauses instead of deep nesting."""
    if value < 0:
        return "negative"
    if value == 0:
        return "zero"
    return "positive"


def validate_positive(value: int) -> int:
    """Validate input by raising an explicit exception (not assert)."""
    if value <= 0:
        raise ValueError("value must be positive")
    return value


class Counter:
    """A small stateful helper that avoids global state."""

    def __init__(self) -> None:
        self._count = 0

    def increment(self) -> int:
        """Increment the counter and return the new value."""
        self._count += 1
        return self._count