thailint 0.1.5__py3-none-any.whl → 0.5.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/__init__.py +7 -2
- src/analyzers/__init__.py +23 -0
- src/analyzers/typescript_base.py +148 -0
- src/api.py +1 -1
- src/cli.py +1111 -144
- src/config.py +12 -33
- src/core/base.py +102 -5
- src/core/cli_utils.py +206 -0
- src/core/config_parser.py +126 -0
- src/core/linter_utils.py +168 -0
- src/core/registry.py +17 -92
- src/core/rule_discovery.py +132 -0
- src/core/violation_builder.py +122 -0
- src/linter_config/ignore.py +112 -40
- src/linter_config/loader.py +3 -13
- src/linters/dry/__init__.py +23 -0
- src/linters/dry/base_token_analyzer.py +76 -0
- src/linters/dry/block_filter.py +265 -0
- src/linters/dry/block_grouper.py +59 -0
- src/linters/dry/cache.py +172 -0
- src/linters/dry/cache_query.py +61 -0
- src/linters/dry/config.py +134 -0
- src/linters/dry/config_loader.py +44 -0
- src/linters/dry/deduplicator.py +120 -0
- src/linters/dry/duplicate_storage.py +63 -0
- src/linters/dry/file_analyzer.py +90 -0
- src/linters/dry/inline_ignore.py +140 -0
- src/linters/dry/linter.py +163 -0
- src/linters/dry/python_analyzer.py +668 -0
- src/linters/dry/storage_initializer.py +42 -0
- src/linters/dry/token_hasher.py +169 -0
- src/linters/dry/typescript_analyzer.py +592 -0
- src/linters/dry/violation_builder.py +74 -0
- src/linters/dry/violation_filter.py +94 -0
- src/linters/dry/violation_generator.py +174 -0
- src/linters/file_header/__init__.py +24 -0
- src/linters/file_header/atemporal_detector.py +87 -0
- src/linters/file_header/config.py +66 -0
- src/linters/file_header/field_validator.py +69 -0
- src/linters/file_header/linter.py +313 -0
- src/linters/file_header/python_parser.py +86 -0
- src/linters/file_header/violation_builder.py +78 -0
- src/linters/file_placement/config_loader.py +86 -0
- src/linters/file_placement/directory_matcher.py +80 -0
- src/linters/file_placement/linter.py +262 -471
- src/linters/file_placement/path_resolver.py +61 -0
- src/linters/file_placement/pattern_matcher.py +55 -0
- src/linters/file_placement/pattern_validator.py +106 -0
- src/linters/file_placement/rule_checker.py +229 -0
- src/linters/file_placement/violation_factory.py +177 -0
- src/linters/magic_numbers/__init__.py +48 -0
- src/linters/magic_numbers/config.py +82 -0
- src/linters/magic_numbers/context_analyzer.py +247 -0
- src/linters/magic_numbers/linter.py +516 -0
- src/linters/magic_numbers/python_analyzer.py +76 -0
- src/linters/magic_numbers/typescript_analyzer.py +218 -0
- src/linters/magic_numbers/violation_builder.py +98 -0
- src/linters/nesting/__init__.py +6 -2
- src/linters/nesting/config.py +17 -4
- src/linters/nesting/linter.py +81 -168
- src/linters/nesting/typescript_analyzer.py +39 -102
- src/linters/nesting/typescript_function_extractor.py +130 -0
- src/linters/nesting/violation_builder.py +139 -0
- src/linters/print_statements/__init__.py +53 -0
- src/linters/print_statements/config.py +83 -0
- src/linters/print_statements/linter.py +430 -0
- src/linters/print_statements/python_analyzer.py +155 -0
- src/linters/print_statements/typescript_analyzer.py +135 -0
- src/linters/print_statements/violation_builder.py +98 -0
- src/linters/srp/__init__.py +99 -0
- src/linters/srp/class_analyzer.py +113 -0
- src/linters/srp/config.py +82 -0
- src/linters/srp/heuristics.py +89 -0
- src/linters/srp/linter.py +234 -0
- src/linters/srp/metrics_evaluator.py +47 -0
- src/linters/srp/python_analyzer.py +72 -0
- src/linters/srp/typescript_analyzer.py +75 -0
- src/linters/srp/typescript_metrics_calculator.py +90 -0
- src/linters/srp/violation_builder.py +117 -0
- src/orchestrator/core.py +54 -9
- src/templates/thailint_config_template.yaml +158 -0
- src/utils/__init__.py +4 -0
- src/utils/project_root.py +203 -0
- thailint-0.5.0.dist-info/METADATA +1286 -0
- thailint-0.5.0.dist-info/RECORD +96 -0
- {thailint-0.1.5.dist-info → thailint-0.5.0.dist-info}/WHEEL +1 -1
- src/.ai/layout.yaml +0 -48
- thailint-0.1.5.dist-info/METADATA +0 -629
- thailint-0.1.5.dist-info/RECORD +0 -28
- {thailint-0.1.5.dist-info → thailint-0.5.0.dist-info}/entry_points.txt +0 -0
- {thailint-0.1.5.dist-info → thailint-0.5.0.dist-info/licenses}/LICENSE +0 -0
|
@@ -0,0 +1,82 @@
|
|
|
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 -1, 0, 1, 2, 3, 4, 5, 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(
|
|
33
|
+
default_factory=lambda: {-1, 0, 1, 2, 3, 4, 5, 10, 100, 1000}
|
|
34
|
+
)
|
|
35
|
+
max_small_integer: int = 10
|
|
36
|
+
ignore: list[str] = field(default_factory=list)
|
|
37
|
+
|
|
38
|
+
def __post_init__(self) -> None:
|
|
39
|
+
"""Validate configuration values."""
|
|
40
|
+
if self.max_small_integer <= 0:
|
|
41
|
+
raise ValueError(f"max_small_integer must be positive, got {self.max_small_integer}")
|
|
42
|
+
|
|
43
|
+
@classmethod
|
|
44
|
+
def from_dict(cls, config: dict[str, Any], language: str | None = None) -> "MagicNumberConfig":
|
|
45
|
+
"""Load configuration from dictionary with language-specific overrides.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
config: Dictionary containing configuration values
|
|
49
|
+
language: Programming language (python, typescript, javascript)
|
|
50
|
+
for language-specific settings
|
|
51
|
+
|
|
52
|
+
Returns:
|
|
53
|
+
MagicNumberConfig instance with values from dictionary
|
|
54
|
+
"""
|
|
55
|
+
# Get language-specific config if available
|
|
56
|
+
if language and language in config:
|
|
57
|
+
lang_config = config[language]
|
|
58
|
+
allowed_numbers = set(
|
|
59
|
+
lang_config.get(
|
|
60
|
+
"allowed_numbers",
|
|
61
|
+
config.get("allowed_numbers", {-1, 0, 1, 2, 3, 4, 5, 10, 100, 1000}),
|
|
62
|
+
)
|
|
63
|
+
)
|
|
64
|
+
max_small_integer = lang_config.get(
|
|
65
|
+
"max_small_integer", config.get("max_small_integer", 10)
|
|
66
|
+
)
|
|
67
|
+
else:
|
|
68
|
+
allowed_numbers = set(
|
|
69
|
+
config.get("allowed_numbers", {-1, 0, 1, 2, 3, 4, 5, 10, 100, 1000})
|
|
70
|
+
)
|
|
71
|
+
max_small_integer = config.get("max_small_integer", 10)
|
|
72
|
+
|
|
73
|
+
ignore_patterns = config.get("ignore", [])
|
|
74
|
+
if not isinstance(ignore_patterns, list):
|
|
75
|
+
ignore_patterns = []
|
|
76
|
+
|
|
77
|
+
return cls(
|
|
78
|
+
enabled=config.get("enabled", True),
|
|
79
|
+
allowed_numbers=allowed_numbers,
|
|
80
|
+
max_small_integer=max_small_integer,
|
|
81
|
+
ignore=ignore_patterns,
|
|
82
|
+
)
|
|
@@ -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)
|