thailint 0.10.0__py3-none-any.whl → 0.12.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.
Files changed (76) hide show
  1. src/__init__.py +1 -0
  2. src/cli/__init__.py +27 -0
  3. src/cli/__main__.py +22 -0
  4. src/cli/config.py +478 -0
  5. src/cli/linters/__init__.py +58 -0
  6. src/cli/linters/code_patterns.py +372 -0
  7. src/cli/linters/code_smells.py +450 -0
  8. src/cli/linters/documentation.py +155 -0
  9. src/cli/linters/shared.py +89 -0
  10. src/cli/linters/structure.py +313 -0
  11. src/cli/linters/structure_quality.py +316 -0
  12. src/cli/main.py +120 -0
  13. src/cli/utils.py +395 -0
  14. src/cli_main.py +34 -0
  15. src/core/types.py +13 -0
  16. src/core/violation_utils.py +69 -0
  17. src/linter_config/ignore.py +32 -16
  18. src/linters/collection_pipeline/linter.py +2 -2
  19. src/linters/dry/block_filter.py +97 -1
  20. src/linters/dry/cache.py +94 -6
  21. src/linters/dry/config.py +47 -10
  22. src/linters/dry/constant.py +92 -0
  23. src/linters/dry/constant_matcher.py +214 -0
  24. src/linters/dry/constant_violation_builder.py +98 -0
  25. src/linters/dry/linter.py +89 -48
  26. src/linters/dry/python_analyzer.py +12 -415
  27. src/linters/dry/python_constant_extractor.py +101 -0
  28. src/linters/dry/single_statement_detector.py +415 -0
  29. src/linters/dry/token_hasher.py +5 -5
  30. src/linters/dry/typescript_analyzer.py +5 -354
  31. src/linters/dry/typescript_constant_extractor.py +134 -0
  32. src/linters/dry/typescript_statement_detector.py +255 -0
  33. src/linters/dry/typescript_value_extractor.py +66 -0
  34. src/linters/file_header/linter.py +2 -2
  35. src/linters/file_placement/linter.py +2 -2
  36. src/linters/file_placement/pattern_matcher.py +19 -5
  37. src/linters/magic_numbers/linter.py +8 -67
  38. src/linters/magic_numbers/typescript_ignore_checker.py +81 -0
  39. src/linters/nesting/linter.py +12 -9
  40. src/linters/print_statements/linter.py +7 -24
  41. src/linters/srp/class_analyzer.py +9 -9
  42. src/linters/srp/heuristics.py +2 -2
  43. src/linters/srp/linter.py +2 -2
  44. src/linters/stateless_class/linter.py +2 -2
  45. src/linters/stringly_typed/__init__.py +36 -0
  46. src/linters/stringly_typed/config.py +190 -0
  47. src/linters/stringly_typed/context_filter.py +451 -0
  48. src/linters/stringly_typed/function_call_violation_builder.py +137 -0
  49. src/linters/stringly_typed/ignore_checker.py +102 -0
  50. src/linters/stringly_typed/ignore_utils.py +51 -0
  51. src/linters/stringly_typed/linter.py +344 -0
  52. src/linters/stringly_typed/python/__init__.py +33 -0
  53. src/linters/stringly_typed/python/analyzer.py +344 -0
  54. src/linters/stringly_typed/python/call_tracker.py +172 -0
  55. src/linters/stringly_typed/python/comparison_tracker.py +252 -0
  56. src/linters/stringly_typed/python/condition_extractor.py +131 -0
  57. src/linters/stringly_typed/python/conditional_detector.py +176 -0
  58. src/linters/stringly_typed/python/constants.py +21 -0
  59. src/linters/stringly_typed/python/match_analyzer.py +88 -0
  60. src/linters/stringly_typed/python/validation_detector.py +186 -0
  61. src/linters/stringly_typed/python/variable_extractor.py +96 -0
  62. src/linters/stringly_typed/storage.py +630 -0
  63. src/linters/stringly_typed/storage_initializer.py +45 -0
  64. src/linters/stringly_typed/typescript/__init__.py +28 -0
  65. src/linters/stringly_typed/typescript/analyzer.py +157 -0
  66. src/linters/stringly_typed/typescript/call_tracker.py +329 -0
  67. src/linters/stringly_typed/typescript/comparison_tracker.py +372 -0
  68. src/linters/stringly_typed/violation_generator.py +376 -0
  69. src/orchestrator/core.py +241 -12
  70. {thailint-0.10.0.dist-info → thailint-0.12.0.dist-info}/METADATA +9 -3
  71. {thailint-0.10.0.dist-info → thailint-0.12.0.dist-info}/RECORD +74 -28
  72. thailint-0.12.0.dist-info/entry_points.txt +4 -0
  73. src/cli.py +0 -2141
  74. thailint-0.10.0.dist-info/entry_points.txt +0 -4
  75. {thailint-0.10.0.dist-info → thailint-0.12.0.dist-info}/WHEEL +0 -0
  76. {thailint-0.10.0.dist-info → thailint-0.12.0.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,186 @@
1
+ """
2
+ Purpose: Detect membership validation patterns in Python AST
3
+
4
+ Scope: Find 'x in ("a", "b")' and 'x not in (...)' patterns
5
+
6
+ Overview: Provides MembershipValidationDetector class that traverses Python AST to find
7
+ membership validation patterns where strings are used instead of enums. Detects
8
+ Compare nodes with In/NotIn operators and string literal collections (tuple, set,
9
+ list). Returns structured MembershipPattern dataclass instances with string values,
10
+ operator type, location, and optional variable name. Filters out non-string
11
+ collections, single-element collections, and variable references.
12
+
13
+ Dependencies: ast module for AST parsing, dataclasses for pattern structure,
14
+ variable_extractor for variable name extraction
15
+
16
+ Exports: MembershipValidationDetector class, MembershipPattern dataclass
17
+
18
+ Interfaces: MembershipValidationDetector.find_patterns(tree) -> list[MembershipPattern]
19
+
20
+ Implementation: AST NodeVisitor pattern with Compare node handling for In/NotIn operators
21
+ """
22
+
23
+ import ast
24
+ from dataclasses import dataclass
25
+
26
+ from .constants import MIN_VALUES_FOR_PATTERN
27
+ from .variable_extractor import extract_variable_name
28
+
29
+
30
+ @dataclass
31
+ class MembershipPattern:
32
+ """Represents a detected membership validation pattern.
33
+
34
+ Captures the essential information about a stringly-typed membership check
35
+ including the string values being compared, the operator used, source location,
36
+ and the variable being tested if identifiable.
37
+ """
38
+
39
+ string_values: set[str]
40
+ """Set of string values in the membership test."""
41
+
42
+ operator: str
43
+ """Operator used: 'in' or 'not in'."""
44
+
45
+ line_number: int
46
+ """Line number where the pattern occurs (1-indexed)."""
47
+
48
+ column: int
49
+ """Column number where the pattern starts (0-indexed)."""
50
+
51
+ variable_name: str | None
52
+ """Variable name being tested, if identifiable from a simple expression."""
53
+
54
+
55
+ class MembershipValidationDetector(ast.NodeVisitor):
56
+ """Detects membership validation patterns in Python AST.
57
+
58
+ Finds patterns like 'x in ("a", "b")' and 'x not in {"c", "d"}' where
59
+ strings are used for validation instead of proper enums.
60
+ """
61
+
62
+ def __init__(self) -> None:
63
+ """Initialize the detector."""
64
+ self.patterns: list[MembershipPattern] = []
65
+
66
+ def find_patterns(self, tree: ast.AST) -> list[MembershipPattern]:
67
+ """Find all membership validation patterns in the AST.
68
+
69
+ Args:
70
+ tree: The AST to analyze
71
+
72
+ Returns:
73
+ List of MembershipPattern instances for each detected pattern
74
+ """
75
+ self.patterns = []
76
+ self.visit(tree)
77
+ return self.patterns
78
+
79
+ def visit_Compare(self, node: ast.Compare) -> None: # pylint: disable=invalid-name
80
+ """Visit a Compare node to check for membership patterns.
81
+
82
+ Handles Compare nodes with In or NotIn operators where the
83
+ comparator is a literal collection of strings.
84
+
85
+ Args:
86
+ node: The Compare node to analyze
87
+ """
88
+ for op_index, operator in enumerate(node.ops):
89
+ self._check_membership_operator(node, operator, op_index)
90
+ self.generic_visit(node)
91
+
92
+ def _check_membership_operator(
93
+ self, node: ast.Compare, operator: ast.cmpop, op_index: int
94
+ ) -> None:
95
+ """Check if an operator forms a valid membership pattern.
96
+
97
+ Args:
98
+ node: The Compare node containing the operator
99
+ operator: The comparison operator to check
100
+ op_index: Index of the operator in the Compare node
101
+ """
102
+ if not isinstance(operator, (ast.In, ast.NotIn)):
103
+ return
104
+
105
+ comparator = node.comparators[op_index]
106
+ string_values = _extract_string_values(comparator)
107
+
108
+ if string_values is None or len(string_values) < MIN_VALUES_FOR_PATTERN:
109
+ return
110
+
111
+ self._add_pattern(node, operator, string_values)
112
+
113
+ def _add_pattern(self, node: ast.Compare, operator: ast.cmpop, string_values: set[str]) -> None:
114
+ """Create and add a membership pattern to results.
115
+
116
+ Args:
117
+ node: The Compare node containing the pattern
118
+ operator: The In or NotIn operator
119
+ string_values: Set of string values detected
120
+ """
121
+ operator_str = "in" if isinstance(operator, ast.In) else "not in"
122
+ variable_name = extract_variable_name(node.left)
123
+
124
+ pattern = MembershipPattern(
125
+ string_values=string_values,
126
+ operator=operator_str,
127
+ line_number=node.lineno,
128
+ column=node.col_offset,
129
+ variable_name=variable_name,
130
+ )
131
+ self.patterns.append(pattern)
132
+
133
+
134
+ def _extract_string_values(node: ast.AST) -> set[str] | None:
135
+ """Extract string values from a collection literal.
136
+
137
+ Args:
138
+ node: AST node representing the collection
139
+
140
+ Returns:
141
+ Set of string values if all elements are strings, None otherwise
142
+ """
143
+ elements = _get_collection_elements(node)
144
+ if elements is None or len(elements) == 0:
145
+ return None
146
+
147
+ return _collect_string_constants(elements)
148
+
149
+
150
+ def _get_collection_elements(node: ast.AST) -> list[ast.expr] | None:
151
+ """Get elements from a collection literal node.
152
+
153
+ Args:
154
+ node: AST node that may be a collection literal
155
+
156
+ Returns:
157
+ List of element nodes if node is a collection, None otherwise
158
+ """
159
+ if isinstance(node, ast.Tuple):
160
+ return list(node.elts)
161
+ if isinstance(node, ast.Set):
162
+ return list(node.elts)
163
+ if isinstance(node, ast.List):
164
+ return list(node.elts)
165
+ return None
166
+
167
+
168
+ def _collect_string_constants(elements: list[ast.expr]) -> set[str] | None:
169
+ """Collect string constants from a list of AST expression nodes.
170
+
171
+ Args:
172
+ elements: List of expression nodes from a collection
173
+
174
+ Returns:
175
+ Set of string values if all elements are string constants, None otherwise
176
+ """
177
+ string_values: set[str] = set()
178
+
179
+ for element in elements:
180
+ if not isinstance(element, ast.Constant):
181
+ return None
182
+ if not isinstance(element.value, str):
183
+ return None
184
+ string_values.add(element.value)
185
+
186
+ return string_values
@@ -0,0 +1,96 @@
1
+ """
2
+ Purpose: Extract variable names from Python AST nodes
3
+
4
+ Scope: AST node analysis for identifying variable names in expressions
5
+
6
+ Overview: Provides functions to extract variable names from various Python AST expression
7
+ types including simple names, attribute access chains, and method calls. Handles
8
+ complex expressions by returning None when the variable cannot be simply identified.
9
+ Supports extraction from Name nodes, Attribute chains (e.g., self.status), and Call
10
+ nodes for method calls (e.g., x.lower()).
11
+
12
+ Dependencies: ast module for AST node types
13
+
14
+ Exports: extract_variable_name, extract_attribute_chain functions
15
+
16
+ Interfaces: extract_variable_name(node) -> str | None for general extraction,
17
+ extract_attribute_chain(node) -> str for attribute chain extraction
18
+
19
+ Implementation: Pattern matching on AST node types with recursive chain handling
20
+ """
21
+
22
+ import ast
23
+
24
+
25
+ def extract_variable_name(node: ast.AST) -> str | None:
26
+ """Extract variable name from an expression node.
27
+
28
+ Identifies the variable being used in an expression, handling
29
+ simple names, attribute access, and method calls.
30
+
31
+ Args:
32
+ node: AST node representing an expression
33
+
34
+ Returns:
35
+ Variable name if identifiable, None for complex expressions
36
+ """
37
+ if isinstance(node, ast.Name):
38
+ return node.id
39
+
40
+ if isinstance(node, ast.Attribute):
41
+ return extract_attribute_chain(node)
42
+
43
+ if isinstance(node, ast.Call):
44
+ return _extract_call_variable(node)
45
+
46
+ return None
47
+
48
+
49
+ def extract_attribute_chain(node: ast.Attribute) -> str:
50
+ """Extract full attribute chain as string.
51
+
52
+ Builds a dotted string representation of attribute access,
53
+ e.g., 'self.status' or 'obj.attr.subattr'.
54
+
55
+ Args:
56
+ node: Attribute node to extract from
57
+
58
+ Returns:
59
+ String representation of attribute chain
60
+ """
61
+ parts: list[str] = [node.attr]
62
+ current = node.value
63
+
64
+ while isinstance(current, ast.Attribute):
65
+ parts.append(current.attr)
66
+ current = current.value
67
+
68
+ if isinstance(current, ast.Name):
69
+ parts.append(current.id)
70
+
71
+ parts.reverse()
72
+ return ".".join(parts)
73
+
74
+
75
+ def _extract_call_variable(node: ast.Call) -> str | None:
76
+ """Extract variable from a method call expression.
77
+
78
+ For expressions like x.lower(), returns 'x'.
79
+ For complex calls like get_value().lower(), returns None.
80
+
81
+ Args:
82
+ node: Call node to extract from
83
+
84
+ Returns:
85
+ Variable name if identifiable, None otherwise
86
+ """
87
+ if not isinstance(node.func, ast.Attribute):
88
+ return None
89
+
90
+ value = node.func.value
91
+ if isinstance(value, ast.Name):
92
+ return value.id
93
+ if isinstance(value, ast.Attribute):
94
+ return extract_attribute_chain(value)
95
+
96
+ return None