lint-utils 0.1.0__tar.gz
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.
Potentially problematic release.
This version of lint-utils might be problematic. Click here for more details.
- lint_utils-0.1.0/.gitignore +20 -0
- lint_utils-0.1.0/LICENSE +21 -0
- lint_utils-0.1.0/PKG-INFO +8 -0
- lint_utils-0.1.0/README.md +0 -0
- lint_utils-0.1.0/_excluded/__init__.py +0 -0
- lint_utils-0.1.0/_excluded/example.py +12 -0
- lint_utils-0.1.0/_excluded/example_2.py +24 -0
- lint_utils-0.1.0/_excluded/example_3.py +6 -0
- lint_utils-0.1.0/_main.py +25 -0
- lint_utils-0.1.0/lint_utils/__init__.py +0 -0
- lint_utils-0.1.0/lint_utils/__main__.py +58 -0
- lint_utils-0.1.0/lint_utils/_cli_commands/__init__.py +0 -0
- lint_utils-0.1.0/lint_utils/_cli_commands/check.py +52 -0
- lint_utils-0.1.0/lint_utils/config.py +44 -0
- lint_utils-0.1.0/lint_utils/py.typed +0 -0
- lint_utils-0.1.0/lint_utils/rules.py +9 -0
- lint_utils-0.1.0/lint_utils/std.py +9 -0
- lint_utils-0.1.0/lint_utils/text_styling.py +21 -0
- lint_utils-0.1.0/lint_utils/timer.py +41 -0
- lint_utils-0.1.0/lint_utils/tree_info.py +17 -0
- lint_utils-0.1.0/lint_utils/visitors/__init__.py +6 -0
- lint_utils-0.1.0/lint_utils/visitors/base.py +21 -0
- lint_utils-0.1.0/lint_utils/visitors/dto.py +8 -0
- lint_utils-0.1.0/lint_utils/visitors/useless_fields.py +208 -0
- lint_utils-0.1.0/pyproject.toml +24 -0
- lint_utils-0.1.0/uv.lock +79 -0
lint_utils-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 barbarrista
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
class BaseCommand:
|
|
2
|
+
def __init__(self, db_context: str) -> None:
|
|
3
|
+
self._db_context = db_context
|
|
4
|
+
|
|
5
|
+
async def execute(self) -> int:
|
|
6
|
+
raise NotImplementedError
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ChildCommand(BaseCommand):
|
|
10
|
+
async def execute(self) -> int:
|
|
11
|
+
return 2
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ChildCommand2(BaseCommand):
|
|
15
|
+
async def execute(self) -> int:
|
|
16
|
+
return 3
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Command2:
|
|
20
|
+
def __init__(self, db_context: str) -> None:
|
|
21
|
+
self._nani_ctx = db_context
|
|
22
|
+
|
|
23
|
+
async def execute(self) -> int:
|
|
24
|
+
return 3
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from lint_utils.config import PyProject
|
|
3
|
+
from lint_utils.tree_info import get_tree_info
|
|
4
|
+
from lint_utils.visitors.useless_fields import check_useless_field
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def main() -> None:
|
|
8
|
+
config = PyProject.from_toml(Path("pyproject.toml"))
|
|
9
|
+
|
|
10
|
+
path = Path("_excluded")
|
|
11
|
+
info = get_tree_info(path)
|
|
12
|
+
assert info
|
|
13
|
+
assert config
|
|
14
|
+
assert config.tool
|
|
15
|
+
assert config.tool.lint_utils
|
|
16
|
+
has_useless_fields = check_useless_field(
|
|
17
|
+
info,
|
|
18
|
+
file_path=path,
|
|
19
|
+
config=config.tool.lint_utils,
|
|
20
|
+
)
|
|
21
|
+
print(has_useless_fields)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
if __name__ == "__main__":
|
|
25
|
+
main()
|
|
File without changes
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
from collections.abc import Sequence
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
import click
|
|
4
|
+
|
|
5
|
+
from lint_utils._cli_commands.check import CheckCommand
|
|
6
|
+
from lint_utils.config import PyProject
|
|
7
|
+
from lint_utils.std import report_info
|
|
8
|
+
from lint_utils.text_styling import pluralize, to_bold, to_green, to_red
|
|
9
|
+
from lint_utils.timer import Timer
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@click.group()
|
|
13
|
+
def cli() -> None:
|
|
14
|
+
pass
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@cli.command("check")
|
|
18
|
+
@click.argument("args", nargs=-1)
|
|
19
|
+
def check(args: Sequence[str]) -> None:
|
|
20
|
+
pyproject = PyProject.from_toml(Path("pyproject.toml"))
|
|
21
|
+
lint_utils_config = (
|
|
22
|
+
pyproject.tool.lint_utils if pyproject and pyproject.tool else None
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
files_count = 0
|
|
26
|
+
errors_files_count = 0
|
|
27
|
+
not_processed_files: list[str] = []
|
|
28
|
+
|
|
29
|
+
if not args:
|
|
30
|
+
report_info(to_red("Please provide the file or directory name"))
|
|
31
|
+
return
|
|
32
|
+
|
|
33
|
+
with Timer() as timer:
|
|
34
|
+
for arg in args:
|
|
35
|
+
root_path = Path(arg)
|
|
36
|
+
paths = root_path.rglob("*.py") if root_path.is_dir() else (root_path,)
|
|
37
|
+
command = CheckCommand(paths=paths, config=lint_utils_config)
|
|
38
|
+
result = command.execute()
|
|
39
|
+
|
|
40
|
+
not_processed_files.extend(result.not_processed_files)
|
|
41
|
+
files_count += result.files_count
|
|
42
|
+
errors_files_count += result.errors_files_count
|
|
43
|
+
|
|
44
|
+
if errors_files_count > 0:
|
|
45
|
+
files_part = pluralize(errors_files_count, "file")
|
|
46
|
+
msg = to_bold(
|
|
47
|
+
to_red(f"Errors found in {errors_files_count} {files_part} 😱")
|
|
48
|
+
)
|
|
49
|
+
report_info(msg)
|
|
50
|
+
else:
|
|
51
|
+
report_info(to_bold(to_green("No errors found. All is well 🤗")))
|
|
52
|
+
|
|
53
|
+
total_info = f"Processed {files_count} {pluralize(files_count, 'file')} at {timer.total_seconds}"
|
|
54
|
+
report_info(to_bold((total_info)))
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
if __name__ == "__main__":
|
|
58
|
+
cli()
|
|
File without changes
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from collections.abc import Iterable
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from lint_utils.config import LintUtilsConfig
|
|
6
|
+
from lint_utils.tree_info import get_tree_info
|
|
7
|
+
from lint_utils.visitors.useless_fields import check_useless_field
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
11
|
+
class CheckResult:
|
|
12
|
+
files_count: int
|
|
13
|
+
errors_files_count: int
|
|
14
|
+
not_processed_files: list[str]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class CheckCommand:
|
|
18
|
+
def __init__(
|
|
19
|
+
self,
|
|
20
|
+
paths: Iterable[Path],
|
|
21
|
+
config: LintUtilsConfig | None,
|
|
22
|
+
) -> None:
|
|
23
|
+
self._paths = paths
|
|
24
|
+
self._config = config
|
|
25
|
+
|
|
26
|
+
def execute(self) -> CheckResult:
|
|
27
|
+
files_count = 0
|
|
28
|
+
errors_files_count = 0
|
|
29
|
+
not_processed_files: list[str] = []
|
|
30
|
+
|
|
31
|
+
for path in self._paths:
|
|
32
|
+
info = get_tree_info(path)
|
|
33
|
+
if info is None:
|
|
34
|
+
not_processed_files.append(path.as_posix())
|
|
35
|
+
continue
|
|
36
|
+
|
|
37
|
+
has_errors = check_useless_field(
|
|
38
|
+
info,
|
|
39
|
+
file_path=path,
|
|
40
|
+
config=self._config,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
if has_errors:
|
|
44
|
+
errors_files_count += 1
|
|
45
|
+
|
|
46
|
+
files_count += 1
|
|
47
|
+
|
|
48
|
+
return CheckResult(
|
|
49
|
+
files_count=files_count,
|
|
50
|
+
errors_files_count=errors_files_count,
|
|
51
|
+
not_processed_files=not_processed_files,
|
|
52
|
+
)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Self
|
|
3
|
+
import msgspec
|
|
4
|
+
|
|
5
|
+
from lint_utils.std import report_error
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Base(
|
|
9
|
+
msgspec.Struct,
|
|
10
|
+
omit_defaults=True,
|
|
11
|
+
forbid_unknown_fields=False,
|
|
12
|
+
):
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class LintConfig(Base):
|
|
17
|
+
ignore: list[str] = msgspec.field(default_factory=list)
|
|
18
|
+
exclude_classes: list[str] = msgspec.field(default_factory=list)
|
|
19
|
+
exclude_base_classes: list[str] = msgspec.field(default_factory=list)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class LintUtilsConfig(Base):
|
|
23
|
+
lint: LintConfig = msgspec.field(default_factory=LintConfig)
|
|
24
|
+
exclude: list[str] = msgspec.field(default_factory=list)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Tool(Base):
|
|
28
|
+
lint_utils: LintUtilsConfig | None = None
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class PyProject(Base):
|
|
32
|
+
tool: Tool | None = None
|
|
33
|
+
|
|
34
|
+
@classmethod
|
|
35
|
+
def from_toml(cls, path: Path) -> Self | None:
|
|
36
|
+
try:
|
|
37
|
+
with path.open("r", encoding="UTF-8") as file:
|
|
38
|
+
return msgspec.toml.decode(file.read(), type=PyProject)
|
|
39
|
+
except OSError as exc:
|
|
40
|
+
if isinstance(exc, FileNotFoundError):
|
|
41
|
+
return None
|
|
42
|
+
|
|
43
|
+
msg = f"There was a problem parsing the file pyproject.toml. Error: {repr(exc)}"
|
|
44
|
+
report_error(msg)
|
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
def to_red(text: str) -> str:
|
|
2
|
+
return f"\033[31m{text}\033[0m"
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def to_cyan(text: str) -> str:
|
|
6
|
+
return f"\033[36m{text}\033[0m"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def to_green(text: str) -> str:
|
|
10
|
+
return f"\033[32m{text}\033[0m"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def to_bold(text: str) -> str:
|
|
14
|
+
return f"\033[1m{text}\033[0m"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def pluralize(count: int, singular: str, *, plural: str | None = None):
|
|
18
|
+
if count == 1:
|
|
19
|
+
return singular
|
|
20
|
+
|
|
21
|
+
return f"{plural or singular + 's'}"
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from types import TracebackType
|
|
3
|
+
from typing import Self
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Timer:
|
|
7
|
+
def __init__(self) -> None:
|
|
8
|
+
self._start_time: float | None = None
|
|
9
|
+
self._finish_time: float | None = None
|
|
10
|
+
|
|
11
|
+
@property
|
|
12
|
+
def elapsed_seconds(self) -> float:
|
|
13
|
+
if self._start_time is None:
|
|
14
|
+
msg = "Timer not started. Call `with Timer() as timer: ...`"
|
|
15
|
+
raise ValueError(msg)
|
|
16
|
+
|
|
17
|
+
if self._finish_time is None:
|
|
18
|
+
msg = "The timer has not finished its work yet"
|
|
19
|
+
raise ValueError(msg)
|
|
20
|
+
|
|
21
|
+
return self._finish_time - self._start_time
|
|
22
|
+
|
|
23
|
+
@property
|
|
24
|
+
def total_seconds(self) -> str:
|
|
25
|
+
elapsed = self.elapsed_seconds
|
|
26
|
+
minutes = int(elapsed // 60)
|
|
27
|
+
seconds = int(elapsed % 60)
|
|
28
|
+
milliseconds = int((elapsed - int(elapsed)) * 10000)
|
|
29
|
+
return f"{minutes}:{seconds:02}:{milliseconds:04}"
|
|
30
|
+
|
|
31
|
+
def __enter__(self) -> Self:
|
|
32
|
+
self._start_time = time.perf_counter()
|
|
33
|
+
return self
|
|
34
|
+
|
|
35
|
+
def __exit__(
|
|
36
|
+
self,
|
|
37
|
+
exc_type: type[BaseException] | None,
|
|
38
|
+
exc_val: BaseException | None,
|
|
39
|
+
exc_tb: TracebackType | None,
|
|
40
|
+
) -> None:
|
|
41
|
+
self._finish_time = time.perf_counter()
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from contextlib import suppress
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
import ast
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
9
|
+
class TreeInfo:
|
|
10
|
+
tree: ast.Module
|
|
11
|
+
raw: str
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def get_tree_info(file_path: Path) -> TreeInfo | None:
|
|
15
|
+
with suppress(SyntaxError, OSError, UnicodeDecodeError), file_path.open("r", encoding="UTF-8") as file:
|
|
16
|
+
source = file.read()
|
|
17
|
+
return TreeInfo(tree=ast.parse(source), raw=source)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
from typing import Protocol
|
|
3
|
+
|
|
4
|
+
from lint_utils.config import LintUtilsConfig
|
|
5
|
+
from lint_utils.visitors.dto import FileInfoDTO
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class HasRuleProtocol(Protocol):
|
|
9
|
+
rule: str
|
|
10
|
+
_config: LintUtilsConfig | None
|
|
11
|
+
_file_info: FileInfoDTO
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class BaseVisitor(HasRuleProtocol, ast.NodeVisitor):
|
|
15
|
+
@property
|
|
16
|
+
def can_skip_visitor(self) -> bool:
|
|
17
|
+
if self._config is None:
|
|
18
|
+
return False
|
|
19
|
+
|
|
20
|
+
can_skip = self.rule in self._config.lint.ignore
|
|
21
|
+
return can_skip or self._file_info.path.as_posix() in self._config.exclude
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
import itertools
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Any, Final, Mapping, TypeAlias
|
|
6
|
+
|
|
7
|
+
from lint_utils.config import LintUtilsConfig
|
|
8
|
+
from lint_utils.rules import Rule, can_ignore_rule
|
|
9
|
+
from lint_utils.std import report_info
|
|
10
|
+
from lint_utils.text_styling import to_bold, to_cyan, to_red
|
|
11
|
+
from lint_utils.tree_info import TreeInfo
|
|
12
|
+
from lint_utils.visitors.base import BaseVisitor
|
|
13
|
+
from lint_utils.visitors.dto import FileInfoDTO
|
|
14
|
+
|
|
15
|
+
FuncDef: TypeAlias = ast.FunctionDef | ast.AsyncFunctionDef
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
19
|
+
class FieldInfo:
|
|
20
|
+
class_name: str
|
|
21
|
+
name: str
|
|
22
|
+
line: int
|
|
23
|
+
col_offset: int
|
|
24
|
+
assigned_to: str | None = None
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class UselessFieldVisitor(BaseVisitor):
|
|
28
|
+
rule: Final[str] = Rule.useless_field
|
|
29
|
+
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
file_info: FileInfoDTO,
|
|
33
|
+
config: LintUtilsConfig | None = None,
|
|
34
|
+
) -> None:
|
|
35
|
+
super().__init__()
|
|
36
|
+
|
|
37
|
+
self._file_info = file_info
|
|
38
|
+
self._class_name: str | None = None
|
|
39
|
+
self._base_class_names: list[str] | None = None
|
|
40
|
+
self._field_definitions: dict[str, FieldInfo] = {}
|
|
41
|
+
self._config = config
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def useless_fields(self) -> Mapping[str, FieldInfo]:
|
|
45
|
+
return self._field_definitions
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def class_name(self) -> str:
|
|
49
|
+
if self._class_name:
|
|
50
|
+
return self._class_name
|
|
51
|
+
|
|
52
|
+
raise ValueError
|
|
53
|
+
|
|
54
|
+
def _clear(self) -> None:
|
|
55
|
+
self._class_name = None
|
|
56
|
+
self._base_class_names = None
|
|
57
|
+
self._field_definitions = {}
|
|
58
|
+
|
|
59
|
+
def visit_ClassDef(self, node: ast.ClassDef) -> Any:
|
|
60
|
+
if self.can_skip_visitor:
|
|
61
|
+
return
|
|
62
|
+
|
|
63
|
+
self._class_name = node.name
|
|
64
|
+
self._base_class_names = self._get_parent_class_names(node)
|
|
65
|
+
|
|
66
|
+
if self._is_class_excluded:
|
|
67
|
+
return
|
|
68
|
+
|
|
69
|
+
for item in node.body:
|
|
70
|
+
if not isinstance(item, FuncDef):
|
|
71
|
+
continue
|
|
72
|
+
|
|
73
|
+
if item.name == "__init__":
|
|
74
|
+
self._process_init_assignment(item)
|
|
75
|
+
|
|
76
|
+
self.generic_visit(node)
|
|
77
|
+
|
|
78
|
+
def visit_Attribute(self, node: ast.Attribute) -> Any:
|
|
79
|
+
if isinstance(node.ctx, ast.Load):
|
|
80
|
+
root_node = _find_root_node(node)
|
|
81
|
+
self._field_definitions.pop(self._build_field_key(root_node.attr), None)
|
|
82
|
+
|
|
83
|
+
return self.generic_visit(node)
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def _is_class_excluded(self) -> bool:
|
|
87
|
+
if self._config is None or self._config.lint is None:
|
|
88
|
+
return False
|
|
89
|
+
|
|
90
|
+
excluded_classes = self._config.lint.exclude_classes or ()
|
|
91
|
+
excluded_base_classes = self._config.lint.exclude_base_classes or ()
|
|
92
|
+
all_excluded_classes = list(
|
|
93
|
+
itertools.chain(excluded_classes, excluded_base_classes)
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
is_excluded = False
|
|
97
|
+
if self._base_class_names:
|
|
98
|
+
is_excluded = any(
|
|
99
|
+
cls in all_excluded_classes for cls in self._base_class_names
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
return is_excluded or self.class_name in all_excluded_classes
|
|
103
|
+
|
|
104
|
+
def _get_parent_class_names(self, node: ast.ClassDef) -> list[str] | None:
|
|
105
|
+
base_names = []
|
|
106
|
+
for base in node.bases:
|
|
107
|
+
if isinstance(base, ast.Name):
|
|
108
|
+
base_names.append(base.id)
|
|
109
|
+
|
|
110
|
+
return base_names
|
|
111
|
+
|
|
112
|
+
def _process_init_assignment(self, method: FuncDef):
|
|
113
|
+
for item in method.body:
|
|
114
|
+
match item:
|
|
115
|
+
case ast.Assign():
|
|
116
|
+
target = item.targets[0]
|
|
117
|
+
if not isinstance(target, ast.Attribute):
|
|
118
|
+
continue
|
|
119
|
+
|
|
120
|
+
field_info = FieldInfo(
|
|
121
|
+
class_name=self._class_name,
|
|
122
|
+
name=target.attr,
|
|
123
|
+
line=target.lineno,
|
|
124
|
+
col_offset=target.col_offset,
|
|
125
|
+
assigned_to=_get_assigned_to(item),
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
case ast.AnnAssign():
|
|
129
|
+
target = item.target
|
|
130
|
+
if not isinstance(target, ast.Attribute):
|
|
131
|
+
continue
|
|
132
|
+
|
|
133
|
+
field_info = FieldInfo(
|
|
134
|
+
class_name=self._class_name,
|
|
135
|
+
name=target.attr,
|
|
136
|
+
line=target.lineno,
|
|
137
|
+
col_offset=target.col_offset,
|
|
138
|
+
assigned_to=_get_assigned_to(item),
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
case _:
|
|
142
|
+
continue
|
|
143
|
+
|
|
144
|
+
if can_ignore_rule(
|
|
145
|
+
self._file_info.source_code_lines,
|
|
146
|
+
line=field_info.line - 1,
|
|
147
|
+
rule=self.rule,
|
|
148
|
+
):
|
|
149
|
+
continue
|
|
150
|
+
|
|
151
|
+
self._field_definitions[self._build_field_key(field_info.name)] = field_info
|
|
152
|
+
|
|
153
|
+
def _build_field_key(self, field_name: str) -> str:
|
|
154
|
+
return f"{self._class_name}_{field_name}"
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _get_assigned_to(attr: ast.Assign | ast.AnnAssign) -> str | None:
|
|
158
|
+
if not isinstance(attr.value, ast.Name):
|
|
159
|
+
return
|
|
160
|
+
|
|
161
|
+
return attr.value.id
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _find_root_node(node: ast.Attribute) -> ast.Attribute:
|
|
165
|
+
if isinstance(node.value, ast.Attribute):
|
|
166
|
+
return _find_root_node(node.value)
|
|
167
|
+
|
|
168
|
+
return node
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def check_useless_field(
|
|
172
|
+
info: TreeInfo,
|
|
173
|
+
*,
|
|
174
|
+
file_path: Path,
|
|
175
|
+
config: LintUtilsConfig | None = None,
|
|
176
|
+
) -> bool:
|
|
177
|
+
has_errors = []
|
|
178
|
+
for module in ast.walk(info.tree):
|
|
179
|
+
if isinstance(module, ast.Module):
|
|
180
|
+
for item in module.body:
|
|
181
|
+
if not isinstance(item, ast.ClassDef):
|
|
182
|
+
continue
|
|
183
|
+
|
|
184
|
+
visitor = UselessFieldVisitor(
|
|
185
|
+
file_info=FileInfoDTO(
|
|
186
|
+
source_code_lines=info.raw.split("\n"),
|
|
187
|
+
path=file_path,
|
|
188
|
+
),
|
|
189
|
+
config=config,
|
|
190
|
+
)
|
|
191
|
+
visitor.visit(item)
|
|
192
|
+
|
|
193
|
+
if visitor.useless_fields:
|
|
194
|
+
msg = f"{to_bold(to_cyan(visitor.rule))} Unused object class fields found in class {to_bold(visitor.class_name)}"
|
|
195
|
+
report_info(msg)
|
|
196
|
+
for field_info in visitor.useless_fields.values():
|
|
197
|
+
full_path = f"{file_path.as_posix()}:{field_info.line}:{field_info.col_offset + 1}"
|
|
198
|
+
line_msg = (
|
|
199
|
+
f"{full_path} {to_bold(to_red(f'self.{field_info.name}'))}"
|
|
200
|
+
)
|
|
201
|
+
report_info(line_msg)
|
|
202
|
+
report_info("")
|
|
203
|
+
|
|
204
|
+
has_errors.append(True)
|
|
205
|
+
|
|
206
|
+
has_errors.append(False)
|
|
207
|
+
|
|
208
|
+
return any(has_errors)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
dependencies = [
|
|
3
|
+
"click>=8.1.8",
|
|
4
|
+
"msgspec>=0.19.0",
|
|
5
|
+
"toml>=0.10.2",
|
|
6
|
+
]
|
|
7
|
+
description = ""
|
|
8
|
+
name = "lint-utils"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
version = "0.1.0"
|
|
12
|
+
|
|
13
|
+
[build-system]
|
|
14
|
+
build-backend = "hatchling.build"
|
|
15
|
+
requires = ["hatchling"]
|
|
16
|
+
|
|
17
|
+
[project.scripts]
|
|
18
|
+
lint-utils = "lint_utils.__main__:cli"
|
|
19
|
+
|
|
20
|
+
[tool.lint_utils]
|
|
21
|
+
exclude = []
|
|
22
|
+
lint.exclude_base_classes = []
|
|
23
|
+
lint.exclude_classes = []
|
|
24
|
+
lint.ignore = []
|
lint_utils-0.1.0/uv.lock
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
version = 1
|
|
2
|
+
revision = 2
|
|
3
|
+
requires-python = ">=3.11"
|
|
4
|
+
|
|
5
|
+
[[package]]
|
|
6
|
+
name = "click"
|
|
7
|
+
version = "8.1.8"
|
|
8
|
+
source = { registry = "https://pypi.org/simple" }
|
|
9
|
+
dependencies = [
|
|
10
|
+
{ name = "colorama", marker = "sys_platform == 'win32'" },
|
|
11
|
+
]
|
|
12
|
+
sdist = { url = "https://files.pythonhosted.org/packages/b9/2e/0090cbf739cee7d23781ad4b89a9894a41538e4fcf4c31dcdd705b78eb8b/click-8.1.8.tar.gz", hash = "sha256:ed53c9d8990d83c2a27deae68e4ee337473f6330c040a31d4225c9574d16096a", size = 226593, upload-time = "2024-12-21T18:38:44.339Z" }
|
|
13
|
+
wheels = [
|
|
14
|
+
{ url = "https://files.pythonhosted.org/packages/7e/d4/7ebdbd03970677812aac39c869717059dbb71a4cfc033ca6e5221787892c/click-8.1.8-py3-none-any.whl", hash = "sha256:63c132bbbed01578a06712a2d1f497bb62d9c1c0d329b7903a866228027263b2", size = 98188, upload-time = "2024-12-21T18:38:41.666Z" },
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[[package]]
|
|
18
|
+
name = "colorama"
|
|
19
|
+
version = "0.4.6"
|
|
20
|
+
source = { registry = "https://pypi.org/simple" }
|
|
21
|
+
sdist = { url = "https://files.pythonhosted.org/packages/d8/53/6f443c9a4a8358a93a6792e2acffb9d9d5cb0a5cfd8802644b7b1c9a02e4/colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44", size = 27697, upload-time = "2022-10-25T02:36:22.414Z" }
|
|
22
|
+
wheels = [
|
|
23
|
+
{ url = "https://files.pythonhosted.org/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", size = 25335, upload-time = "2022-10-25T02:36:20.889Z" },
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[[package]]
|
|
27
|
+
name = "lint-utils"
|
|
28
|
+
version = "0.1.0"
|
|
29
|
+
source = { editable = "." }
|
|
30
|
+
dependencies = [
|
|
31
|
+
{ name = "click" },
|
|
32
|
+
{ name = "msgspec" },
|
|
33
|
+
{ name = "toml" },
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
[package.metadata]
|
|
37
|
+
requires-dist = [
|
|
38
|
+
{ name = "click", specifier = ">=8.1.8" },
|
|
39
|
+
{ name = "msgspec", specifier = ">=0.19.0" },
|
|
40
|
+
{ name = "toml", specifier = ">=0.10.2" },
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
[[package]]
|
|
44
|
+
name = "msgspec"
|
|
45
|
+
version = "0.19.0"
|
|
46
|
+
source = { registry = "https://pypi.org/simple" }
|
|
47
|
+
sdist = { url = "https://files.pythonhosted.org/packages/cf/9b/95d8ce458462b8b71b8a70fa94563b2498b89933689f3a7b8911edfae3d7/msgspec-0.19.0.tar.gz", hash = "sha256:604037e7cd475345848116e89c553aa9a233259733ab51986ac924ab1b976f8e", size = 216934, upload-time = "2024-12-27T17:40:28.597Z" }
|
|
48
|
+
wheels = [
|
|
49
|
+
{ url = "https://files.pythonhosted.org/packages/24/d4/2ec2567ac30dab072cce3e91fb17803c52f0a37aab6b0c24375d2b20a581/msgspec-0.19.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:aa77046904db764b0462036bc63ef71f02b75b8f72e9c9dd4c447d6da1ed8f8e", size = 187939, upload-time = "2024-12-27T17:39:32.347Z" },
|
|
50
|
+
{ url = "https://files.pythonhosted.org/packages/2b/c0/18226e4328897f4f19875cb62bb9259fe47e901eade9d9376ab5f251a929/msgspec-0.19.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:047cfa8675eb3bad68722cfe95c60e7afabf84d1bd8938979dd2b92e9e4a9551", size = 182202, upload-time = "2024-12-27T17:39:33.633Z" },
|
|
51
|
+
{ url = "https://files.pythonhosted.org/packages/81/25/3a4b24d468203d8af90d1d351b77ea3cffb96b29492855cf83078f16bfe4/msgspec-0.19.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e78f46ff39a427e10b4a61614a2777ad69559cc8d603a7c05681f5a595ea98f7", size = 209029, upload-time = "2024-12-27T17:39:35.023Z" },
|
|
52
|
+
{ url = "https://files.pythonhosted.org/packages/85/2e/db7e189b57901955239f7689b5dcd6ae9458637a9c66747326726c650523/msgspec-0.19.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c7adf191e4bd3be0e9231c3b6dc20cf1199ada2af523885efc2ed218eafd011", size = 210682, upload-time = "2024-12-27T17:39:36.384Z" },
|
|
53
|
+
{ url = "https://files.pythonhosted.org/packages/03/97/7c8895c9074a97052d7e4a1cc1230b7b6e2ca2486714eb12c3f08bb9d284/msgspec-0.19.0-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:f04cad4385e20be7c7176bb8ae3dca54a08e9756cfc97bcdb4f18560c3042063", size = 214003, upload-time = "2024-12-27T17:39:39.097Z" },
|
|
54
|
+
{ url = "https://files.pythonhosted.org/packages/61/61/e892997bcaa289559b4d5869f066a8021b79f4bf8e955f831b095f47a4cd/msgspec-0.19.0-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:45c8fb410670b3b7eb884d44a75589377c341ec1392b778311acdbfa55187716", size = 216833, upload-time = "2024-12-27T17:39:41.203Z" },
|
|
55
|
+
{ url = "https://files.pythonhosted.org/packages/ce/3d/71b2dffd3a1c743ffe13296ff701ee503feaebc3f04d0e75613b6563c374/msgspec-0.19.0-cp311-cp311-win_amd64.whl", hash = "sha256:70eaef4934b87193a27d802534dc466778ad8d536e296ae2f9334e182ac27b6c", size = 186184, upload-time = "2024-12-27T17:39:43.702Z" },
|
|
56
|
+
{ url = "https://files.pythonhosted.org/packages/b2/5f/a70c24f075e3e7af2fae5414c7048b0e11389685b7f717bb55ba282a34a7/msgspec-0.19.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:f98bd8962ad549c27d63845b50af3f53ec468b6318400c9f1adfe8b092d7b62f", size = 190485, upload-time = "2024-12-27T17:39:44.974Z" },
|
|
57
|
+
{ url = "https://files.pythonhosted.org/packages/89/b0/1b9763938cfae12acf14b682fcf05c92855974d921a5a985ecc197d1c672/msgspec-0.19.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:43bbb237feab761b815ed9df43b266114203f53596f9b6e6f00ebd79d178cdf2", size = 183910, upload-time = "2024-12-27T17:39:46.401Z" },
|
|
58
|
+
{ url = "https://files.pythonhosted.org/packages/87/81/0c8c93f0b92c97e326b279795f9c5b956c5a97af28ca0fbb9fd86c83737a/msgspec-0.19.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4cfc033c02c3e0aec52b71710d7f84cb3ca5eb407ab2ad23d75631153fdb1f12", size = 210633, upload-time = "2024-12-27T17:39:49.099Z" },
|
|
59
|
+
{ url = "https://files.pythonhosted.org/packages/d0/ef/c5422ce8af73928d194a6606f8ae36e93a52fd5e8df5abd366903a5ca8da/msgspec-0.19.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d911c442571605e17658ca2b416fd8579c5050ac9adc5e00c2cb3126c97f73bc", size = 213594, upload-time = "2024-12-27T17:39:51.204Z" },
|
|
60
|
+
{ url = "https://files.pythonhosted.org/packages/19/2b/4137bc2ed45660444842d042be2cf5b18aa06efd2cda107cff18253b9653/msgspec-0.19.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:757b501fa57e24896cf40a831442b19a864f56d253679f34f260dcb002524a6c", size = 214053, upload-time = "2024-12-27T17:39:52.866Z" },
|
|
61
|
+
{ url = "https://files.pythonhosted.org/packages/9d/e6/8ad51bdc806aac1dc501e8fe43f759f9ed7284043d722b53323ea421c360/msgspec-0.19.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:5f0f65f29b45e2816d8bded36e6b837a4bf5fb60ec4bc3c625fa2c6da4124537", size = 219081, upload-time = "2024-12-27T17:39:55.142Z" },
|
|
62
|
+
{ url = "https://files.pythonhosted.org/packages/b1/ef/27dd35a7049c9a4f4211c6cd6a8c9db0a50647546f003a5867827ec45391/msgspec-0.19.0-cp312-cp312-win_amd64.whl", hash = "sha256:067f0de1c33cfa0b6a8206562efdf6be5985b988b53dd244a8e06f993f27c8c0", size = 187467, upload-time = "2024-12-27T17:39:56.531Z" },
|
|
63
|
+
{ url = "https://files.pythonhosted.org/packages/3c/cb/2842c312bbe618d8fefc8b9cedce37f773cdc8fa453306546dba2c21fd98/msgspec-0.19.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:f12d30dd6266557aaaf0aa0f9580a9a8fbeadfa83699c487713e355ec5f0bd86", size = 190498, upload-time = "2024-12-27T17:40:00.427Z" },
|
|
64
|
+
{ url = "https://files.pythonhosted.org/packages/58/95/c40b01b93465e1a5f3b6c7d91b10fb574818163740cc3acbe722d1e0e7e4/msgspec-0.19.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:82b2c42c1b9ebc89e822e7e13bbe9d17ede0c23c187469fdd9505afd5a481314", size = 183950, upload-time = "2024-12-27T17:40:04.219Z" },
|
|
65
|
+
{ url = "https://files.pythonhosted.org/packages/e8/f0/5b764e066ce9aba4b70d1db8b087ea66098c7c27d59b9dd8a3532774d48f/msgspec-0.19.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:19746b50be214a54239aab822964f2ac81e38b0055cca94808359d779338c10e", size = 210647, upload-time = "2024-12-27T17:40:05.606Z" },
|
|
66
|
+
{ url = "https://files.pythonhosted.org/packages/9d/87/bc14f49bc95c4cb0dd0a8c56028a67c014ee7e6818ccdce74a4862af259b/msgspec-0.19.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:60ef4bdb0ec8e4ad62e5a1f95230c08efb1f64f32e6e8dd2ced685bcc73858b5", size = 213563, upload-time = "2024-12-27T17:40:10.516Z" },
|
|
67
|
+
{ url = "https://files.pythonhosted.org/packages/53/2f/2b1c2b056894fbaa975f68f81e3014bb447516a8b010f1bed3fb0e016ed7/msgspec-0.19.0-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:ac7f7c377c122b649f7545810c6cd1b47586e3aa3059126ce3516ac7ccc6a6a9", size = 213996, upload-time = "2024-12-27T17:40:12.244Z" },
|
|
68
|
+
{ url = "https://files.pythonhosted.org/packages/aa/5a/4cd408d90d1417e8d2ce6a22b98a6853c1b4d7cb7669153e4424d60087f6/msgspec-0.19.0-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:a5bc1472223a643f5ffb5bf46ccdede7f9795078194f14edd69e3aab7020d327", size = 219087, upload-time = "2024-12-27T17:40:14.881Z" },
|
|
69
|
+
{ url = "https://files.pythonhosted.org/packages/23/d8/f15b40611c2d5753d1abb0ca0da0c75348daf1252220e5dda2867bd81062/msgspec-0.19.0-cp313-cp313-win_amd64.whl", hash = "sha256:317050bc0f7739cb30d257ff09152ca309bf5a369854bbf1e57dffc310c1f20f", size = 187432, upload-time = "2024-12-27T17:40:16.256Z" },
|
|
70
|
+
]
|
|
71
|
+
|
|
72
|
+
[[package]]
|
|
73
|
+
name = "toml"
|
|
74
|
+
version = "0.10.2"
|
|
75
|
+
source = { registry = "https://pypi.org/simple" }
|
|
76
|
+
sdist = { url = "https://files.pythonhosted.org/packages/be/ba/1f744cdc819428fc6b5084ec34d9b30660f6f9daaf70eead706e3203ec3c/toml-0.10.2.tar.gz", hash = "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f", size = 22253, upload-time = "2020-11-01T01:40:22.204Z" }
|
|
77
|
+
wheels = [
|
|
78
|
+
{ url = "https://files.pythonhosted.org/packages/44/6f/7120676b6d73228c96e17f1f794d8ab046fc910d781c8d151120c3f1569e/toml-0.10.2-py2.py3-none-any.whl", hash = "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b", size = 16588, upload-time = "2020-11-01T01:40:20.672Z" },
|
|
79
|
+
]
|