thailint 0.11.0__py3-none-any.whl → 0.13.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/analyzers/__init__.py +4 -3
- src/analyzers/ast_utils.py +54 -0
- src/analyzers/typescript_base.py +4 -0
- src/cli/__init__.py +3 -0
- src/cli/config.py +12 -12
- src/cli/config_merge.py +241 -0
- src/cli/linters/__init__.py +3 -0
- src/cli/linters/code_patterns.py +113 -5
- src/cli/linters/code_smells.py +118 -7
- src/cli/linters/documentation.py +3 -0
- src/cli/linters/structure.py +3 -0
- src/cli/linters/structure_quality.py +3 -0
- src/cli/utils.py +29 -9
- src/cli_main.py +3 -0
- src/config.py +2 -1
- src/core/base.py +3 -2
- src/core/cli_utils.py +3 -1
- src/core/config_parser.py +5 -2
- src/core/constants.py +54 -0
- src/core/linter_utils.py +4 -0
- src/core/rule_discovery.py +5 -1
- src/core/violation_builder.py +3 -0
- src/linter_config/directive_markers.py +109 -0
- src/linter_config/ignore.py +225 -383
- src/linter_config/pattern_utils.py +65 -0
- src/linter_config/rule_matcher.py +89 -0
- src/linters/collection_pipeline/any_all_analyzer.py +281 -0
- src/linters/collection_pipeline/ast_utils.py +40 -0
- src/linters/collection_pipeline/config.py +12 -0
- src/linters/collection_pipeline/continue_analyzer.py +2 -8
- src/linters/collection_pipeline/detector.py +262 -32
- src/linters/collection_pipeline/filter_map_analyzer.py +402 -0
- src/linters/collection_pipeline/linter.py +18 -35
- src/linters/collection_pipeline/suggestion_builder.py +68 -1
- src/linters/dry/base_token_analyzer.py +16 -9
- src/linters/dry/block_filter.py +7 -4
- src/linters/dry/cache.py +7 -2
- src/linters/dry/config.py +7 -1
- src/linters/dry/constant_matcher.py +34 -25
- src/linters/dry/file_analyzer.py +4 -2
- src/linters/dry/inline_ignore.py +7 -16
- src/linters/dry/linter.py +48 -25
- src/linters/dry/python_analyzer.py +18 -10
- src/linters/dry/python_constant_extractor.py +51 -52
- src/linters/dry/single_statement_detector.py +14 -12
- src/linters/dry/token_hasher.py +115 -115
- src/linters/dry/typescript_analyzer.py +11 -6
- src/linters/dry/typescript_constant_extractor.py +4 -0
- src/linters/dry/typescript_statement_detector.py +208 -208
- src/linters/dry/typescript_value_extractor.py +3 -0
- src/linters/dry/violation_filter.py +1 -4
- src/linters/dry/violation_generator.py +1 -4
- src/linters/file_header/atemporal_detector.py +4 -0
- src/linters/file_header/base_parser.py +4 -0
- src/linters/file_header/bash_parser.py +4 -0
- src/linters/file_header/field_validator.py +5 -8
- src/linters/file_header/linter.py +19 -12
- src/linters/file_header/markdown_parser.py +6 -0
- src/linters/file_placement/config_loader.py +3 -1
- src/linters/file_placement/linter.py +22 -8
- src/linters/file_placement/pattern_matcher.py +21 -4
- src/linters/file_placement/pattern_validator.py +21 -7
- src/linters/file_placement/rule_checker.py +2 -2
- src/linters/lazy_ignores/__init__.py +43 -0
- src/linters/lazy_ignores/config.py +66 -0
- src/linters/lazy_ignores/directive_utils.py +121 -0
- src/linters/lazy_ignores/header_parser.py +177 -0
- src/linters/lazy_ignores/linter.py +158 -0
- src/linters/lazy_ignores/matcher.py +135 -0
- src/linters/lazy_ignores/python_analyzer.py +201 -0
- src/linters/lazy_ignores/rule_id_utils.py +180 -0
- src/linters/lazy_ignores/skip_detector.py +298 -0
- src/linters/lazy_ignores/types.py +67 -0
- src/linters/lazy_ignores/typescript_analyzer.py +146 -0
- src/linters/lazy_ignores/violation_builder.py +131 -0
- src/linters/lbyl/__init__.py +29 -0
- src/linters/lbyl/config.py +63 -0
- src/linters/lbyl/pattern_detectors/__init__.py +25 -0
- src/linters/lbyl/pattern_detectors/base.py +46 -0
- src/linters/magic_numbers/context_analyzer.py +227 -229
- src/linters/magic_numbers/linter.py +20 -15
- src/linters/magic_numbers/python_analyzer.py +4 -16
- src/linters/magic_numbers/typescript_analyzer.py +9 -16
- src/linters/method_property/config.py +4 -0
- src/linters/method_property/linter.py +5 -4
- src/linters/method_property/python_analyzer.py +5 -4
- src/linters/method_property/violation_builder.py +3 -0
- src/linters/nesting/typescript_analyzer.py +6 -12
- src/linters/nesting/typescript_function_extractor.py +0 -4
- src/linters/print_statements/linter.py +6 -4
- src/linters/print_statements/python_analyzer.py +85 -81
- src/linters/print_statements/typescript_analyzer.py +6 -15
- src/linters/srp/heuristics.py +4 -4
- src/linters/srp/linter.py +12 -12
- src/linters/srp/violation_builder.py +0 -4
- src/linters/stateless_class/linter.py +30 -36
- src/linters/stateless_class/python_analyzer.py +11 -20
- src/linters/stringly_typed/__init__.py +22 -9
- src/linters/stringly_typed/config.py +32 -8
- src/linters/stringly_typed/context_filter.py +451 -0
- src/linters/stringly_typed/function_call_violation_builder.py +135 -0
- src/linters/stringly_typed/ignore_checker.py +102 -0
- src/linters/stringly_typed/ignore_utils.py +51 -0
- src/linters/stringly_typed/linter.py +376 -0
- src/linters/stringly_typed/python/__init__.py +9 -5
- src/linters/stringly_typed/python/analyzer.py +159 -9
- src/linters/stringly_typed/python/call_tracker.py +175 -0
- src/linters/stringly_typed/python/comparison_tracker.py +257 -0
- src/linters/stringly_typed/python/condition_extractor.py +3 -0
- src/linters/stringly_typed/python/conditional_detector.py +4 -1
- src/linters/stringly_typed/python/match_analyzer.py +8 -2
- src/linters/stringly_typed/python/validation_detector.py +3 -0
- src/linters/stringly_typed/storage.py +630 -0
- src/linters/stringly_typed/storage_initializer.py +45 -0
- src/linters/stringly_typed/typescript/__init__.py +28 -0
- src/linters/stringly_typed/typescript/analyzer.py +157 -0
- src/linters/stringly_typed/typescript/call_tracker.py +335 -0
- src/linters/stringly_typed/typescript/comparison_tracker.py +378 -0
- src/linters/stringly_typed/violation_generator.py +405 -0
- src/orchestrator/core.py +13 -4
- src/templates/thailint_config_template.yaml +166 -0
- src/utils/project_root.py +3 -0
- thailint-0.13.0.dist-info/METADATA +184 -0
- thailint-0.13.0.dist-info/RECORD +189 -0
- thailint-0.11.0.dist-info/METADATA +0 -1661
- thailint-0.11.0.dist-info/RECORD +0 -150
- {thailint-0.11.0.dist-info → thailint-0.13.0.dist-info}/WHEEL +0 -0
- {thailint-0.11.0.dist-info → thailint-0.13.0.dist-info}/entry_points.txt +0 -0
- {thailint-0.11.0.dist-info → thailint-0.13.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Purpose: Configuration dataclass for LBYL linter
|
|
3
|
+
|
|
4
|
+
Scope: Pattern toggles, ignore patterns, and validation
|
|
5
|
+
|
|
6
|
+
Overview: Provides LBYLConfig dataclass with pattern-specific toggles for each LBYL
|
|
7
|
+
pattern type (dict_key, hasattr, isinstance, file_exists, len_check, none_check,
|
|
8
|
+
string_validation, division_check). Some patterns like isinstance and none_check
|
|
9
|
+
are disabled by default due to many valid use cases. Configuration can be loaded
|
|
10
|
+
from dictionary (YAML) with sensible defaults.
|
|
11
|
+
|
|
12
|
+
Dependencies: dataclasses, typing
|
|
13
|
+
|
|
14
|
+
Exports: LBYLConfig
|
|
15
|
+
|
|
16
|
+
Interfaces: LBYLConfig.from_dict() for YAML configuration loading
|
|
17
|
+
|
|
18
|
+
Implementation: Dataclass with factory defaults and conservative default settings
|
|
19
|
+
|
|
20
|
+
Suppressions:
|
|
21
|
+
too-many-instance-attributes: Configuration dataclass requires many toggles
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from dataclasses import dataclass, field
|
|
25
|
+
from typing import Any
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass
|
|
29
|
+
class LBYLConfig: # pylint: disable=too-many-instance-attributes
|
|
30
|
+
"""Configuration for LBYL linter."""
|
|
31
|
+
|
|
32
|
+
enabled: bool = True
|
|
33
|
+
|
|
34
|
+
# Pattern toggles
|
|
35
|
+
detect_dict_key: bool = True
|
|
36
|
+
detect_hasattr: bool = True
|
|
37
|
+
detect_isinstance: bool = False # Disabled - many valid uses for type narrowing
|
|
38
|
+
detect_file_exists: bool = True
|
|
39
|
+
detect_len_check: bool = True
|
|
40
|
+
detect_none_check: bool = False # Disabled - many valid uses
|
|
41
|
+
detect_string_validation: bool = True
|
|
42
|
+
detect_division_check: bool = True
|
|
43
|
+
|
|
44
|
+
# File patterns to ignore
|
|
45
|
+
ignore: list[str] = field(default_factory=list)
|
|
46
|
+
|
|
47
|
+
@classmethod
|
|
48
|
+
def from_dict(cls, config: dict[str, Any], language: str | None = None) -> "LBYLConfig":
|
|
49
|
+
"""Load configuration from dictionary."""
|
|
50
|
+
# Language parameter reserved for future multi-language support
|
|
51
|
+
_ = language
|
|
52
|
+
return cls(
|
|
53
|
+
enabled=config.get("enabled", True),
|
|
54
|
+
detect_dict_key=config.get("detect_dict_key", True),
|
|
55
|
+
detect_hasattr=config.get("detect_hasattr", True),
|
|
56
|
+
detect_isinstance=config.get("detect_isinstance", False),
|
|
57
|
+
detect_file_exists=config.get("detect_file_exists", True),
|
|
58
|
+
detect_len_check=config.get("detect_len_check", True),
|
|
59
|
+
detect_none_check=config.get("detect_none_check", False),
|
|
60
|
+
detect_string_validation=config.get("detect_string_validation", True),
|
|
61
|
+
detect_division_check=config.get("detect_division_check", True),
|
|
62
|
+
ignore=config.get("ignore", []),
|
|
63
|
+
)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Purpose: Pattern detector exports for LBYL linter
|
|
3
|
+
|
|
4
|
+
Scope: All AST-based pattern detectors for LBYL anti-pattern detection
|
|
5
|
+
|
|
6
|
+
Overview: Exports pattern detector classes for the LBYL linter. Each detector is an
|
|
7
|
+
AST NodeVisitor that identifies specific LBYL anti-patterns. Detectors include
|
|
8
|
+
dict key checking, hasattr, isinstance, file exists, length checks, None checks,
|
|
9
|
+
string validation, and division safety checks.
|
|
10
|
+
|
|
11
|
+
Dependencies: ast module, base detector class
|
|
12
|
+
|
|
13
|
+
Exports: BaseLBYLDetector, LBYLPattern
|
|
14
|
+
|
|
15
|
+
Interfaces: find_patterns(tree: ast.AST) -> list[LBYLPattern]
|
|
16
|
+
|
|
17
|
+
Implementation: Modular detector pattern for extensible LBYL detection
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from .base import BaseLBYLDetector, LBYLPattern
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"BaseLBYLDetector",
|
|
24
|
+
"LBYLPattern",
|
|
25
|
+
]
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Purpose: Base class for LBYL pattern detectors
|
|
3
|
+
|
|
4
|
+
Scope: Abstract base providing common detector interface
|
|
5
|
+
|
|
6
|
+
Overview: Defines BaseLBYLDetector abstract class that all pattern detectors extend.
|
|
7
|
+
Inherits from ast.NodeVisitor for AST traversal. Defines LBYLPattern base dataclass
|
|
8
|
+
for representing detected patterns with line number and column information. Each
|
|
9
|
+
concrete detector implements find_patterns() to identify specific LBYL anti-patterns.
|
|
10
|
+
|
|
11
|
+
Dependencies: abc, ast, dataclasses
|
|
12
|
+
|
|
13
|
+
Exports: BaseLBYLDetector, LBYLPattern
|
|
14
|
+
|
|
15
|
+
Interfaces: find_patterns(tree: ast.AST) -> list[LBYLPattern]
|
|
16
|
+
|
|
17
|
+
Implementation: Abstract base with NodeVisitor pattern for extensibility
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import ast
|
|
21
|
+
from abc import ABC, abstractmethod
|
|
22
|
+
from dataclasses import dataclass
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass
|
|
26
|
+
class LBYLPattern:
|
|
27
|
+
"""Base pattern data for detected LBYL anti-patterns."""
|
|
28
|
+
|
|
29
|
+
line_number: int
|
|
30
|
+
column: int
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class BaseLBYLDetector(ast.NodeVisitor, ABC):
|
|
34
|
+
"""Base class for LBYL pattern detectors."""
|
|
35
|
+
|
|
36
|
+
@abstractmethod
|
|
37
|
+
def find_patterns(self, tree: ast.AST) -> list[LBYLPattern]:
|
|
38
|
+
"""Find LBYL patterns in AST.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
tree: Python AST to analyze
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
List of detected LBYL patterns
|
|
45
|
+
"""
|
|
46
|
+
raise NotImplementedError
|
|
@@ -3,249 +3,247 @@ Purpose: Analyzes contexts to determine if numeric literals are acceptable
|
|
|
3
3
|
|
|
4
4
|
Scope: Context detection for magic number acceptable usage patterns
|
|
5
5
|
|
|
6
|
-
Overview: Provides
|
|
6
|
+
Overview: Provides module-level functions that determine whether a numeric literal is in an acceptable
|
|
7
7
|
context where it should not be flagged as a magic number. Detects acceptable contexts including
|
|
8
8
|
constant definitions (UPPERCASE names), small integers in range() or enumerate() calls, test files,
|
|
9
9
|
and configuration contexts. Uses AST node analysis to inspect parent nodes and determine the usage
|
|
10
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.
|
|
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.
|
|
11
|
+
numeric literals and true magic numbers that should be extracted to constants.
|
|
14
12
|
|
|
15
13
|
Dependencies: ast module for AST node types, pathlib for Path handling
|
|
16
14
|
|
|
17
|
-
Exports:
|
|
15
|
+
Exports: is_acceptable_context function and helper functions
|
|
18
16
|
|
|
19
|
-
Interfaces:
|
|
20
|
-
various helper methods for specific context checks
|
|
17
|
+
Interfaces: is_acceptable_context(node, parent, file_path, config) -> bool
|
|
21
18
|
|
|
22
19
|
Implementation: AST parent node inspection, pattern matching for acceptable contexts, configurable
|
|
23
20
|
max_small_integer threshold for range detection
|
|
21
|
+
|
|
22
|
+
Suppressions:
|
|
23
|
+
- B101: Assert used for internal invariant checking, not security validation
|
|
24
24
|
"""
|
|
25
25
|
|
|
26
26
|
import ast
|
|
27
27
|
from pathlib import Path
|
|
28
28
|
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
node:
|
|
40
|
-
parent:
|
|
41
|
-
file_path: Path
|
|
42
|
-
config:
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
"""
|
|
251
|
-
return isinstance(node, ast.Constant) and isinstance(node.value, str)
|
|
30
|
+
def is_acceptable_context(
|
|
31
|
+
node: ast.Constant,
|
|
32
|
+
parent: ast.AST | None,
|
|
33
|
+
file_path: Path | None,
|
|
34
|
+
config: dict,
|
|
35
|
+
) -> bool:
|
|
36
|
+
"""Check if a numeric literal is in an acceptable context.
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
node: The numeric constant node
|
|
40
|
+
parent: The parent node in the AST
|
|
41
|
+
file_path: Path to the file being analyzed
|
|
42
|
+
config: Configuration with allowed_numbers and max_small_integer
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
True if the context is acceptable and should not be flagged
|
|
46
|
+
"""
|
|
47
|
+
# File-level and definition checks
|
|
48
|
+
if is_test_file(file_path) or is_constant_definition(node, parent):
|
|
49
|
+
return True
|
|
50
|
+
|
|
51
|
+
# Usage pattern checks
|
|
52
|
+
return _is_acceptable_usage_pattern(node, parent, config)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _is_acceptable_usage_pattern(node: ast.Constant, parent: ast.AST | None, config: dict) -> bool:
|
|
56
|
+
"""Check if numeric literal is in acceptable usage pattern.
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
node: The numeric constant node
|
|
60
|
+
parent: The parent node in the AST
|
|
61
|
+
config: Configuration with max_small_integer threshold
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
True if usage pattern is acceptable
|
|
65
|
+
"""
|
|
66
|
+
if is_small_integer_in_range(node, parent, config):
|
|
67
|
+
return True
|
|
68
|
+
|
|
69
|
+
if is_small_integer_in_enumerate(node, parent, config):
|
|
70
|
+
return True
|
|
71
|
+
|
|
72
|
+
return is_string_repetition(node, parent)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def is_test_file(file_path: Path | None) -> bool:
|
|
76
|
+
"""Check if the file is a test file.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
file_path: Path to the file
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
True if the file is a test file (matches test_*.py pattern)
|
|
83
|
+
"""
|
|
84
|
+
if not file_path:
|
|
85
|
+
return False
|
|
86
|
+
return file_path.name.startswith("test_") or "_test.py" in file_path.name
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def is_constant_definition(node: ast.Constant, parent: ast.AST | None) -> bool:
|
|
90
|
+
"""Check if the number is part of an UPPERCASE constant definition.
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
node: The numeric constant node
|
|
94
|
+
parent: The parent node in the AST
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
True if this is a constant definition
|
|
98
|
+
"""
|
|
99
|
+
if not _is_assignment_node(parent):
|
|
100
|
+
return False
|
|
101
|
+
|
|
102
|
+
# Type narrowing: parent is ast.Assign after the check above
|
|
103
|
+
assert isinstance(parent, ast.Assign) # nosec B101
|
|
104
|
+
return _has_constant_target(parent)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _is_assignment_node(parent: ast.AST | None) -> bool:
|
|
108
|
+
"""Check if parent is an assignment node."""
|
|
109
|
+
return parent is not None and isinstance(parent, ast.Assign)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _has_constant_target(parent: ast.Assign) -> bool:
|
|
113
|
+
"""Check if assignment has uppercase constant target."""
|
|
114
|
+
return any(
|
|
115
|
+
isinstance(target, ast.Name) and _is_constant_name(target.id) for target in parent.targets
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _is_constant_name(name: str) -> bool:
|
|
120
|
+
"""Check if a name follows constant naming convention.
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
name: Variable name to check
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
True if the name is UPPERCASE (constant convention)
|
|
127
|
+
"""
|
|
128
|
+
return name.isupper() and len(name) > 1
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def is_small_integer_in_range(node: ast.Constant, parent: ast.AST | None, config: dict) -> bool:
|
|
132
|
+
"""Check if this is a small integer used in range() call.
|
|
133
|
+
|
|
134
|
+
Args:
|
|
135
|
+
node: The numeric constant node
|
|
136
|
+
parent: The parent node in the AST
|
|
137
|
+
config: Configuration with max_small_integer threshold
|
|
138
|
+
|
|
139
|
+
Returns:
|
|
140
|
+
True if this is a small integer in range()
|
|
141
|
+
"""
|
|
142
|
+
if not isinstance(node.value, int):
|
|
143
|
+
return False
|
|
144
|
+
|
|
145
|
+
max_small_int = config.get("max_small_integer", 10)
|
|
146
|
+
if not 0 <= node.value <= max_small_int:
|
|
147
|
+
return False
|
|
148
|
+
|
|
149
|
+
return _is_in_range_call(parent)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def is_small_integer_in_enumerate(node: ast.Constant, parent: ast.AST | None, config: dict) -> bool:
|
|
153
|
+
"""Check if this is a small integer used in enumerate() call.
|
|
154
|
+
|
|
155
|
+
Args:
|
|
156
|
+
node: The numeric constant node
|
|
157
|
+
parent: The parent node in the AST
|
|
158
|
+
config: Configuration with max_small_integer threshold
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
True if this is a small integer in enumerate()
|
|
162
|
+
"""
|
|
163
|
+
if not isinstance(node.value, int):
|
|
164
|
+
return False
|
|
165
|
+
|
|
166
|
+
max_small_int = config.get("max_small_integer", 10)
|
|
167
|
+
if not 0 <= node.value <= max_small_int:
|
|
168
|
+
return False
|
|
169
|
+
|
|
170
|
+
return _is_in_enumerate_call(parent)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def _is_in_range_call(parent: ast.AST | None) -> bool:
|
|
174
|
+
"""Check if the parent is a range() call.
|
|
175
|
+
|
|
176
|
+
Args:
|
|
177
|
+
parent: The parent node
|
|
178
|
+
|
|
179
|
+
Returns:
|
|
180
|
+
True if parent is range() call
|
|
181
|
+
"""
|
|
182
|
+
return (
|
|
183
|
+
isinstance(parent, ast.Call)
|
|
184
|
+
and isinstance(parent.func, ast.Name)
|
|
185
|
+
and parent.func.id == "range"
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _is_in_enumerate_call(parent: ast.AST | None) -> bool:
|
|
190
|
+
"""Check if the parent is an enumerate() call.
|
|
191
|
+
|
|
192
|
+
Args:
|
|
193
|
+
parent: The parent node
|
|
194
|
+
|
|
195
|
+
Returns:
|
|
196
|
+
True if parent is enumerate() call
|
|
197
|
+
"""
|
|
198
|
+
return (
|
|
199
|
+
isinstance(parent, ast.Call)
|
|
200
|
+
and isinstance(parent.func, ast.Name)
|
|
201
|
+
and parent.func.id == "enumerate"
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def is_string_repetition(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 _has_string_operand(parent)
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def _has_string_operand(binop: ast.BinOp) -> bool:
|
|
229
|
+
"""Check if binary operation has a string operand.
|
|
230
|
+
|
|
231
|
+
Args:
|
|
232
|
+
binop: Binary operation node
|
|
233
|
+
|
|
234
|
+
Returns:
|
|
235
|
+
True if either left or right operand is a string constant
|
|
236
|
+
"""
|
|
237
|
+
return _is_string_constant(binop.left) or _is_string_constant(binop.right)
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def _is_string_constant(node: ast.AST) -> bool:
|
|
241
|
+
"""Check if a node is a string constant.
|
|
242
|
+
|
|
243
|
+
Args:
|
|
244
|
+
node: AST node to check
|
|
245
|
+
|
|
246
|
+
Returns:
|
|
247
|
+
True if node is a Constant with string value
|
|
248
|
+
"""
|
|
249
|
+
return isinstance(node, ast.Constant) and isinstance(node.value, str)
|