thailint 0.7.0__py3-none-any.whl → 0.9.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 +233 -1
- src/core/base.py +4 -0
- src/core/rule_discovery.py +110 -84
- src/core/violation_builder.py +75 -15
- src/linter_config/loader.py +45 -12
- src/linters/dry/block_filter.py +15 -8
- src/linters/dry/block_grouper.py +4 -0
- src/linters/dry/cache.py +3 -2
- src/linters/dry/cache_query.py +4 -0
- src/linters/dry/duplicate_storage.py +5 -4
- src/linters/dry/token_hasher.py +5 -1
- src/linters/dry/violation_builder.py +4 -0
- src/linters/dry/violation_filter.py +4 -0
- src/linters/dry/violation_generator.py +1 -1
- src/linters/file_header/bash_parser.py +4 -0
- src/linters/file_placement/directory_matcher.py +4 -0
- src/linters/file_placement/pattern_matcher.py +4 -0
- src/linters/file_placement/pattern_validator.py +4 -0
- src/linters/magic_numbers/context_analyzer.py +4 -0
- src/linters/magic_numbers/typescript_analyzer.py +4 -0
- src/linters/method_property/__init__.py +49 -0
- src/linters/method_property/config.py +135 -0
- src/linters/method_property/linter.py +419 -0
- src/linters/method_property/python_analyzer.py +472 -0
- src/linters/method_property/violation_builder.py +116 -0
- src/linters/nesting/python_analyzer.py +4 -0
- src/linters/nesting/typescript_function_extractor.py +4 -0
- src/linters/print_statements/typescript_analyzer.py +4 -0
- src/linters/srp/class_analyzer.py +4 -0
- src/linters/srp/python_analyzer.py +55 -20
- src/linters/srp/typescript_metrics_calculator.py +83 -47
- src/linters/srp/violation_builder.py +4 -0
- src/linters/stateless_class/__init__.py +25 -0
- src/linters/stateless_class/config.py +58 -0
- src/linters/stateless_class/linter.py +355 -0
- src/linters/stateless_class/python_analyzer.py +299 -0
- {thailint-0.7.0.dist-info → thailint-0.9.0.dist-info}/METADATA +119 -3
- {thailint-0.7.0.dist-info → thailint-0.9.0.dist-info}/RECORD +41 -32
- {thailint-0.7.0.dist-info → thailint-0.9.0.dist-info}/WHEEL +0 -0
- {thailint-0.7.0.dist-info → thailint-0.9.0.dist-info}/entry_points.txt +0 -0
- {thailint-0.7.0.dist-info → thailint-0.9.0.dist-info}/licenses/LICENSE +0 -0
src/cli.py
CHANGED
|
@@ -11,7 +11,7 @@ Overview: Provides the main CLI application using Click decorators for command d
|
|
|
11
11
|
|
|
12
12
|
Dependencies: click for CLI framework, logging for structured output, pathlib for file paths
|
|
13
13
|
|
|
14
|
-
Exports: cli (main command group), hello command, config command group,
|
|
14
|
+
Exports: cli (main command group), hello command, config command group, linter commands
|
|
15
15
|
|
|
16
16
|
Interfaces: Click CLI commands, configuration context via Click ctx, logging integration
|
|
17
17
|
|
|
@@ -1778,5 +1778,237 @@ def _execute_file_header_lint( # pylint: disable=too-many-arguments,too-many-po
|
|
|
1778
1778
|
sys.exit(1 if file_header_violations else 0)
|
|
1779
1779
|
|
|
1780
1780
|
|
|
1781
|
+
# =============================================================================
|
|
1782
|
+
# Method Property Linter Command
|
|
1783
|
+
# =============================================================================
|
|
1784
|
+
|
|
1785
|
+
|
|
1786
|
+
def _setup_method_property_orchestrator(
|
|
1787
|
+
path_objs: list[Path], config_file: str | None, verbose: bool, project_root: Path | None = None
|
|
1788
|
+
):
|
|
1789
|
+
"""Set up orchestrator for method-property command."""
|
|
1790
|
+
from src.orchestrator.core import Orchestrator
|
|
1791
|
+
from src.utils.project_root import get_project_root
|
|
1792
|
+
|
|
1793
|
+
if project_root is None:
|
|
1794
|
+
first_path = path_objs[0] if path_objs else Path.cwd()
|
|
1795
|
+
search_start = first_path if first_path.is_dir() else first_path.parent
|
|
1796
|
+
project_root = get_project_root(search_start)
|
|
1797
|
+
|
|
1798
|
+
orchestrator = Orchestrator(project_root=project_root)
|
|
1799
|
+
|
|
1800
|
+
if config_file:
|
|
1801
|
+
_load_config_file(orchestrator, config_file, verbose)
|
|
1802
|
+
|
|
1803
|
+
return orchestrator
|
|
1804
|
+
|
|
1805
|
+
|
|
1806
|
+
def _run_method_property_lint(orchestrator, path_objs: list[Path], recursive: bool):
|
|
1807
|
+
"""Execute method-property lint on files or directories."""
|
|
1808
|
+
all_violations = _execute_linting_on_paths(orchestrator, path_objs, recursive)
|
|
1809
|
+
return [v for v in all_violations if "method-property" in v.rule_id]
|
|
1810
|
+
|
|
1811
|
+
|
|
1812
|
+
@cli.command("method-property")
|
|
1813
|
+
@click.argument("paths", nargs=-1, type=click.Path())
|
|
1814
|
+
@click.option("--config", "-c", "config_file", type=click.Path(), help="Path to config file")
|
|
1815
|
+
@format_option
|
|
1816
|
+
@click.option("--recursive/--no-recursive", default=True, help="Scan directories recursively")
|
|
1817
|
+
@click.pass_context
|
|
1818
|
+
def method_property(
|
|
1819
|
+
ctx,
|
|
1820
|
+
paths: tuple[str, ...],
|
|
1821
|
+
config_file: str | None,
|
|
1822
|
+
format: str,
|
|
1823
|
+
recursive: bool,
|
|
1824
|
+
):
|
|
1825
|
+
"""Check for methods that should be @property decorators.
|
|
1826
|
+
|
|
1827
|
+
Detects Python methods that could be converted to properties following
|
|
1828
|
+
Pythonic conventions:
|
|
1829
|
+
- Methods returning only self._attribute or self.attribute
|
|
1830
|
+
- get_* prefixed methods (Java-style getters)
|
|
1831
|
+
- Simple computed values with no side effects
|
|
1832
|
+
|
|
1833
|
+
PATHS: Files or directories to lint (defaults to current directory if none provided)
|
|
1834
|
+
|
|
1835
|
+
Examples:
|
|
1836
|
+
|
|
1837
|
+
\b
|
|
1838
|
+
# Check current directory (all files recursively)
|
|
1839
|
+
thai-lint method-property
|
|
1840
|
+
|
|
1841
|
+
\b
|
|
1842
|
+
# Check specific directory
|
|
1843
|
+
thai-lint method-property src/
|
|
1844
|
+
|
|
1845
|
+
\b
|
|
1846
|
+
# Check single file
|
|
1847
|
+
thai-lint method-property src/models.py
|
|
1848
|
+
|
|
1849
|
+
\b
|
|
1850
|
+
# Check multiple files
|
|
1851
|
+
thai-lint method-property src/models.py src/services.py
|
|
1852
|
+
|
|
1853
|
+
\b
|
|
1854
|
+
# Get JSON output
|
|
1855
|
+
thai-lint method-property --format json .
|
|
1856
|
+
|
|
1857
|
+
\b
|
|
1858
|
+
# Get SARIF output for CI/CD integration
|
|
1859
|
+
thai-lint method-property --format sarif src/
|
|
1860
|
+
|
|
1861
|
+
\b
|
|
1862
|
+
# Use custom config file
|
|
1863
|
+
thai-lint method-property --config .thailint.yaml src/
|
|
1864
|
+
"""
|
|
1865
|
+
verbose = ctx.obj.get("verbose", False)
|
|
1866
|
+
project_root = _get_project_root_from_context(ctx)
|
|
1867
|
+
|
|
1868
|
+
if not paths:
|
|
1869
|
+
paths = (".",)
|
|
1870
|
+
|
|
1871
|
+
path_objs = [Path(p) for p in paths]
|
|
1872
|
+
|
|
1873
|
+
try:
|
|
1874
|
+
_execute_method_property_lint(
|
|
1875
|
+
path_objs, config_file, format, recursive, verbose, project_root
|
|
1876
|
+
)
|
|
1877
|
+
except Exception as e:
|
|
1878
|
+
_handle_linting_error(e, verbose)
|
|
1879
|
+
|
|
1880
|
+
|
|
1881
|
+
def _execute_method_property_lint( # pylint: disable=too-many-arguments,too-many-positional-arguments
|
|
1882
|
+
path_objs, config_file, format, recursive, verbose, project_root=None
|
|
1883
|
+
):
|
|
1884
|
+
"""Execute method-property lint."""
|
|
1885
|
+
_validate_paths_exist(path_objs)
|
|
1886
|
+
orchestrator = _setup_method_property_orchestrator(
|
|
1887
|
+
path_objs, config_file, verbose, project_root
|
|
1888
|
+
)
|
|
1889
|
+
method_property_violations = _run_method_property_lint(orchestrator, path_objs, recursive)
|
|
1890
|
+
|
|
1891
|
+
if verbose:
|
|
1892
|
+
logger.info(f"Found {len(method_property_violations)} method-property violation(s)")
|
|
1893
|
+
|
|
1894
|
+
format_violations(method_property_violations, format)
|
|
1895
|
+
sys.exit(1 if method_property_violations else 0)
|
|
1896
|
+
|
|
1897
|
+
|
|
1898
|
+
# =============================================================================
|
|
1899
|
+
# Stateless Class Linter Command
|
|
1900
|
+
# =============================================================================
|
|
1901
|
+
|
|
1902
|
+
|
|
1903
|
+
def _setup_stateless_class_orchestrator(
|
|
1904
|
+
path_objs: list[Path], config_file: str | None, verbose: bool, project_root: Path | None = None
|
|
1905
|
+
):
|
|
1906
|
+
"""Set up orchestrator for stateless-class command."""
|
|
1907
|
+
from src.orchestrator.core import Orchestrator
|
|
1908
|
+
from src.utils.project_root import get_project_root
|
|
1909
|
+
|
|
1910
|
+
if project_root is None:
|
|
1911
|
+
first_path = path_objs[0] if path_objs else Path.cwd()
|
|
1912
|
+
search_start = first_path if first_path.is_dir() else first_path.parent
|
|
1913
|
+
project_root = get_project_root(search_start)
|
|
1914
|
+
|
|
1915
|
+
orchestrator = Orchestrator(project_root=project_root)
|
|
1916
|
+
|
|
1917
|
+
if config_file:
|
|
1918
|
+
_load_config_file(orchestrator, config_file, verbose)
|
|
1919
|
+
|
|
1920
|
+
return orchestrator
|
|
1921
|
+
|
|
1922
|
+
|
|
1923
|
+
def _run_stateless_class_lint(orchestrator, path_objs: list[Path], recursive: bool):
|
|
1924
|
+
"""Execute stateless-class lint on files or directories."""
|
|
1925
|
+
all_violations = _execute_linting_on_paths(orchestrator, path_objs, recursive)
|
|
1926
|
+
return [v for v in all_violations if "stateless-class" in v.rule_id]
|
|
1927
|
+
|
|
1928
|
+
|
|
1929
|
+
@cli.command("stateless-class")
|
|
1930
|
+
@click.argument("paths", nargs=-1, type=click.Path())
|
|
1931
|
+
@click.option("--config", "-c", "config_file", type=click.Path(), help="Path to config file")
|
|
1932
|
+
@format_option
|
|
1933
|
+
@click.option("--recursive/--no-recursive", default=True, help="Scan directories recursively")
|
|
1934
|
+
@click.pass_context
|
|
1935
|
+
def stateless_class(
|
|
1936
|
+
ctx,
|
|
1937
|
+
paths: tuple[str, ...],
|
|
1938
|
+
config_file: str | None,
|
|
1939
|
+
format: str,
|
|
1940
|
+
recursive: bool,
|
|
1941
|
+
):
|
|
1942
|
+
"""Check for stateless classes that should be module functions.
|
|
1943
|
+
|
|
1944
|
+
Detects Python classes that have no constructor (__init__), no instance
|
|
1945
|
+
state, and 2+ methods - indicating they should be refactored to module-level
|
|
1946
|
+
functions instead of using a class as a namespace.
|
|
1947
|
+
|
|
1948
|
+
PATHS: Files or directories to lint (defaults to current directory if none provided)
|
|
1949
|
+
|
|
1950
|
+
Examples:
|
|
1951
|
+
|
|
1952
|
+
\b
|
|
1953
|
+
# Check current directory (all files recursively)
|
|
1954
|
+
thai-lint stateless-class
|
|
1955
|
+
|
|
1956
|
+
\b
|
|
1957
|
+
# Check specific directory
|
|
1958
|
+
thai-lint stateless-class src/
|
|
1959
|
+
|
|
1960
|
+
\b
|
|
1961
|
+
# Check single file
|
|
1962
|
+
thai-lint stateless-class src/utils.py
|
|
1963
|
+
|
|
1964
|
+
\b
|
|
1965
|
+
# Check multiple files
|
|
1966
|
+
thai-lint stateless-class src/utils.py src/helpers.py
|
|
1967
|
+
|
|
1968
|
+
\b
|
|
1969
|
+
# Get JSON output
|
|
1970
|
+
thai-lint stateless-class --format json .
|
|
1971
|
+
|
|
1972
|
+
\b
|
|
1973
|
+
# Get SARIF output for CI/CD integration
|
|
1974
|
+
thai-lint stateless-class --format sarif src/
|
|
1975
|
+
|
|
1976
|
+
\b
|
|
1977
|
+
# Use custom config file
|
|
1978
|
+
thai-lint stateless-class --config .thailint.yaml src/
|
|
1979
|
+
"""
|
|
1980
|
+
verbose = ctx.obj.get("verbose", False)
|
|
1981
|
+
project_root = _get_project_root_from_context(ctx)
|
|
1982
|
+
|
|
1983
|
+
if not paths:
|
|
1984
|
+
paths = (".",)
|
|
1985
|
+
|
|
1986
|
+
path_objs = [Path(p) for p in paths]
|
|
1987
|
+
|
|
1988
|
+
try:
|
|
1989
|
+
_execute_stateless_class_lint(
|
|
1990
|
+
path_objs, config_file, format, recursive, verbose, project_root
|
|
1991
|
+
)
|
|
1992
|
+
except Exception as e:
|
|
1993
|
+
_handle_linting_error(e, verbose)
|
|
1994
|
+
|
|
1995
|
+
|
|
1996
|
+
def _execute_stateless_class_lint( # pylint: disable=too-many-arguments,too-many-positional-arguments
|
|
1997
|
+
path_objs, config_file, format, recursive, verbose, project_root=None
|
|
1998
|
+
):
|
|
1999
|
+
"""Execute stateless-class lint."""
|
|
2000
|
+
_validate_paths_exist(path_objs)
|
|
2001
|
+
orchestrator = _setup_stateless_class_orchestrator(
|
|
2002
|
+
path_objs, config_file, verbose, project_root
|
|
2003
|
+
)
|
|
2004
|
+
stateless_class_violations = _run_stateless_class_lint(orchestrator, path_objs, recursive)
|
|
2005
|
+
|
|
2006
|
+
if verbose:
|
|
2007
|
+
logger.info(f"Found {len(stateless_class_violations)} stateless-class violation(s)")
|
|
2008
|
+
|
|
2009
|
+
format_violations(stateless_class_violations, format)
|
|
2010
|
+
sys.exit(1 if stateless_class_violations else 0)
|
|
2011
|
+
|
|
2012
|
+
|
|
1781
2013
|
if __name__ == "__main__":
|
|
1782
2014
|
cli()
|
src/core/base.py
CHANGED
|
@@ -151,6 +151,10 @@ class MultiLanguageLintRule(BaseLintRule):
|
|
|
151
151
|
- _load_config(context) for configuration loading
|
|
152
152
|
"""
|
|
153
153
|
|
|
154
|
+
def __init__(self) -> None:
|
|
155
|
+
"""Initialize the multi-language lint rule."""
|
|
156
|
+
pass # Base class for multi-language linters
|
|
157
|
+
|
|
154
158
|
def check(self, context: BaseLintContext) -> list[Violation]:
|
|
155
159
|
"""Check for violations with automatic language dispatch.
|
|
156
160
|
|
src/core/rule_discovery.py
CHANGED
|
@@ -10,7 +10,7 @@ Overview: Provides automatic rule discovery functionality for the linter framewo
|
|
|
10
10
|
|
|
11
11
|
Dependencies: importlib, inspect, pkgutil, BaseLintRule
|
|
12
12
|
|
|
13
|
-
Exports: RuleDiscovery
|
|
13
|
+
Exports: discover_from_package function, RuleDiscovery class (compat)
|
|
14
14
|
|
|
15
15
|
Interfaces: discover_from_package(package_path) -> list[BaseLintRule]
|
|
16
16
|
|
|
@@ -25,108 +25,134 @@ from typing import Any
|
|
|
25
25
|
from .base import BaseLintRule
|
|
26
26
|
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
"""
|
|
28
|
+
def discover_from_package(package_path: str) -> list[BaseLintRule]:
|
|
29
|
+
"""Discover rules from a package and its modules.
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
Args:
|
|
32
|
+
package_path: Python package path (e.g., 'src.linters')
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
Returns:
|
|
35
|
+
List of discovered rule instances
|
|
36
|
+
"""
|
|
37
|
+
try:
|
|
38
|
+
package = importlib.import_module(package_path)
|
|
39
|
+
except ImportError:
|
|
40
|
+
return []
|
|
36
41
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"""
|
|
40
|
-
try:
|
|
41
|
-
package = importlib.import_module(package_path)
|
|
42
|
-
except ImportError:
|
|
43
|
-
return []
|
|
42
|
+
if not hasattr(package, "__path__"):
|
|
43
|
+
return _discover_from_module(package_path)
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
return self._discover_from_module(package_path)
|
|
45
|
+
return _discover_from_package_modules(package_path, package)
|
|
47
46
|
|
|
48
|
-
return self._discover_from_package_modules(package_path, package)
|
|
49
47
|
|
|
50
|
-
|
|
51
|
-
|
|
48
|
+
def _discover_from_package_modules(package_path: str, package: Any) -> list[BaseLintRule]:
|
|
49
|
+
"""Discover rules from all modules in a package.
|
|
52
50
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
51
|
+
Args:
|
|
52
|
+
package_path: Package path
|
|
53
|
+
package: Imported package object
|
|
56
54
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
55
|
+
Returns:
|
|
56
|
+
List of discovered rules
|
|
57
|
+
"""
|
|
58
|
+
rules = []
|
|
59
|
+
for _, module_name, _ in pkgutil.iter_modules(package.__path__):
|
|
60
|
+
full_module_name = f"{package_path}.{module_name}"
|
|
61
|
+
module_rules = _try_discover_from_module(full_module_name)
|
|
62
|
+
rules.extend(module_rules)
|
|
63
|
+
return rules
|
|
66
64
|
|
|
67
|
-
def _try_discover_from_module(self, module_name: str) -> list[BaseLintRule]:
|
|
68
|
-
"""Try to discover rules from a module, return empty list on error.
|
|
69
65
|
|
|
70
|
-
|
|
71
|
-
|
|
66
|
+
def _try_discover_from_module(module_name: str) -> list[BaseLintRule]:
|
|
67
|
+
"""Try to discover rules from a module, return empty list on error.
|
|
72
68
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
"""
|
|
76
|
-
try:
|
|
77
|
-
return self._discover_from_module(module_name)
|
|
78
|
-
except (ImportError, AttributeError):
|
|
79
|
-
return []
|
|
69
|
+
Args:
|
|
70
|
+
module_name: Full module name
|
|
80
71
|
|
|
81
|
-
|
|
82
|
-
|
|
72
|
+
Returns:
|
|
73
|
+
List of discovered rules (empty on error)
|
|
74
|
+
"""
|
|
75
|
+
try:
|
|
76
|
+
return _discover_from_module(module_name)
|
|
77
|
+
except (ImportError, AttributeError):
|
|
78
|
+
return []
|
|
83
79
|
|
|
84
|
-
Args:
|
|
85
|
-
module_path: Full module path to search
|
|
86
80
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
"""
|
|
90
|
-
try:
|
|
91
|
-
module = importlib.import_module(module_path)
|
|
92
|
-
except (ImportError, AttributeError):
|
|
93
|
-
return []
|
|
94
|
-
|
|
95
|
-
rules = []
|
|
96
|
-
for _name, obj in inspect.getmembers(module):
|
|
97
|
-
if not self._is_rule_class(obj):
|
|
98
|
-
continue
|
|
99
|
-
rule_instance = self._try_instantiate_rule(obj)
|
|
100
|
-
if rule_instance:
|
|
101
|
-
rules.append(rule_instance)
|
|
102
|
-
return rules
|
|
103
|
-
|
|
104
|
-
def _try_instantiate_rule(self, rule_class: type[BaseLintRule]) -> BaseLintRule | None:
|
|
105
|
-
"""Try to instantiate a rule class.
|
|
81
|
+
def _discover_from_module(module_path: str) -> list[BaseLintRule]:
|
|
82
|
+
"""Discover rules from a specific module.
|
|
106
83
|
|
|
107
|
-
|
|
108
|
-
|
|
84
|
+
Args:
|
|
85
|
+
module_path: Full module path to search
|
|
109
86
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
87
|
+
Returns:
|
|
88
|
+
List of discovered rule instances
|
|
89
|
+
"""
|
|
90
|
+
try:
|
|
91
|
+
module = importlib.import_module(module_path)
|
|
92
|
+
except (ImportError, AttributeError):
|
|
93
|
+
return []
|
|
94
|
+
|
|
95
|
+
rules = []
|
|
96
|
+
for _name, obj in inspect.getmembers(module):
|
|
97
|
+
if not _is_rule_class(obj):
|
|
98
|
+
continue
|
|
99
|
+
rule_instance = _try_instantiate_rule(obj)
|
|
100
|
+
if rule_instance:
|
|
101
|
+
rules.append(rule_instance)
|
|
102
|
+
return rules
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _try_instantiate_rule(rule_class: type[BaseLintRule]) -> BaseLintRule | None:
|
|
106
|
+
"""Try to instantiate a rule class.
|
|
107
|
+
|
|
108
|
+
Args:
|
|
109
|
+
rule_class: Rule class to instantiate
|
|
110
|
+
|
|
111
|
+
Returns:
|
|
112
|
+
Rule instance or None on error
|
|
113
|
+
"""
|
|
114
|
+
try:
|
|
115
|
+
return rule_class()
|
|
116
|
+
except (TypeError, AttributeError):
|
|
117
|
+
return None
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _is_rule_class(obj: Any) -> bool:
|
|
121
|
+
"""Check if an object is a valid rule class.
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
obj: Object to check
|
|
125
|
+
|
|
126
|
+
Returns:
|
|
127
|
+
True if obj is a concrete BaseLintRule subclass
|
|
128
|
+
"""
|
|
129
|
+
return (
|
|
130
|
+
inspect.isclass(obj)
|
|
131
|
+
and issubclass(obj, BaseLintRule)
|
|
132
|
+
and obj is not BaseLintRule
|
|
133
|
+
and not inspect.isabstract(obj)
|
|
134
|
+
)
|
|
117
135
|
|
|
118
|
-
|
|
119
|
-
|
|
136
|
+
|
|
137
|
+
# Legacy class wrapper for backward compatibility
|
|
138
|
+
class RuleDiscovery:
|
|
139
|
+
"""Discovers linting rules from Python packages.
|
|
140
|
+
|
|
141
|
+
Note: This class is a thin wrapper around module-level functions
|
|
142
|
+
for backward compatibility.
|
|
143
|
+
"""
|
|
144
|
+
|
|
145
|
+
def __init__(self) -> None:
|
|
146
|
+
"""Initialize the discovery service."""
|
|
147
|
+
pass # No state needed
|
|
148
|
+
|
|
149
|
+
def discover_from_package(self, package_path: str) -> list[BaseLintRule]:
|
|
150
|
+
"""Discover rules from a package and its modules.
|
|
120
151
|
|
|
121
152
|
Args:
|
|
122
|
-
|
|
153
|
+
package_path: Python package path (e.g., 'src.linters')
|
|
123
154
|
|
|
124
155
|
Returns:
|
|
125
|
-
|
|
156
|
+
List of discovered rule instances
|
|
126
157
|
"""
|
|
127
|
-
return (
|
|
128
|
-
inspect.isclass(obj)
|
|
129
|
-
and issubclass(obj, BaseLintRule)
|
|
130
|
-
and obj is not BaseLintRule
|
|
131
|
-
and not inspect.isabstract(obj)
|
|
132
|
-
)
|
|
158
|
+
return discover_from_package(package_path)
|
src/core/violation_builder.py
CHANGED
|
@@ -13,13 +13,14 @@ Overview: Provides base classes and data structures for violation creation acros
|
|
|
13
13
|
|
|
14
14
|
Dependencies: dataclasses, src.core.types (Violation, Severity)
|
|
15
15
|
|
|
16
|
-
Exports: ViolationInfo dataclass,
|
|
16
|
+
Exports: ViolationInfo dataclass, build_violation function, build_violation_from_params function,
|
|
17
|
+
BaseViolationBuilder class (compat)
|
|
17
18
|
|
|
18
19
|
Interfaces: ViolationInfo(rule_id, file_path, line, message, column, severity),
|
|
19
|
-
|
|
20
|
+
build_violation(info: ViolationInfo) -> Violation
|
|
20
21
|
|
|
21
|
-
Implementation: Uses dataclass for type-safe violation info,
|
|
22
|
-
|
|
22
|
+
Implementation: Uses dataclass for type-safe violation info, functions provide build logic
|
|
23
|
+
that constructs Violation objects with proper defaults
|
|
23
24
|
"""
|
|
24
25
|
|
|
25
26
|
from dataclasses import dataclass
|
|
@@ -50,14 +51,82 @@ class ViolationInfo:
|
|
|
50
51
|
suggestion: str | None = None
|
|
51
52
|
|
|
52
53
|
|
|
54
|
+
def build_violation(info: ViolationInfo) -> Violation:
|
|
55
|
+
"""Build a Violation from ViolationInfo.
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
info: ViolationInfo containing all violation details
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
Violation object with all fields populated
|
|
62
|
+
"""
|
|
63
|
+
return Violation(
|
|
64
|
+
rule_id=info.rule_id,
|
|
65
|
+
file_path=info.file_path,
|
|
66
|
+
line=info.line,
|
|
67
|
+
column=info.column,
|
|
68
|
+
message=info.message,
|
|
69
|
+
severity=info.severity,
|
|
70
|
+
suggestion=info.suggestion,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def build_violation_from_params( # pylint: disable=too-many-arguments,too-many-positional-arguments
|
|
75
|
+
rule_id: str,
|
|
76
|
+
file_path: str,
|
|
77
|
+
line: int,
|
|
78
|
+
message: str,
|
|
79
|
+
column: int = 1,
|
|
80
|
+
severity: Severity = Severity.ERROR,
|
|
81
|
+
suggestion: str | None = None,
|
|
82
|
+
) -> Violation:
|
|
83
|
+
"""Build a Violation directly from parameters.
|
|
84
|
+
|
|
85
|
+
Note: Pylint too-many-arguments disabled. This convenience function mirrors the
|
|
86
|
+
ViolationInfo dataclass fields (7 parameters, 3 with defaults). The alternative
|
|
87
|
+
would require every caller to create ViolationInfo objects manually, reducing
|
|
88
|
+
readability.
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
rule_id: Identifier for the rule that was violated
|
|
92
|
+
file_path: Path to the file containing the violation
|
|
93
|
+
line: Line number where violation occurs (1-indexed)
|
|
94
|
+
message: Description of the violation
|
|
95
|
+
column: Column number where violation occurs (0-indexed, default=1)
|
|
96
|
+
severity: Severity level of the violation (default=ERROR)
|
|
97
|
+
suggestion: Optional suggestion for fixing the violation
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
Violation object with all fields populated
|
|
101
|
+
"""
|
|
102
|
+
info = ViolationInfo(
|
|
103
|
+
rule_id=rule_id,
|
|
104
|
+
file_path=file_path,
|
|
105
|
+
line=line,
|
|
106
|
+
message=message,
|
|
107
|
+
column=column,
|
|
108
|
+
severity=severity,
|
|
109
|
+
suggestion=suggestion,
|
|
110
|
+
)
|
|
111
|
+
return build_violation(info)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
# Legacy class wrapper for backward compatibility
|
|
53
115
|
class BaseViolationBuilder:
|
|
54
116
|
"""Base class for building violations with consistent structure.
|
|
55
117
|
|
|
56
118
|
Provides common build() method for creating Violation objects from ViolationInfo.
|
|
57
119
|
Linter-specific builders extend this class to add their domain-specific violation
|
|
58
120
|
creation methods while inheriting the common construction logic.
|
|
121
|
+
|
|
122
|
+
Note: This class is a thin wrapper around module-level functions
|
|
123
|
+
for backward compatibility.
|
|
59
124
|
"""
|
|
60
125
|
|
|
126
|
+
def __init__(self) -> None:
|
|
127
|
+
"""Initialize the builder."""
|
|
128
|
+
pass # No state needed
|
|
129
|
+
|
|
61
130
|
def build(self, info: ViolationInfo) -> Violation:
|
|
62
131
|
"""Build a Violation from ViolationInfo.
|
|
63
132
|
|
|
@@ -67,15 +136,7 @@ class BaseViolationBuilder:
|
|
|
67
136
|
Returns:
|
|
68
137
|
Violation object with all fields populated
|
|
69
138
|
"""
|
|
70
|
-
return
|
|
71
|
-
rule_id=info.rule_id,
|
|
72
|
-
file_path=info.file_path,
|
|
73
|
-
line=info.line,
|
|
74
|
-
column=info.column,
|
|
75
|
-
message=info.message,
|
|
76
|
-
severity=info.severity,
|
|
77
|
-
suggestion=info.suggestion,
|
|
78
|
-
)
|
|
139
|
+
return build_violation(info)
|
|
79
140
|
|
|
80
141
|
def build_from_params( # pylint: disable=too-many-arguments,too-many-positional-arguments
|
|
81
142
|
self,
|
|
@@ -110,7 +171,7 @@ class BaseViolationBuilder:
|
|
|
110
171
|
Returns:
|
|
111
172
|
Violation object with all fields populated
|
|
112
173
|
"""
|
|
113
|
-
|
|
174
|
+
return build_violation_from_params(
|
|
114
175
|
rule_id=rule_id,
|
|
115
176
|
file_path=file_path,
|
|
116
177
|
line=line,
|
|
@@ -119,4 +180,3 @@ class BaseViolationBuilder:
|
|
|
119
180
|
severity=severity,
|
|
120
181
|
suggestion=suggestion,
|
|
121
182
|
)
|
|
122
|
-
return self.build(info)
|