thailint 0.2.0__py3-none-any.whl → 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- src/cli.py +30 -4
- src/config.py +6 -2
- src/core/base.py +90 -5
- src/linters/dry/block_filter.py +5 -2
- src/linters/dry/cache.py +46 -92
- src/linters/dry/config.py +17 -13
- src/linters/dry/duplicate_storage.py +17 -80
- src/linters/dry/file_analyzer.py +11 -48
- src/linters/dry/linter.py +5 -12
- src/linters/dry/python_analyzer.py +12 -1
- src/linters/dry/storage_initializer.py +9 -18
- src/linters/dry/violation_filter.py +4 -1
- src/linters/magic_numbers/__init__.py +48 -0
- src/linters/magic_numbers/config.py +71 -0
- src/linters/magic_numbers/context_analyzer.py +247 -0
- src/linters/magic_numbers/linter.py +452 -0
- src/linters/magic_numbers/python_analyzer.py +76 -0
- src/linters/magic_numbers/typescript_analyzer.py +217 -0
- src/linters/magic_numbers/violation_builder.py +98 -0
- src/linters/nesting/__init__.py +6 -2
- src/linters/nesting/config.py +6 -3
- src/linters/nesting/linter.py +8 -19
- src/linters/srp/__init__.py +3 -3
- src/linters/srp/config.py +12 -6
- src/linters/srp/linter.py +33 -24
- {thailint-0.2.0.dist-info → thailint-0.3.0.dist-info}/METADATA +196 -42
- {thailint-0.2.0.dist-info → thailint-0.3.0.dist-info}/RECORD +30 -23
- {thailint-0.2.0.dist-info → thailint-0.3.0.dist-info}/LICENSE +0 -0
- {thailint-0.2.0.dist-info → thailint-0.3.0.dist-info}/WHEEL +0 -0
- {thailint-0.2.0.dist-info → thailint-0.3.0.dist-info}/entry_points.txt +0 -0
src/linters/dry/file_analyzer.py
CHANGED
|
@@ -1,45 +1,32 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Purpose: File analysis orchestration for duplicate detection
|
|
3
3
|
|
|
4
|
-
Scope: Coordinates language-specific analyzers
|
|
4
|
+
Scope: Coordinates language-specific analyzers
|
|
5
5
|
|
|
6
|
-
Overview: Orchestrates file analysis by delegating to language-specific analyzers (Python, TypeScript)
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
SRP compliance.
|
|
6
|
+
Overview: Orchestrates file analysis by delegating to language-specific analyzers (Python, TypeScript).
|
|
7
|
+
Analyzes files fresh every run - no cache loading. Separates file analysis orchestration from
|
|
8
|
+
main linter rule logic to maintain SRP compliance.
|
|
10
9
|
|
|
11
|
-
Dependencies: PythonDuplicateAnalyzer, TypeScriptDuplicateAnalyzer,
|
|
10
|
+
Dependencies: PythonDuplicateAnalyzer, TypeScriptDuplicateAnalyzer, DRYConfig, CodeBlock
|
|
12
11
|
|
|
13
12
|
Exports: FileAnalyzer class
|
|
14
13
|
|
|
15
|
-
Interfaces: FileAnalyzer.
|
|
14
|
+
Interfaces: FileAnalyzer.analyze(file_path, content, language, config)
|
|
16
15
|
|
|
17
|
-
Implementation: Delegates to language-specific analyzers,
|
|
16
|
+
Implementation: Delegates to language-specific analyzers, always performs fresh analysis
|
|
18
17
|
"""
|
|
19
18
|
|
|
20
|
-
from dataclasses import dataclass
|
|
21
19
|
from pathlib import Path
|
|
22
20
|
|
|
23
21
|
from .block_filter import BlockFilterRegistry, create_default_registry
|
|
24
|
-
from .cache import CodeBlock
|
|
22
|
+
from .cache import CodeBlock
|
|
25
23
|
from .config import DRYConfig
|
|
26
24
|
from .python_analyzer import PythonDuplicateAnalyzer
|
|
27
25
|
from .typescript_analyzer import TypeScriptDuplicateAnalyzer
|
|
28
26
|
|
|
29
27
|
|
|
30
|
-
@dataclass
|
|
31
|
-
class FileAnalysisContext:
|
|
32
|
-
"""Context for file analysis."""
|
|
33
|
-
|
|
34
|
-
file_path: Path
|
|
35
|
-
content: str
|
|
36
|
-
language: str
|
|
37
|
-
config: DRYConfig
|
|
38
|
-
cache: DRYCache | None
|
|
39
|
-
|
|
40
|
-
|
|
41
28
|
class FileAnalyzer:
|
|
42
|
-
"""Orchestrates file analysis
|
|
29
|
+
"""Orchestrates file analysis for duplicate detection."""
|
|
43
30
|
|
|
44
31
|
def __init__(self, config: DRYConfig | None = None) -> None:
|
|
45
32
|
"""Initialize with language-specific analyzers.
|
|
@@ -77,49 +64,25 @@ class FileAnalyzer:
|
|
|
77
64
|
|
|
78
65
|
return registry
|
|
79
66
|
|
|
80
|
-
def
|
|
67
|
+
def analyze(
|
|
81
68
|
self,
|
|
82
69
|
file_path: Path,
|
|
83
70
|
content: str,
|
|
84
71
|
language: str,
|
|
85
72
|
config: DRYConfig,
|
|
86
|
-
cache: DRYCache | None = None,
|
|
87
73
|
) -> list[CodeBlock]:
|
|
88
|
-
"""Analyze file
|
|
74
|
+
"""Analyze file for duplicate code blocks.
|
|
89
75
|
|
|
90
76
|
Args:
|
|
91
77
|
file_path: Path to file
|
|
92
78
|
content: File content
|
|
93
79
|
language: File language
|
|
94
80
|
config: DRY configuration
|
|
95
|
-
cache: Optional cache instance
|
|
96
81
|
|
|
97
82
|
Returns:
|
|
98
83
|
List of CodeBlock instances
|
|
99
84
|
"""
|
|
100
|
-
# Check if file is fresh in cache
|
|
101
|
-
if cache:
|
|
102
|
-
mtime = file_path.stat().st_mtime
|
|
103
|
-
if cache.is_fresh(file_path, mtime):
|
|
104
|
-
return cache.load(file_path)
|
|
105
|
-
|
|
106
85
|
# Analyze file based on language
|
|
107
|
-
return self._analyze_file(file_path, content, language, config)
|
|
108
|
-
|
|
109
|
-
def _analyze_file(
|
|
110
|
-
self, file_path: Path, content: str, language: str, config: DRYConfig
|
|
111
|
-
) -> list[CodeBlock]:
|
|
112
|
-
"""Analyze file based on language.
|
|
113
|
-
|
|
114
|
-
Args:
|
|
115
|
-
file_path: Path to file
|
|
116
|
-
content: File content
|
|
117
|
-
language: File language
|
|
118
|
-
config: DRY configuration
|
|
119
|
-
|
|
120
|
-
Returns:
|
|
121
|
-
List of CodeBlock instances
|
|
122
|
-
"""
|
|
123
86
|
if language == "python":
|
|
124
87
|
return self._python_analyzer.analyze(file_path, content, config)
|
|
125
88
|
if language in ("typescript", "javascript"):
|
src/linters/dry/linter.py
CHANGED
|
@@ -37,7 +37,7 @@ from .storage_initializer import StorageInitializer
|
|
|
37
37
|
from .violation_generator import ViolationGenerator
|
|
38
38
|
|
|
39
39
|
if TYPE_CHECKING:
|
|
40
|
-
from .cache import CodeBlock
|
|
40
|
+
from .cache import CodeBlock
|
|
41
41
|
|
|
42
42
|
|
|
43
43
|
@dataclass
|
|
@@ -132,24 +132,17 @@ class DRYRule(BaseLintRule):
|
|
|
132
132
|
return # Should never happen after initialization
|
|
133
133
|
|
|
134
134
|
file_path = Path(context.file_path)
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
file_path, context.file_content, context.language, config, cache
|
|
135
|
+
blocks = self._file_analyzer.analyze(
|
|
136
|
+
file_path, context.file_content, context.language, config
|
|
138
137
|
)
|
|
139
138
|
|
|
140
139
|
if blocks:
|
|
141
140
|
self._store_blocks(file_path, blocks)
|
|
142
141
|
|
|
143
|
-
def _get_cache(self) -> DRYCache | None:
|
|
144
|
-
"""Get cache from storage if available."""
|
|
145
|
-
if not self._storage:
|
|
146
|
-
return None
|
|
147
|
-
return self._storage._cache # pylint: disable=protected-access
|
|
148
|
-
|
|
149
142
|
def _store_blocks(self, file_path: Path, blocks: list[CodeBlock]) -> None:
|
|
150
|
-
"""Store blocks in
|
|
143
|
+
"""Store blocks in SQLite if storage available."""
|
|
151
144
|
if self._storage:
|
|
152
|
-
self._storage.
|
|
145
|
+
self._storage.add_blocks(file_path, blocks)
|
|
153
146
|
|
|
154
147
|
def finalize(self) -> list[Violation]:
|
|
155
148
|
"""Generate violations after all files processed.
|
|
@@ -38,6 +38,10 @@ from .block_filter import BlockFilterRegistry, create_default_registry
|
|
|
38
38
|
from .cache import CodeBlock
|
|
39
39
|
from .config import DRYConfig
|
|
40
40
|
|
|
41
|
+
# AST context checking constants
|
|
42
|
+
AST_LOOKBACK_LINES = 10
|
|
43
|
+
AST_LOOKFORWARD_LINES = 5
|
|
44
|
+
|
|
41
45
|
# Type alias for AST nodes that have line number attributes
|
|
42
46
|
# All stmt and expr nodes have lineno and end_lineno after parsing
|
|
43
47
|
ASTWithLineNumbers = ast.stmt | ast.expr
|
|
@@ -514,4 +518,11 @@ class PythonDuplicateAnalyzer(BaseTokenAnalyzer): # thailint: ignore[srp.violat
|
|
|
514
518
|
return True
|
|
515
519
|
return False
|
|
516
520
|
|
|
517
|
-
return self._check_ast_context(
|
|
521
|
+
return self._check_ast_context(
|
|
522
|
+
lines,
|
|
523
|
+
start_line,
|
|
524
|
+
end_line,
|
|
525
|
+
AST_LOOKBACK_LINES,
|
|
526
|
+
AST_LOOKFORWARD_LINES,
|
|
527
|
+
is_within_class_body,
|
|
528
|
+
)
|
|
@@ -1,23 +1,21 @@
|
|
|
1
1
|
"""
|
|
2
2
|
Purpose: Storage initialization for DRY linter
|
|
3
3
|
|
|
4
|
-
Scope: Initializes DuplicateStorage with
|
|
4
|
+
Scope: Initializes DuplicateStorage with SQLite storage
|
|
5
5
|
|
|
6
|
-
Overview: Handles storage initialization based on DRY configuration. Creates SQLite
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
Overview: Handles storage initialization based on DRY configuration. Creates SQLite storage in
|
|
7
|
+
either memory or tempfile mode based on config.storage_mode. Separates initialization logic
|
|
8
|
+
from main linter rule to maintain SRP compliance.
|
|
9
9
|
|
|
10
|
-
Dependencies: BaseLintContext, DRYConfig, DRYCache, DuplicateStorage
|
|
10
|
+
Dependencies: BaseLintContext, DRYConfig, DRYCache, DuplicateStorage
|
|
11
11
|
|
|
12
12
|
Exports: StorageInitializer class
|
|
13
13
|
|
|
14
14
|
Interfaces: StorageInitializer.initialize(context, config) -> DuplicateStorage
|
|
15
15
|
|
|
16
|
-
Implementation: Creates
|
|
16
|
+
Implementation: Creates DRYCache with storage_mode, delegates to DuplicateStorage for management
|
|
17
17
|
"""
|
|
18
18
|
|
|
19
|
-
from pathlib import Path
|
|
20
|
-
|
|
21
19
|
from src.core.base import BaseLintContext
|
|
22
20
|
|
|
23
21
|
from .cache import DRYCache
|
|
@@ -36,16 +34,9 @@ class StorageInitializer:
|
|
|
36
34
|
config: DRY configuration
|
|
37
35
|
|
|
38
36
|
Returns:
|
|
39
|
-
DuplicateStorage instance
|
|
37
|
+
DuplicateStorage instance with SQLite storage
|
|
40
38
|
"""
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
# Use SQLite cache
|
|
44
|
-
metadata = getattr(context, "metadata", {})
|
|
45
|
-
project_root = metadata.get("_project_root", Path.cwd())
|
|
46
|
-
cache_path = project_root / config.cache_path
|
|
47
|
-
cache_path.parent.mkdir(parents=True, exist_ok=True)
|
|
48
|
-
cache = DRYCache(cache_path)
|
|
49
|
-
# else: cache = None triggers in-memory fallback in DuplicateStorage
|
|
39
|
+
# Create SQLite storage (in-memory or tempfile based on config)
|
|
40
|
+
cache = DRYCache(storage_mode=config.storage_mode)
|
|
50
41
|
|
|
51
42
|
return DuplicateStorage(cache)
|
|
@@ -18,6 +18,9 @@ Implementation: Iterates through sorted violations, keeps first of each overlapp
|
|
|
18
18
|
|
|
19
19
|
from src.core.types import Violation
|
|
20
20
|
|
|
21
|
+
# Default fallback for line count when parsing fails
|
|
22
|
+
DEFAULT_FALLBACK_LINE_COUNT = 5
|
|
23
|
+
|
|
21
24
|
|
|
22
25
|
class ViolationFilter:
|
|
23
26
|
"""Filters overlapping violations."""
|
|
@@ -88,4 +91,4 @@ class ViolationFilter:
|
|
|
88
91
|
end = message.index(" lines")
|
|
89
92
|
return int(message[start:end])
|
|
90
93
|
except (ValueError, IndexError):
|
|
91
|
-
return
|
|
94
|
+
return DEFAULT_FALLBACK_LINE_COUNT # Default fallback
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Purpose: Magic numbers linter package exports and convenience functions
|
|
3
|
+
|
|
4
|
+
Scope: Public API for magic numbers linter module
|
|
5
|
+
|
|
6
|
+
Overview: Provides the public interface for the magic numbers linter package. Exports main
|
|
7
|
+
MagicNumberRule class for use by the orchestrator and MagicNumberConfig for configuration.
|
|
8
|
+
Includes lint() convenience function that provides a simple API for running the magic numbers
|
|
9
|
+
linter on a file or directory without directly interacting with the orchestrator. This module
|
|
10
|
+
serves as the entry point for users of the magic numbers linter, hiding implementation details
|
|
11
|
+
and exposing only the essential components needed for linting operations.
|
|
12
|
+
|
|
13
|
+
Dependencies: .linter for MagicNumberRule, .config for MagicNumberConfig
|
|
14
|
+
|
|
15
|
+
Exports: MagicNumberRule class, MagicNumberConfig dataclass, lint() convenience function
|
|
16
|
+
|
|
17
|
+
Interfaces: lint(path, config) -> list[Violation] for simple linting operations
|
|
18
|
+
|
|
19
|
+
Implementation: Module-level exports with __all__ definition, convenience function wrapper
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from .config import MagicNumberConfig
|
|
23
|
+
from .linter import MagicNumberRule
|
|
24
|
+
|
|
25
|
+
__all__ = ["MagicNumberRule", "MagicNumberConfig", "lint"]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def lint(file_path: str, config: dict | None = None) -> list:
|
|
29
|
+
"""Convenience function for linting a file for magic numbers.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
file_path: Path to the file to lint
|
|
33
|
+
config: Optional configuration dictionary
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
List of violations found
|
|
37
|
+
"""
|
|
38
|
+
from pathlib import Path
|
|
39
|
+
|
|
40
|
+
from src.orchestrator.core import FileLintContext
|
|
41
|
+
|
|
42
|
+
rule = MagicNumberRule()
|
|
43
|
+
context = FileLintContext(
|
|
44
|
+
path=Path(file_path),
|
|
45
|
+
lang="python",
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
return rule.check(context)
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Purpose: Configuration schema for magic numbers linter
|
|
3
|
+
|
|
4
|
+
Scope: MagicNumberConfig dataclass with allowed_numbers and max_small_integer settings
|
|
5
|
+
|
|
6
|
+
Overview: Defines configuration schema for magic numbers linter. Provides MagicNumberConfig dataclass
|
|
7
|
+
with allowed_numbers set (default includes common acceptable numbers like 0, 1, 2, -1, 10, 100, 1000)
|
|
8
|
+
and max_small_integer threshold (default 10) for range() contexts. Supports per-file and per-directory
|
|
9
|
+
config overrides through from_dict class method. Validates that configuration values are appropriate
|
|
10
|
+
types. Integrates with orchestrator's configuration system to allow users to customize allowed numbers
|
|
11
|
+
via .thailint.yaml configuration files.
|
|
12
|
+
|
|
13
|
+
Dependencies: dataclasses for class definition, typing for type hints
|
|
14
|
+
|
|
15
|
+
Exports: MagicNumberConfig dataclass
|
|
16
|
+
|
|
17
|
+
Interfaces: MagicNumberConfig(allowed_numbers: set, max_small_integer: int, enabled: bool),
|
|
18
|
+
from_dict class method for loading configuration from dictionary
|
|
19
|
+
|
|
20
|
+
Implementation: Dataclass with validation and defaults, matches reference implementation patterns
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from dataclasses import dataclass, field
|
|
24
|
+
from typing import Any
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass
|
|
28
|
+
class MagicNumberConfig:
|
|
29
|
+
"""Configuration for magic numbers linter."""
|
|
30
|
+
|
|
31
|
+
enabled: bool = True
|
|
32
|
+
allowed_numbers: set[int | float] = field(default_factory=lambda: {-1, 0, 1, 2, 10, 100, 1000})
|
|
33
|
+
max_small_integer: int = 10
|
|
34
|
+
|
|
35
|
+
def __post_init__(self) -> None:
|
|
36
|
+
"""Validate configuration values."""
|
|
37
|
+
if self.max_small_integer <= 0:
|
|
38
|
+
raise ValueError(f"max_small_integer must be positive, got {self.max_small_integer}")
|
|
39
|
+
|
|
40
|
+
@classmethod
|
|
41
|
+
def from_dict(cls, config: dict[str, Any], language: str | None = None) -> "MagicNumberConfig":
|
|
42
|
+
"""Load configuration from dictionary with language-specific overrides.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
config: Dictionary containing configuration values
|
|
46
|
+
language: Programming language (python, typescript, javascript)
|
|
47
|
+
for language-specific settings
|
|
48
|
+
|
|
49
|
+
Returns:
|
|
50
|
+
MagicNumberConfig instance with values from dictionary
|
|
51
|
+
"""
|
|
52
|
+
# Get language-specific config if available
|
|
53
|
+
if language and language in config:
|
|
54
|
+
lang_config = config[language]
|
|
55
|
+
allowed_numbers = set(
|
|
56
|
+
lang_config.get(
|
|
57
|
+
"allowed_numbers", config.get("allowed_numbers", {-1, 0, 1, 2, 10, 100, 1000})
|
|
58
|
+
)
|
|
59
|
+
)
|
|
60
|
+
max_small_integer = lang_config.get(
|
|
61
|
+
"max_small_integer", config.get("max_small_integer", 10)
|
|
62
|
+
)
|
|
63
|
+
else:
|
|
64
|
+
allowed_numbers = set(config.get("allowed_numbers", {-1, 0, 1, 2, 10, 100, 1000}))
|
|
65
|
+
max_small_integer = config.get("max_small_integer", 10)
|
|
66
|
+
|
|
67
|
+
return cls(
|
|
68
|
+
enabled=config.get("enabled", True),
|
|
69
|
+
allowed_numbers=allowed_numbers,
|
|
70
|
+
max_small_integer=max_small_integer,
|
|
71
|
+
)
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Purpose: Analyzes contexts to determine if numeric literals are acceptable
|
|
3
|
+
|
|
4
|
+
Scope: Context detection for magic number acceptable usage patterns
|
|
5
|
+
|
|
6
|
+
Overview: Provides ContextAnalyzer class that determines whether a numeric literal is in an acceptable
|
|
7
|
+
context where it should not be flagged as a magic number. Detects acceptable contexts including
|
|
8
|
+
constant definitions (UPPERCASE names), small integers in range() or enumerate() calls, test files,
|
|
9
|
+
and configuration contexts. Uses AST node analysis to inspect parent nodes and determine the usage
|
|
10
|
+
pattern of numeric literals. Helps reduce false positives by distinguishing between legitimate
|
|
11
|
+
numeric literals and true magic numbers that should be extracted to constants. Method count (10)
|
|
12
|
+
exceeds SRP limit (8) because refactoring for A-grade complexity requires extracting helper methods.
|
|
13
|
+
Class maintains single responsibility of context analysis - all methods support this core purpose.
|
|
14
|
+
|
|
15
|
+
Dependencies: ast module for AST node types, pathlib for Path handling
|
|
16
|
+
|
|
17
|
+
Exports: ContextAnalyzer class
|
|
18
|
+
|
|
19
|
+
Interfaces: ContextAnalyzer.is_acceptable_context(node, parent, file_path, config) -> bool,
|
|
20
|
+
various helper methods for specific context checks
|
|
21
|
+
|
|
22
|
+
Implementation: AST parent node inspection, pattern matching for acceptable contexts, configurable
|
|
23
|
+
max_small_integer threshold for range detection
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
import ast
|
|
27
|
+
from pathlib import Path
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ContextAnalyzer: # thailint: ignore[srp]
|
|
31
|
+
"""Analyzes contexts to determine if numeric literals are acceptable."""
|
|
32
|
+
|
|
33
|
+
def is_acceptable_context(
|
|
34
|
+
self,
|
|
35
|
+
node: ast.Constant,
|
|
36
|
+
parent: ast.AST | None,
|
|
37
|
+
file_path: Path | None,
|
|
38
|
+
config: dict,
|
|
39
|
+
) -> bool:
|
|
40
|
+
"""Check if a numeric literal is in an acceptable context.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
node: The numeric constant node
|
|
44
|
+
parent: The parent node in the AST
|
|
45
|
+
file_path: Path to the file being analyzed
|
|
46
|
+
config: Configuration with allowed_numbers and max_small_integer
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
True if the context is acceptable and should not be flagged
|
|
50
|
+
"""
|
|
51
|
+
# File-level and definition checks
|
|
52
|
+
if self.is_test_file(file_path) or self.is_constant_definition(node, parent):
|
|
53
|
+
return True
|
|
54
|
+
|
|
55
|
+
# Usage pattern checks
|
|
56
|
+
return self._is_acceptable_usage_pattern(node, parent, config)
|
|
57
|
+
|
|
58
|
+
def _is_acceptable_usage_pattern(
|
|
59
|
+
self, node: ast.Constant, parent: ast.AST | None, config: dict
|
|
60
|
+
) -> bool:
|
|
61
|
+
"""Check if numeric literal is in acceptable usage pattern.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
node: The numeric constant node
|
|
65
|
+
parent: The parent node in the AST
|
|
66
|
+
config: Configuration with max_small_integer threshold
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
True if usage pattern is acceptable
|
|
70
|
+
"""
|
|
71
|
+
if self.is_small_integer_in_range(node, parent, config):
|
|
72
|
+
return True
|
|
73
|
+
|
|
74
|
+
if self.is_small_integer_in_enumerate(node, parent, config):
|
|
75
|
+
return True
|
|
76
|
+
|
|
77
|
+
return self.is_string_repetition(node, parent)
|
|
78
|
+
|
|
79
|
+
def is_test_file(self, file_path: Path | None) -> bool:
|
|
80
|
+
"""Check if the file is a test file.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
file_path: Path to the file
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
True if the file is a test file (matches test_*.py pattern)
|
|
87
|
+
"""
|
|
88
|
+
if not file_path:
|
|
89
|
+
return False
|
|
90
|
+
return file_path.name.startswith("test_") or "_test.py" in file_path.name
|
|
91
|
+
|
|
92
|
+
def is_constant_definition(self, node: ast.Constant, parent: ast.AST | None) -> bool:
|
|
93
|
+
"""Check if the number is part of an UPPERCASE constant definition.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
node: The numeric constant node
|
|
97
|
+
parent: The parent node in the AST
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
True if this is a constant definition
|
|
101
|
+
"""
|
|
102
|
+
if not self._is_assignment_node(parent):
|
|
103
|
+
return False
|
|
104
|
+
|
|
105
|
+
# Type narrowing: parent is ast.Assign after the check above
|
|
106
|
+
assert isinstance(parent, ast.Assign) # nosec B101
|
|
107
|
+
return self._has_constant_target(parent)
|
|
108
|
+
|
|
109
|
+
def _is_assignment_node(self, parent: ast.AST | None) -> bool:
|
|
110
|
+
"""Check if parent is an assignment node."""
|
|
111
|
+
return parent is not None and isinstance(parent, ast.Assign)
|
|
112
|
+
|
|
113
|
+
def _has_constant_target(self, parent: ast.Assign) -> bool:
|
|
114
|
+
"""Check if assignment has uppercase constant target."""
|
|
115
|
+
return any(
|
|
116
|
+
isinstance(target, ast.Name) and self._is_constant_name(target.id)
|
|
117
|
+
for target in parent.targets
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
def _is_constant_name(self, name: str) -> bool:
|
|
121
|
+
"""Check if a name follows constant naming convention.
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
name: Variable name to check
|
|
125
|
+
|
|
126
|
+
Returns:
|
|
127
|
+
True if the name is UPPERCASE (constant convention)
|
|
128
|
+
"""
|
|
129
|
+
return name.isupper() and len(name) > 1
|
|
130
|
+
|
|
131
|
+
def is_small_integer_in_range(
|
|
132
|
+
self, node: ast.Constant, parent: ast.AST | None, config: dict
|
|
133
|
+
) -> bool:
|
|
134
|
+
"""Check if this is a small integer used in range() call.
|
|
135
|
+
|
|
136
|
+
Args:
|
|
137
|
+
node: The numeric constant node
|
|
138
|
+
parent: The parent node in the AST
|
|
139
|
+
config: Configuration with max_small_integer threshold
|
|
140
|
+
|
|
141
|
+
Returns:
|
|
142
|
+
True if this is a small integer in range()
|
|
143
|
+
"""
|
|
144
|
+
if not isinstance(node.value, int):
|
|
145
|
+
return False
|
|
146
|
+
|
|
147
|
+
max_small_int = config.get("max_small_integer", 10)
|
|
148
|
+
if not 0 <= node.value <= max_small_int:
|
|
149
|
+
return False
|
|
150
|
+
|
|
151
|
+
return self._is_in_range_call(parent)
|
|
152
|
+
|
|
153
|
+
def is_small_integer_in_enumerate(
|
|
154
|
+
self, node: ast.Constant, parent: ast.AST | None, config: dict
|
|
155
|
+
) -> bool:
|
|
156
|
+
"""Check if this is a small integer used in enumerate() call.
|
|
157
|
+
|
|
158
|
+
Args:
|
|
159
|
+
node: The numeric constant node
|
|
160
|
+
parent: The parent node in the AST
|
|
161
|
+
config: Configuration with max_small_integer threshold
|
|
162
|
+
|
|
163
|
+
Returns:
|
|
164
|
+
True if this is a small integer in enumerate()
|
|
165
|
+
"""
|
|
166
|
+
if not isinstance(node.value, int):
|
|
167
|
+
return False
|
|
168
|
+
|
|
169
|
+
max_small_int = config.get("max_small_integer", 10)
|
|
170
|
+
if not 0 <= node.value <= max_small_int:
|
|
171
|
+
return False
|
|
172
|
+
|
|
173
|
+
return self._is_in_enumerate_call(parent)
|
|
174
|
+
|
|
175
|
+
def _is_in_range_call(self, parent: ast.AST | None) -> bool:
|
|
176
|
+
"""Check if the parent is a range() call.
|
|
177
|
+
|
|
178
|
+
Args:
|
|
179
|
+
parent: The parent node
|
|
180
|
+
|
|
181
|
+
Returns:
|
|
182
|
+
True if parent is range() call
|
|
183
|
+
"""
|
|
184
|
+
return (
|
|
185
|
+
isinstance(parent, ast.Call)
|
|
186
|
+
and isinstance(parent.func, ast.Name)
|
|
187
|
+
and parent.func.id == "range"
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
def _is_in_enumerate_call(self, parent: ast.AST | None) -> bool:
|
|
191
|
+
"""Check if the parent is an enumerate() call.
|
|
192
|
+
|
|
193
|
+
Args:
|
|
194
|
+
parent: The parent node
|
|
195
|
+
|
|
196
|
+
Returns:
|
|
197
|
+
True if parent is enumerate() call
|
|
198
|
+
"""
|
|
199
|
+
return (
|
|
200
|
+
isinstance(parent, ast.Call)
|
|
201
|
+
and isinstance(parent.func, ast.Name)
|
|
202
|
+
and parent.func.id == "enumerate"
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
def is_string_repetition(self, node: ast.Constant, parent: ast.AST | None) -> bool:
|
|
206
|
+
"""Check if this number is used in string repetition (e.g., "-" * 40).
|
|
207
|
+
|
|
208
|
+
Args:
|
|
209
|
+
node: The numeric constant node
|
|
210
|
+
parent: The parent node in the AST
|
|
211
|
+
|
|
212
|
+
Returns:
|
|
213
|
+
True if this is a string repetition pattern
|
|
214
|
+
"""
|
|
215
|
+
if not isinstance(node.value, int):
|
|
216
|
+
return False
|
|
217
|
+
|
|
218
|
+
if not isinstance(parent, ast.BinOp):
|
|
219
|
+
return False
|
|
220
|
+
|
|
221
|
+
if not isinstance(parent.op, ast.Mult):
|
|
222
|
+
return False
|
|
223
|
+
|
|
224
|
+
# Check if either operand is a string constant
|
|
225
|
+
return self._has_string_operand(parent)
|
|
226
|
+
|
|
227
|
+
def _has_string_operand(self, binop: ast.BinOp) -> bool:
|
|
228
|
+
"""Check if binary operation has a string operand.
|
|
229
|
+
|
|
230
|
+
Args:
|
|
231
|
+
binop: Binary operation node
|
|
232
|
+
|
|
233
|
+
Returns:
|
|
234
|
+
True if either left or right operand is a string constant
|
|
235
|
+
"""
|
|
236
|
+
return self._is_string_constant(binop.left) or self._is_string_constant(binop.right)
|
|
237
|
+
|
|
238
|
+
def _is_string_constant(self, node: ast.AST) -> bool:
|
|
239
|
+
"""Check if a node is a string constant.
|
|
240
|
+
|
|
241
|
+
Args:
|
|
242
|
+
node: AST node to check
|
|
243
|
+
|
|
244
|
+
Returns:
|
|
245
|
+
True if node is a Constant with string value
|
|
246
|
+
"""
|
|
247
|
+
return isinstance(node, ast.Constant) and isinstance(node.value, str)
|