thailint 0.12.0__py3-none-any.whl → 0.14.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 (135) hide show
  1. src/analyzers/__init__.py +4 -3
  2. src/analyzers/ast_utils.py +54 -0
  3. src/analyzers/typescript_base.py +4 -0
  4. src/cli/__init__.py +3 -0
  5. src/cli/config.py +12 -12
  6. src/cli/config_merge.py +241 -0
  7. src/cli/linters/__init__.py +9 -0
  8. src/cli/linters/code_patterns.py +107 -257
  9. src/cli/linters/code_smells.py +48 -165
  10. src/cli/linters/documentation.py +21 -95
  11. src/cli/linters/performance.py +274 -0
  12. src/cli/linters/shared.py +232 -6
  13. src/cli/linters/structure.py +26 -21
  14. src/cli/linters/structure_quality.py +28 -21
  15. src/cli_main.py +3 -0
  16. src/config.py +2 -1
  17. src/core/base.py +3 -2
  18. src/core/cli_utils.py +3 -1
  19. src/core/config_parser.py +5 -2
  20. src/core/constants.py +54 -0
  21. src/core/linter_utils.py +95 -6
  22. src/core/rule_discovery.py +5 -1
  23. src/core/violation_builder.py +3 -0
  24. src/linter_config/directive_markers.py +109 -0
  25. src/linter_config/ignore.py +225 -383
  26. src/linter_config/pattern_utils.py +65 -0
  27. src/linter_config/rule_matcher.py +89 -0
  28. src/linters/collection_pipeline/any_all_analyzer.py +281 -0
  29. src/linters/collection_pipeline/ast_utils.py +40 -0
  30. src/linters/collection_pipeline/config.py +12 -0
  31. src/linters/collection_pipeline/continue_analyzer.py +2 -8
  32. src/linters/collection_pipeline/detector.py +262 -32
  33. src/linters/collection_pipeline/filter_map_analyzer.py +402 -0
  34. src/linters/collection_pipeline/linter.py +18 -35
  35. src/linters/collection_pipeline/suggestion_builder.py +68 -1
  36. src/linters/dry/base_token_analyzer.py +16 -9
  37. src/linters/dry/block_filter.py +7 -4
  38. src/linters/dry/cache.py +7 -2
  39. src/linters/dry/config.py +7 -1
  40. src/linters/dry/constant_matcher.py +34 -25
  41. src/linters/dry/file_analyzer.py +4 -2
  42. src/linters/dry/inline_ignore.py +7 -16
  43. src/linters/dry/linter.py +48 -25
  44. src/linters/dry/python_analyzer.py +18 -10
  45. src/linters/dry/python_constant_extractor.py +51 -52
  46. src/linters/dry/single_statement_detector.py +14 -12
  47. src/linters/dry/token_hasher.py +115 -115
  48. src/linters/dry/typescript_analyzer.py +11 -6
  49. src/linters/dry/typescript_constant_extractor.py +4 -0
  50. src/linters/dry/typescript_statement_detector.py +208 -208
  51. src/linters/dry/typescript_value_extractor.py +3 -0
  52. src/linters/dry/violation_filter.py +1 -4
  53. src/linters/dry/violation_generator.py +1 -4
  54. src/linters/file_header/atemporal_detector.py +58 -40
  55. src/linters/file_header/base_parser.py +4 -0
  56. src/linters/file_header/bash_parser.py +4 -0
  57. src/linters/file_header/config.py +14 -0
  58. src/linters/file_header/field_validator.py +5 -8
  59. src/linters/file_header/linter.py +19 -12
  60. src/linters/file_header/markdown_parser.py +6 -0
  61. src/linters/file_placement/config_loader.py +3 -1
  62. src/linters/file_placement/linter.py +22 -8
  63. src/linters/file_placement/pattern_matcher.py +21 -4
  64. src/linters/file_placement/pattern_validator.py +21 -7
  65. src/linters/file_placement/rule_checker.py +2 -2
  66. src/linters/lazy_ignores/__init__.py +43 -0
  67. src/linters/lazy_ignores/config.py +66 -0
  68. src/linters/lazy_ignores/directive_utils.py +121 -0
  69. src/linters/lazy_ignores/header_parser.py +177 -0
  70. src/linters/lazy_ignores/linter.py +158 -0
  71. src/linters/lazy_ignores/matcher.py +135 -0
  72. src/linters/lazy_ignores/python_analyzer.py +205 -0
  73. src/linters/lazy_ignores/rule_id_utils.py +180 -0
  74. src/linters/lazy_ignores/skip_detector.py +298 -0
  75. src/linters/lazy_ignores/types.py +69 -0
  76. src/linters/lazy_ignores/typescript_analyzer.py +146 -0
  77. src/linters/lazy_ignores/violation_builder.py +131 -0
  78. src/linters/lbyl/__init__.py +29 -0
  79. src/linters/lbyl/config.py +63 -0
  80. src/linters/lbyl/pattern_detectors/__init__.py +25 -0
  81. src/linters/lbyl/pattern_detectors/base.py +46 -0
  82. src/linters/magic_numbers/context_analyzer.py +227 -229
  83. src/linters/magic_numbers/linter.py +20 -15
  84. src/linters/magic_numbers/python_analyzer.py +4 -16
  85. src/linters/magic_numbers/typescript_analyzer.py +9 -16
  86. src/linters/method_property/config.py +4 -1
  87. src/linters/method_property/linter.py +5 -10
  88. src/linters/method_property/python_analyzer.py +5 -4
  89. src/linters/method_property/violation_builder.py +3 -0
  90. src/linters/nesting/linter.py +11 -6
  91. src/linters/nesting/typescript_analyzer.py +6 -12
  92. src/linters/nesting/typescript_function_extractor.py +0 -4
  93. src/linters/nesting/violation_builder.py +1 -0
  94. src/linters/performance/__init__.py +91 -0
  95. src/linters/performance/config.py +43 -0
  96. src/linters/performance/constants.py +49 -0
  97. src/linters/performance/linter.py +149 -0
  98. src/linters/performance/python_analyzer.py +365 -0
  99. src/linters/performance/regex_analyzer.py +312 -0
  100. src/linters/performance/regex_linter.py +139 -0
  101. src/linters/performance/typescript_analyzer.py +236 -0
  102. src/linters/performance/violation_builder.py +160 -0
  103. src/linters/print_statements/linter.py +6 -4
  104. src/linters/print_statements/python_analyzer.py +85 -81
  105. src/linters/print_statements/typescript_analyzer.py +6 -15
  106. src/linters/srp/heuristics.py +4 -4
  107. src/linters/srp/linter.py +12 -12
  108. src/linters/srp/violation_builder.py +0 -4
  109. src/linters/stateless_class/linter.py +30 -36
  110. src/linters/stateless_class/python_analyzer.py +11 -20
  111. src/linters/stringly_typed/config.py +4 -5
  112. src/linters/stringly_typed/context_filter.py +410 -410
  113. src/linters/stringly_typed/function_call_violation_builder.py +93 -95
  114. src/linters/stringly_typed/linter.py +48 -16
  115. src/linters/stringly_typed/python/analyzer.py +5 -1
  116. src/linters/stringly_typed/python/call_tracker.py +8 -5
  117. src/linters/stringly_typed/python/comparison_tracker.py +10 -5
  118. src/linters/stringly_typed/python/condition_extractor.py +3 -0
  119. src/linters/stringly_typed/python/conditional_detector.py +4 -1
  120. src/linters/stringly_typed/python/match_analyzer.py +8 -2
  121. src/linters/stringly_typed/python/validation_detector.py +3 -0
  122. src/linters/stringly_typed/storage.py +14 -14
  123. src/linters/stringly_typed/typescript/call_tracker.py +9 -3
  124. src/linters/stringly_typed/typescript/comparison_tracker.py +9 -3
  125. src/linters/stringly_typed/violation_generator.py +288 -259
  126. src/orchestrator/core.py +13 -4
  127. src/templates/thailint_config_template.yaml +196 -0
  128. src/utils/project_root.py +3 -0
  129. thailint-0.14.0.dist-info/METADATA +185 -0
  130. thailint-0.14.0.dist-info/RECORD +199 -0
  131. thailint-0.12.0.dist-info/METADATA +0 -1667
  132. thailint-0.12.0.dist-info/RECORD +0 -164
  133. {thailint-0.12.0.dist-info → thailint-0.14.0.dist-info}/WHEEL +0 -0
  134. {thailint-0.12.0.dist-info → thailint-0.14.0.dist-info}/entry_points.txt +0 -0
  135. {thailint-0.12.0.dist-info → thailint-0.14.0.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,177 @@
1
+ """
2
+ Purpose: Parse Suppressions section from file headers
3
+
4
+ Scope: Python docstrings and TypeScript JSDoc comment header parsing
5
+
6
+ Overview: Provides SuppressionsParser class for extracting the Suppressions section from
7
+ file headers. Parses Python triple-quoted docstrings and TypeScript JSDoc comments.
8
+ Extracts rule IDs and justifications, normalizing rule IDs for case-insensitive matching.
9
+ Returns dictionary mapping normalized rule IDs to their justifications.
10
+
11
+ Dependencies: re for pattern matching, Language enum for type safety
12
+
13
+ Exports: SuppressionsParser
14
+
15
+ Interfaces: parse(header: str) -> dict[str, str], extract_header(code: str, language: Language)
16
+
17
+ Implementation: Regex-based section extraction with line-by-line entry parsing
18
+ """
19
+
20
+ import re
21
+
22
+ from src.core.constants import Language
23
+
24
+
25
+ class SuppressionsParser:
26
+ """Parses Suppressions section from file headers."""
27
+
28
+ # Pattern to find Suppressions section (case-insensitive)
29
+ # Matches "Suppressions:" followed by indented lines
30
+ SUPPRESSIONS_SECTION = re.compile(
31
+ r"Suppressions:\s*\n((?:[ \t]+\S.*\n?)+)",
32
+ re.MULTILINE | re.IGNORECASE,
33
+ )
34
+
35
+ # Pattern for JSDoc-style suppressions (* prefixed lines)
36
+ JSDOC_SUPPRESSIONS_SECTION = re.compile(
37
+ r"Suppressions:\s*\n((?:\s*\*\s+\S.*\n?)+)",
38
+ re.MULTILINE | re.IGNORECASE,
39
+ )
40
+
41
+ # Pattern to parse individual entries (rule_id: justification)
42
+ # Rule IDs can contain colons (e.g., type:ignore[arg-type])
43
+ # Handles list prefixes: "- ", "* ", "• " and plain indented entries
44
+ # Justification must start with word char or underscore to avoid matching continuation lines
45
+ ENTRY_PATTERN = re.compile(
46
+ r"^\s*[-*•]?\s*(.+):\s+([A-Za-z_].*)$",
47
+ re.MULTILINE,
48
+ )
49
+
50
+ def parse(self, header: str) -> dict[str, str]:
51
+ """Parse Suppressions section, return rule_id -> justification mapping.
52
+
53
+ Args:
54
+ header: File header content (docstring or JSDoc)
55
+
56
+ Returns:
57
+ Dictionary mapping normalized rule IDs to justification strings
58
+ """
59
+ # Try standard Python-style first, then JSDoc-style
60
+ section_match = self.SUPPRESSIONS_SECTION.search(header)
61
+ if not section_match:
62
+ section_match = self.JSDOC_SUPPRESSIONS_SECTION.search(header)
63
+
64
+ if not section_match:
65
+ return {}
66
+
67
+ entries: dict[str, str] = {}
68
+ section_content = section_match.group(1)
69
+
70
+ for match in self.ENTRY_PATTERN.finditer(section_content):
71
+ rule_id = match.group(1).strip()
72
+ justification = match.group(2).strip()
73
+
74
+ # Skip entries with empty justification
75
+ if justification:
76
+ normalized_id = self.normalize_rule_id(rule_id)
77
+ entries[normalized_id] = justification
78
+
79
+ return entries
80
+
81
+ def normalize_rule_id(self, rule_id: str) -> str:
82
+ """Normalize rule ID for case-insensitive matching.
83
+
84
+ Strips common list prefixes (-, *, •) and normalizes to lowercase.
85
+
86
+ Args:
87
+ rule_id: Original rule ID string
88
+
89
+ Returns:
90
+ Normalized rule ID (lowercase, no list prefix)
91
+ """
92
+ normalized = rule_id.lower().strip()
93
+ # Strip common list prefixes (bullet points)
94
+ if normalized.startswith(("- ", "* ", "• ")):
95
+ normalized = normalized[2:]
96
+ elif normalized.startswith(("-", "*", "•")):
97
+ normalized = normalized[1:].lstrip()
98
+ return normalized
99
+
100
+ def extract_header(self, code: str, language: str | Language = Language.PYTHON) -> str:
101
+ """Extract the header section from code.
102
+
103
+ Args:
104
+ code: Full source code
105
+ language: Programming language (Language enum or string)
106
+
107
+ Returns:
108
+ Header content as string, or empty string if not found
109
+ """
110
+ lang = Language(language) if isinstance(language, str) else language
111
+ if lang == Language.PYTHON:
112
+ return self._extract_python_header(code)
113
+ if lang in (Language.TYPESCRIPT, Language.JAVASCRIPT):
114
+ return self._extract_ts_header(code)
115
+ return ""
116
+
117
+ def _extract_python_header(self, code: str) -> str:
118
+ """Extract Python docstring header.
119
+
120
+ Args:
121
+ code: Python source code
122
+
123
+ Returns:
124
+ Docstring content or empty string
125
+ """
126
+ # Match triple-quoted docstring at start of file
127
+ # Skip leading whitespace, comments, and encoding declarations
128
+ stripped = self._skip_leading_comments(code)
129
+
130
+ # Try double quotes first
131
+ match = re.match(r'^"""(.*?)"""', stripped, re.DOTALL)
132
+ if match:
133
+ return match.group(0)
134
+
135
+ # Try single quotes
136
+ match = re.match(r"^'''(.*?)'''", stripped, re.DOTALL)
137
+ if match:
138
+ return match.group(0)
139
+
140
+ return ""
141
+
142
+ def _skip_leading_comments(self, code: str) -> str:
143
+ """Skip leading comments and empty lines to find docstring.
144
+
145
+ Args:
146
+ code: Python source code
147
+
148
+ Returns:
149
+ Code with leading comments/empty lines removed
150
+ """
151
+ lines = code.split("\n")
152
+ for i, line in enumerate(lines):
153
+ stripped = line.strip()
154
+ # Skip empty lines
155
+ if not stripped:
156
+ continue
157
+ # Skip comment lines (including pylint/noqa/type comments)
158
+ if stripped.startswith("#"):
159
+ continue
160
+ # Found non-comment, non-empty line - return from here
161
+ return "\n".join(lines[i:])
162
+ return ""
163
+
164
+ def _extract_ts_header(self, code: str) -> str:
165
+ """Extract TypeScript/JavaScript JSDoc header.
166
+
167
+ Args:
168
+ code: TypeScript/JavaScript source code
169
+
170
+ Returns:
171
+ JSDoc comment content or empty string
172
+ """
173
+ stripped = code.lstrip()
174
+ match = re.match(r"^/\*\*(.*?)\*/", stripped, re.DOTALL)
175
+ if match:
176
+ return match.group(0)
177
+ return ""
@@ -0,0 +1,158 @@
1
+ """
2
+ Purpose: Main LazyIgnoresRule class for detecting unjustified linting suppressions
3
+
4
+ Scope: Orchestration of ignore detection and header suppression validation
5
+
6
+ Overview: Provides LazyIgnoresRule that cross-references linting ignore directives found
7
+ in code (noqa, type:ignore, pylint:disable, nosec) and test skip patterns with
8
+ Suppressions entries declared in file headers. Detects two types of violations:
9
+ unjustified ignores/skips (directive without header declaration) and orphaned
10
+ suppressions (header declaration without matching ignore in code). Enforces the
11
+ header-based suppression model requiring human approval for all linting bypasses.
12
+
13
+ Dependencies: PythonIgnoreDetector, TestSkipDetector, SuppressionsParser, IgnoreSuppressionMatcher
14
+
15
+ Exports: LazyIgnoresRule
16
+
17
+ Interfaces: check(context: BaseLintContext) -> list[Violation]
18
+
19
+ Implementation: Delegation to matcher for cross-reference logic, violation builder for messages
20
+ """
21
+
22
+ from pathlib import Path
23
+
24
+ from src.core.base import BaseLintContext, BaseLintRule
25
+ from src.core.constants import Language
26
+ from src.core.types import Violation
27
+
28
+ from .header_parser import SuppressionsParser
29
+ from .matcher import IgnoreSuppressionMatcher
30
+ from .python_analyzer import PythonIgnoreDetector
31
+ from .skip_detector import TestSkipDetector
32
+ from .types import IgnoreDirective
33
+ from .violation_builder import build_orphaned_violation, build_unjustified_violation
34
+
35
+
36
+ class LazyIgnoresRule(BaseLintRule):
37
+ """Detects unjustified linting suppressions and orphaned header entries."""
38
+
39
+ def __init__(self, check_test_skips: bool = True) -> None:
40
+ """Initialize the lazy ignores rule with detection components.
41
+
42
+ Args:
43
+ check_test_skips: Whether to check for unjustified test skips.
44
+ """
45
+ self._python_detector = PythonIgnoreDetector()
46
+ self._test_skip_detector = TestSkipDetector()
47
+ self._suppression_parser = SuppressionsParser()
48
+ self._matcher = IgnoreSuppressionMatcher(self._suppression_parser)
49
+ self._check_test_skips = check_test_skips
50
+
51
+ @property
52
+ def rule_id(self) -> str:
53
+ """Unique identifier for this rule."""
54
+ return "lazy-ignores"
55
+
56
+ @property
57
+ def rule_name(self) -> str:
58
+ """Human-readable name for this rule."""
59
+ return "Lazy Ignores"
60
+
61
+ @property
62
+ def description(self) -> str:
63
+ """Description of what this rule checks."""
64
+ return (
65
+ "Detects linting suppressions (noqa, type:ignore, pylint:disable, nosec) "
66
+ "and test skips without corresponding entries in the file header's "
67
+ "Suppressions section."
68
+ )
69
+
70
+ def check(self, context: BaseLintContext) -> list[Violation]:
71
+ """Check for violations in the given context.
72
+
73
+ Args:
74
+ context: The lint context containing file information.
75
+
76
+ Returns:
77
+ List of violations for unjustified and orphaned suppressions.
78
+ """
79
+ if context.language != Language.PYTHON:
80
+ return []
81
+
82
+ if not context.file_content:
83
+ return []
84
+
85
+ file_path = str(context.file_path) if context.file_path else "unknown"
86
+ return self.check_content(context.file_content, file_path)
87
+
88
+ def check_content(self, code: str, file_path: str) -> list[Violation]:
89
+ """Check code for unjustified ignores and orphaned suppressions.
90
+
91
+ Args:
92
+ code: Source code content to analyze.
93
+ file_path: Path to the file being analyzed.
94
+
95
+ Returns:
96
+ List of violations for unjustified and orphaned suppressions.
97
+ """
98
+ # Extract and parse header suppressions
99
+ header = self._suppression_parser.extract_header(code, "python")
100
+ suppressions = self._suppression_parser.parse(header)
101
+
102
+ # Find all ignore directives in code
103
+ ignores = self._python_detector.find_ignores(code, Path(file_path))
104
+
105
+ # Find test skip directives if enabled
106
+ if self._check_test_skips:
107
+ test_skips = self._test_skip_detector.find_skips(code, Path(file_path), "python")
108
+ ignores = list(ignores) + list(test_skips)
109
+
110
+ # Build set of normalized rule IDs used in code
111
+ used_rule_ids = self._matcher.collect_used_rule_ids(ignores)
112
+
113
+ # Find violations
114
+ violations: list[Violation] = []
115
+ violations.extend(self._find_unjustified(ignores, suppressions, file_path))
116
+ violations.extend(self._find_orphaned(suppressions, used_rule_ids, file_path))
117
+
118
+ return violations
119
+
120
+ def _find_unjustified(
121
+ self, ignores: list[IgnoreDirective], suppressions: dict[str, str], file_path: str
122
+ ) -> list[Violation]:
123
+ """Find ignore directives without matching header suppressions."""
124
+ violations: list[Violation] = []
125
+
126
+ for ignore in ignores:
127
+ unjustified = self._matcher.find_unjustified_rule_ids(ignore, suppressions)
128
+ if unjustified:
129
+ violations.append(
130
+ build_unjustified_violation(
131
+ file_path=file_path,
132
+ line=ignore.line,
133
+ column=ignore.column,
134
+ rule_id=", ".join(unjustified),
135
+ raw_text=ignore.raw_text,
136
+ )
137
+ )
138
+
139
+ return violations
140
+
141
+ def _find_orphaned(
142
+ self, suppressions: dict[str, str], used_rule_ids: set[str], file_path: str
143
+ ) -> list[Violation]:
144
+ """Find header suppressions without matching code ignores."""
145
+ violations: list[Violation] = []
146
+ orphaned = self._matcher.find_orphaned_rule_ids(suppressions, used_rule_ids)
147
+
148
+ for rule_id, justification in orphaned:
149
+ violations.append(
150
+ build_orphaned_violation(
151
+ file_path=file_path,
152
+ header_line=1, # Header entries are at file start
153
+ rule_id=rule_id,
154
+ justification=justification,
155
+ )
156
+ )
157
+
158
+ return violations
@@ -0,0 +1,135 @@
1
+ """
2
+ Purpose: Cross-reference matcher for lazy-ignores linter
3
+
4
+ Scope: Matching ignore directives with header suppressions
5
+
6
+ Overview: Provides IgnoreSuppressionMatcher class that cross-references linting ignore
7
+ directives found in code with Suppressions entries declared in file headers. Handles
8
+ case-insensitive rule ID normalization and special patterns like type:ignore[code].
9
+ Identifies unjustified ignores (code ignores without header entries) and orphaned
10
+ suppressions (header entries without matching code ignores).
11
+
12
+ Dependencies: SuppressionsParser for normalization, types for IgnoreDirective and IgnoreType,
13
+ rule_id_utils for pure parsing functions
14
+
15
+ Exports: IgnoreSuppressionMatcher
16
+
17
+ Interfaces: find_unjustified(), find_orphaned()
18
+
19
+ Implementation: Set-based matching with rule ID normalization for case-insensitive comparison
20
+ """
21
+
22
+ from .header_parser import SuppressionsParser
23
+ from .rule_id_utils import (
24
+ comma_list_has_used_rule,
25
+ find_rule_in_suppressions,
26
+ is_type_ignore_format_in_suppressions,
27
+ type_ignore_bracket_has_used_rule,
28
+ )
29
+ from .types import IgnoreDirective, IgnoreType
30
+
31
+
32
+ class IgnoreSuppressionMatcher:
33
+ """Matches ignore directives with header suppressions."""
34
+
35
+ def __init__(self, parser: SuppressionsParser) -> None:
36
+ """Initialize the matcher.
37
+
38
+ Args:
39
+ parser: SuppressionsParser for rule ID normalization.
40
+ """
41
+ self._parser = parser
42
+
43
+ def collect_used_rule_ids(self, ignores: list[IgnoreDirective]) -> set[str]:
44
+ """Collect all normalized rule IDs used in ignore directives.
45
+
46
+ Args:
47
+ ignores: List of ignore directives from code.
48
+
49
+ Returns:
50
+ Set of normalized rule IDs that have ignore directives.
51
+ """
52
+ used: set[str] = set()
53
+ for ignore in ignores:
54
+ used.update(self._get_matchable_rule_ids(ignore))
55
+ return used
56
+
57
+ def _get_matchable_rule_ids(self, ignore: IgnoreDirective) -> list[str]:
58
+ """Get normalized rule IDs for matching, handling special formats."""
59
+ if not ignore.rule_ids:
60
+ return [self._normalize(ignore.ignore_type.value)]
61
+
62
+ ids: list[str] = []
63
+ for rule_id in ignore.rule_ids:
64
+ normalized = self._normalize(rule_id)
65
+ ids.append(normalized)
66
+ if ignore.ignore_type == IgnoreType.TYPE_IGNORE:
67
+ ids.append(f"type:ignore[{normalized}]")
68
+ return ids
69
+
70
+ def find_unjustified_rule_ids(
71
+ self, ignore: IgnoreDirective, suppressions: dict[str, str]
72
+ ) -> list[str]:
73
+ """Find which rule IDs in an ignore are not justified.
74
+
75
+ Args:
76
+ ignore: The ignore directive to check.
77
+ suppressions: Dict of normalized rule IDs to justifications.
78
+
79
+ Returns:
80
+ List of unjustified rule IDs (original case preserved).
81
+ """
82
+ if not ignore.rule_ids:
83
+ type_key = self._normalize(ignore.ignore_type.value)
84
+ if type_key not in suppressions:
85
+ return [ignore.ignore_type.value]
86
+ return []
87
+
88
+ unjustified: list[str] = []
89
+ for rule_id in ignore.rule_ids:
90
+ if not self._is_rule_justified(ignore, rule_id, suppressions):
91
+ unjustified.append(rule_id)
92
+ return unjustified
93
+
94
+ def _is_rule_justified(
95
+ self, ignore: IgnoreDirective, rule_id: str, suppressions: dict[str, str]
96
+ ) -> bool:
97
+ """Check if a specific rule ID is justified in suppressions."""
98
+ normalized = self._normalize(rule_id)
99
+ is_type_ignore = ignore.ignore_type == IgnoreType.TYPE_IGNORE
100
+
101
+ if normalized in suppressions:
102
+ return True
103
+ if is_type_ignore and is_type_ignore_format_in_suppressions(normalized, suppressions):
104
+ return True
105
+ return find_rule_in_suppressions(normalized, suppressions, is_type_ignore)
106
+
107
+ def find_orphaned_rule_ids(
108
+ self, suppressions: dict[str, str], used_rule_ids: set[str]
109
+ ) -> list[tuple[str, str]]:
110
+ """Find header suppressions without matching code ignores.
111
+
112
+ Args:
113
+ suppressions: Dict mapping normalized rule IDs to justifications.
114
+ used_rule_ids: Set of normalized rule IDs used in code.
115
+
116
+ Returns:
117
+ List of (rule_id, justification) tuples for orphaned suppressions.
118
+ """
119
+ orphaned: list[tuple[str, str]] = []
120
+ for rule_id, justification in suppressions.items():
121
+ if not self._suppression_is_used(rule_id, used_rule_ids):
122
+ orphaned.append((rule_id.upper(), justification))
123
+ return orphaned
124
+
125
+ def _suppression_is_used(self, suppression_key: str, used_rule_ids: set[str]) -> bool:
126
+ """Check if a suppression key is used by any code ignores."""
127
+ if suppression_key in used_rule_ids:
128
+ return True
129
+ if comma_list_has_used_rule(suppression_key, used_rule_ids):
130
+ return True
131
+ return type_ignore_bracket_has_used_rule(suppression_key, used_rule_ids)
132
+
133
+ def _normalize(self, rule_id: str) -> str:
134
+ """Normalize a rule ID for case-insensitive matching."""
135
+ return self._parser.normalize_rule_id(rule_id)
@@ -0,0 +1,205 @@
1
+ """
2
+ Purpose: Detect Python linting ignore directives in source code
3
+
4
+ Scope: noqa, type:ignore, pylint:disable, nosec, dry:ignore-block pattern detection
5
+
6
+ Overview: Provides PythonIgnoreDetector class that scans Python source code for common
7
+ linting ignore patterns. Detects bare patterns (e.g., # noqa) and rule-specific
8
+ patterns (e.g., # noqa: PLR0912). Handles case-insensitive matching and extracts
9
+ rule IDs from comma-separated lists. Returns list of IgnoreDirective objects with
10
+ line/column positions for violation reporting. Skips patterns inside docstrings
11
+ and string literals to avoid false positives.
12
+
13
+ Dependencies: re for pattern matching, pathlib for file paths, types module for dataclasses
14
+
15
+ Exports: PythonIgnoreDetector
16
+
17
+ Interfaces: find_ignores(code: str, file_path: Path | None) -> list[IgnoreDirective]
18
+
19
+ Implementation: Regex-based line-by-line scanning with docstring-aware state tracking
20
+ """
21
+
22
+ import re
23
+ from pathlib import Path
24
+
25
+ from src.linters.lazy_ignores.directive_utils import create_directive
26
+ from src.linters.lazy_ignores.types import IgnoreDirective, IgnoreType
27
+
28
+
29
+ def _count_unescaped_triple_quotes(line: str, quote: str) -> int:
30
+ """Count unescaped triple-quote occurrences in a line.
31
+
32
+ Uses regex to find non-escaped triple quotes.
33
+
34
+ Args:
35
+ line: Line to scan
36
+ quote: Triple-quote pattern to count (single or double)
37
+
38
+ Returns:
39
+ Number of unescaped triple-quote occurrences
40
+ """
41
+ # Pattern matches triple quotes not preceded by odd number of backslashes
42
+ # Escape the quote for regex
43
+ escaped_quote = re.escape(quote)
44
+ pattern = re.compile(rf"(?<!\\){escaped_quote}")
45
+ return len(pattern.findall(line))
46
+
47
+
48
+ def _count_unescaped_single_quotes(text: str, quote_char: str) -> int:
49
+ """Count unescaped single quote characters in text.
50
+
51
+ Args:
52
+ text: Text to scan
53
+ quote_char: The quote character (' or ")
54
+
55
+ Returns:
56
+ Number of unescaped quote characters
57
+ """
58
+ count = 0
59
+ escaped = False
60
+ for char in text:
61
+ if escaped:
62
+ escaped = False
63
+ continue
64
+ if char == "\\":
65
+ escaped = True
66
+ continue
67
+ if char == quote_char:
68
+ count += 1
69
+ return count
70
+
71
+
72
+ def _is_pattern_in_string_literal(line: str, match_start: int) -> bool:
73
+ """Check if a match position is inside a string literal.
74
+
75
+ Args:
76
+ line: The line of code
77
+ match_start: The start position of the pattern match
78
+
79
+ Returns:
80
+ True if the match is inside a string literal
81
+ """
82
+ before_match = line[:match_start]
83
+ single_count = _count_unescaped_single_quotes(before_match, "'")
84
+ double_count = _count_unescaped_single_quotes(before_match, '"')
85
+ return (single_count % 2 == 1) or (double_count % 2 == 1)
86
+
87
+
88
+ class PythonIgnoreDetector:
89
+ """Detects Python linting ignore directives in source code."""
90
+
91
+ # Regex patterns for each ignore type
92
+ # Each pattern captures optional rule IDs in group 1
93
+ PATTERNS: dict[IgnoreType, re.Pattern[str]] = {
94
+ IgnoreType.NOQA: re.compile(
95
+ r"#\s*noqa(?::\s*([A-Z0-9,\s]+))?(?:\s|$)",
96
+ re.IGNORECASE,
97
+ ),
98
+ IgnoreType.TYPE_IGNORE: re.compile(
99
+ r"#\s*type:\s*ignore(?:\[([^\]]+)\])?",
100
+ ),
101
+ IgnoreType.PYLINT_DISABLE: re.compile(
102
+ r"#\s*pylint:\s*disable=([a-z0-9\-,\s]+)",
103
+ re.IGNORECASE,
104
+ ),
105
+ IgnoreType.NOSEC: re.compile(
106
+ r"#\s*nosec(?:\s+([A-Z0-9,\s]+))?(?:\s|$)",
107
+ re.IGNORECASE,
108
+ ),
109
+ IgnoreType.THAILINT_IGNORE: re.compile(
110
+ r"#\s*thailint:\s*ignore(?!-)(?:\[([^\]]+)\])?",
111
+ re.IGNORECASE,
112
+ ),
113
+ IgnoreType.THAILINT_IGNORE_FILE: re.compile(
114
+ r"#\s*thailint:\s*ignore-file(?:\[([^\]]+)\])?",
115
+ re.IGNORECASE,
116
+ ),
117
+ IgnoreType.THAILINT_IGNORE_NEXT: re.compile(
118
+ r"#\s*thailint:\s*ignore-next-line(?:\[([^\]]+)\])?",
119
+ re.IGNORECASE,
120
+ ),
121
+ IgnoreType.THAILINT_IGNORE_BLOCK: re.compile(
122
+ r"#\s*thailint:\s*ignore-start(?:\[([^\]]+)\])?",
123
+ re.IGNORECASE,
124
+ ),
125
+ IgnoreType.DRY_IGNORE_BLOCK: re.compile(
126
+ r"#\s*dry:\s*ignore-block\b",
127
+ re.IGNORECASE,
128
+ ),
129
+ }
130
+
131
+ def find_ignores(self, code: str, file_path: Path | None = None) -> list[IgnoreDirective]:
132
+ """Find all Python ignore directives in code.
133
+
134
+ Tracks docstring state across lines to avoid false positives from
135
+ patterns mentioned in documentation.
136
+
137
+ Args:
138
+ code: Python source code to scan
139
+ file_path: Optional path to the source file
140
+
141
+ Returns:
142
+ List of IgnoreDirective objects for each detected ignore pattern
143
+ """
144
+ effective_path = file_path or Path("unknown")
145
+ scannable_lines = self._get_scannable_lines(code)
146
+ directives: list[IgnoreDirective] = []
147
+ for line_num, line in scannable_lines:
148
+ directives.extend(self._scan_line(line, line_num, effective_path))
149
+ return directives
150
+
151
+ def _get_scannable_lines(self, code: str) -> list[tuple[int, str]]:
152
+ """Get lines that are not inside docstrings.
153
+
154
+ Args:
155
+ code: Source code to analyze
156
+
157
+ Returns:
158
+ List of (line_number, line_text) tuples for scannable lines
159
+ """
160
+ in_docstring = [False, False] # [triple_double, triple_single]
161
+ quotes = ['"""', "'''"]
162
+ scannable: list[tuple[int, str]] = []
163
+
164
+ for line_num, line in enumerate(code.splitlines(), start=1):
165
+ was_in_docstring = in_docstring[0] or in_docstring[1]
166
+ self._update_docstring_state(line, quotes, in_docstring)
167
+ if not was_in_docstring:
168
+ scannable.append((line_num, line))
169
+
170
+ return scannable
171
+
172
+ def _update_docstring_state(self, line: str, quotes: list[str], state: list[bool]) -> None:
173
+ """Update docstring tracking state based on quotes in line.
174
+
175
+ Args:
176
+ line: Line to analyze
177
+ quotes: List of quote patterns to check
178
+ state: Mutable list tracking in-docstring state for each quote type
179
+ """
180
+ for i, quote in enumerate(quotes):
181
+ if _count_unescaped_triple_quotes(line, quote) % 2 == 1:
182
+ state[i] = not state[i]
183
+
184
+ def _scan_line(self, line: str, line_num: int, file_path: Path) -> list[IgnoreDirective]:
185
+ """Scan a single line for ignore patterns.
186
+
187
+ Skips patterns that appear inside string literals.
188
+
189
+ Args:
190
+ line: Line of code to scan
191
+ line_num: 1-indexed line number
192
+ file_path: Path to the source file
193
+
194
+ Returns:
195
+ List of IgnoreDirective objects found on this line
196
+ """
197
+ found: list[IgnoreDirective] = []
198
+ for ignore_type, pattern in self.PATTERNS.items():
199
+ match = pattern.search(line)
200
+ if not match:
201
+ continue
202
+ if _is_pattern_in_string_literal(line, match.start()):
203
+ continue
204
+ found.append(create_directive(match, ignore_type, line_num, file_path))
205
+ return found