2.2 KiB
2.2 KiB
name, enabled, event, action, conditions
| name | enabled | event | action | conditions | |||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| block-magic-numbers | true | file | block |
|
🚫 Code Quality Violation: Magic Number Detected
Your edit contains a magic number - a numeric literal that should be extracted to a named constant.
Why this is problematic:
- Magic numbers hide the intent of the code
- Hard to update when requirements change
- Makes code harder to understand and maintain
- Violates clean code principles
Examples of what triggers this rule:
# BAD - Magic numbers
timeout = 30
retry_count = 3
buffer_size = 1024
if attempts > 5:
...
# GOOD - Named constants
DEFAULT_TIMEOUT_SECONDS = 30
MAX_RETRY_COUNT = 3
BUFFER_SIZE_BYTES = 1024
MAX_ATTEMPTS = 5
How to fix:
- Define a constant at module level with a descriptive name
- Use
typing.Finalfor type-checked constants:from typing import Final MAX_RETRIES: Final = 3 DEFAULT_TIMEOUT_SECONDS: Final = 30 - Group related constants in a class or module
Exceptions (not blocked):
0and1(common defaults/incrementors)- Decimal literals with units in comments (e.g.,
30 # seconds) - Hex literals (
0x...) - Numbers in test files (use
@pytest.mark.parametrizeinstead) constants.*files - dedicated constant definition files are allowed
Project conventions:
- Constants go in
constants.pyfiles, the module where they're used, orconfig/settings.pyfor app-wide values - Use
SCREAMING_SNAKE_CASEfor constant names - Include unit suffix in name:
_SECONDS,_BYTES,_COUNT, etc.