code-prune 1.4.0__tar.gz → 1.5.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.
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.5.0 - 2026-11-29
4
+
5
+ ### Changed
6
+
7
+ - `unused-symbol` no longer reports overriding members: Java `@Override`, Gosu
8
+ `override` / `@Override`, TypeScript `override`, and Python `@override` /
9
+ `@typing.override` (including stacked decorators such as `@property`).
10
+ - `unused-symbol` no longer reports Java and Gosu declarations annotated with
11
+ `@SuppressWarnings("unused")` or `@SuppressWarnings("all")` (string, array,
12
+ or `value =` forms). An annotated class exempts all of its members.
13
+
3
14
  ## 1.4.0 - 2026-11-22
4
15
 
5
16
  ### Changed
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: code-prune
3
- Version: 1.4.0
3
+ Version: 1.5.0
4
4
  Summary: Find unreachable, unused, and obsolete code before it becomes technical debt.
5
5
  License: MIT License
6
6
 
@@ -56,7 +56,7 @@ Description-Content-Type: text/markdown
56
56
 
57
57
  # code-prune
58
58
 
59
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/rcw3bb/code-prune/blob/main/LICENSE) [![Version](https://img.shields.io/badge/Version-1.4.0-green.svg)](https://github.com/rcw3bb/code-prune/blob/main/CHANGELOG.md) [![Python](https://img.shields.io/badge/Python-3.14%2B-blue)](https://www.python.org/) [![PyPI](https://img.shields.io/badge/PyPI-code--prune-orange)](https://pypi.org/project/code-prune/)
59
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/rcw3bb/code-prune/blob/main/LICENSE) [![Version](https://img.shields.io/badge/Version-1.5.0-green.svg)](https://github.com/rcw3bb/code-prune/blob/main/CHANGELOG.md) [![Python](https://img.shields.io/badge/Python-3.14%2B-blue)](https://www.python.org/) [![PyPI](https://img.shields.io/badge/PyPI-code--prune-orange)](https://pypi.org/project/code-prune/)
60
60
 
61
61
 
62
62
  A CLI tool that scans a codebase for unreachable, unused, and obsolete code,
@@ -168,6 +168,12 @@ names to suppress from "unused function/class/method/field" findings — useful
168
168
  dunder methods, `main`, test hooks (`test_*`, `setUp`), `serialVersionUID`, and similar
169
169
  intentionally-unreferenced symbols.
170
170
 
171
+ Overriding members are also never reported as unused symbols: Java `@Override`,
172
+ Gosu `override` / `@Override`, TypeScript `override`, and Python `@override` /
173
+ `@typing.override`. In Java and Gosu, declarations annotated with
174
+ `@SuppressWarnings("unused")` or `@SuppressWarnings("all")` (including a class
175
+ annotated that way, which covers all of its members) are skipped as well.
176
+
171
177
  ## Development
172
178
 
173
179
  Development uses [Poetry](https://python-poetry.org/) `2.2`.
@@ -1,6 +1,6 @@
1
1
  # code-prune
2
2
 
3
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/rcw3bb/code-prune/blob/main/LICENSE) [![Version](https://img.shields.io/badge/Version-1.4.0-green.svg)](https://github.com/rcw3bb/code-prune/blob/main/CHANGELOG.md) [![Python](https://img.shields.io/badge/Python-3.14%2B-blue)](https://www.python.org/) [![PyPI](https://img.shields.io/badge/PyPI-code--prune-orange)](https://pypi.org/project/code-prune/)
3
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/rcw3bb/code-prune/blob/main/LICENSE) [![Version](https://img.shields.io/badge/Version-1.5.0-green.svg)](https://github.com/rcw3bb/code-prune/blob/main/CHANGELOG.md) [![Python](https://img.shields.io/badge/Python-3.14%2B-blue)](https://www.python.org/) [![PyPI](https://img.shields.io/badge/PyPI-code--prune-orange)](https://pypi.org/project/code-prune/)
4
4
 
5
5
 
6
6
  A CLI tool that scans a codebase for unreachable, unused, and obsolete code,
@@ -112,6 +112,12 @@ names to suppress from "unused function/class/method/field" findings — useful
112
112
  dunder methods, `main`, test hooks (`test_*`, `setUp`), `serialVersionUID`, and similar
113
113
  intentionally-unreferenced symbols.
114
114
 
115
+ Overriding members are also never reported as unused symbols: Java `@Override`,
116
+ Gosu `override` / `@Override`, TypeScript `override`, and Python `@override` /
117
+ `@typing.override`. In Java and Gosu, declarations annotated with
118
+ `@SuppressWarnings("unused")` or `@SuppressWarnings("all")` (including a class
119
+ annotated that way, which covers all of its members) are skipped as well.
120
+
115
121
  ## Development
116
122
 
117
123
  Development uses [Poetry](https://python-poetry.org/) `2.2`.
@@ -10,7 +10,7 @@ Since: 1.0.0
10
10
  from env_dir_bootstrap import EnvDirBootstrap
11
11
 
12
12
  __app_name__ = "code-prune"
13
- __version__ = "1.4.0"
13
+ __version__ = "1.5.0"
14
14
 
15
15
  _bootstrapper = EnvDirBootstrap(
16
16
  env_var="CODE_PRUNE_CONFIG_DIR",
@@ -145,6 +145,30 @@ DECORATOR_NODE_TYPES: dict[str, frozenset[str]] = {
145
145
  "Gosu": frozenset({"annotation"}),
146
146
  }
147
147
 
148
+ #: Languages whose declarations may carry ``@SuppressWarnings`` annotations.
149
+ SUPPRESSIBLE_LANGUAGES = frozenset({"Java", "Gosu"})
150
+
151
+ #: Annotation node types found inside a ``modifiers`` node, per language.
152
+ ANNOTATION_NODE_TYPES: dict[str, frozenset[str]] = {
153
+ "Java": frozenset({"marker_annotation", "annotation"}),
154
+ "Gosu": frozenset({"annotation"}),
155
+ }
156
+
157
+ #: Name of the annotation that suppresses warnings.
158
+ SUPPRESS_WARNINGS_NAME = "SuppressWarnings"
159
+
160
+ #: ``@SuppressWarnings`` values (lower-case) that suppress unused-symbol findings.
161
+ SUPPRESS_WARNINGS_VALUES = frozenset({"unused", "all"})
162
+
163
+ #: Annotation names marking a method as an override.
164
+ OVERRIDE_ANNOTATION_NAMES = frozenset({"Override"})
165
+
166
+ #: Modifier node types marking a declaration as an override, per language.
167
+ OVERRIDE_MODIFIER_TYPES: dict[str, frozenset[str]] = {
168
+ "TypeScript": frozenset({"override_modifier"}),
169
+ "Gosu": frozenset({"override"}),
170
+ }
171
+
148
172
  #: Identifier-like node types counted as a "usage" of a name.
149
173
  USAGE_NODE_TYPES: dict[str, frozenset[str]] = {
150
174
  "Python": frozenset({"identifier"}),
@@ -37,6 +37,7 @@ from .language_tables import (
37
37
  USAGE_NODE_TYPES,
38
38
  )
39
39
  from .parser import parse
40
+ from .suppression import is_exempt_symbol
40
41
 
41
42
  _logger = logging.getLogger(__name__)
42
43
 
@@ -546,16 +547,17 @@ class _BindingCollector:
546
547
  if name_node is not None:
547
548
  decoded_name = _decode(self._source, name_node)
548
549
  class_name = decoded_name if is_class else None
549
- self.bindings.definitions.append(
550
- Definition(
551
- name=decoded_name,
552
- kind=SYMBOL_KIND_CLASS if is_class else SYMBOL_KIND_FUNCTION,
553
- file_path=self._file_path,
554
- line=name_node.start_point[0] + 1,
555
- column=name_node.start_point[1],
556
- enclosing_class=None if is_class else scope.enclosing_class,
550
+ if not is_exempt_symbol(node, self._source, self._language):
551
+ self.bindings.definitions.append(
552
+ Definition(
553
+ name=decoded_name,
554
+ kind=SYMBOL_KIND_CLASS if is_class else SYMBOL_KIND_FUNCTION,
555
+ file_path=self._file_path,
556
+ line=name_node.start_point[0] + 1,
557
+ column=name_node.start_point[1],
558
+ enclosing_class=None if is_class else scope.enclosing_class,
559
+ )
557
560
  )
558
- )
559
561
  self.bindings.excluded_positions.add(name_node.start_byte)
560
562
  return _Scope(
561
563
  inside_function=scope.inside_function or not is_class,
@@ -573,7 +575,9 @@ class _BindingCollector:
573
575
  if scope.enclosing_class is not None:
574
576
  if node.type in FIELD_NODE_TYPES.get(language, frozenset()):
575
577
  self._add_fields(
576
- _field_name_nodes(node, language), scope.enclosing_class
578
+ _field_name_nodes(node, language),
579
+ scope.enclosing_class,
580
+ exempt=is_exempt_symbol(node, self._source, language),
577
581
  )
578
582
  elif language == "TypeScript" and node.type in FUNCTION_NODE_TYPES.get(
579
583
  language, frozenset()
@@ -591,11 +595,14 @@ class _BindingCollector:
591
595
  if name_node is not None:
592
596
  self._add_fields([name_node], scope.owner_class)
593
597
 
594
- def _add_fields(self, name_nodes: list[Node], class_name: str) -> None:
598
+ def _add_fields(
599
+ self, name_nodes: list[Node], class_name: str, exempt: bool = False
600
+ ) -> None:
595
601
  """Record each of *name_nodes* as a field of *class_name*.
596
602
 
597
603
  Every declaration position is excluded from usages; only the first
598
- declaration of a field yields a :class:`Definition`.
604
+ declaration of a field yields a :class:`Definition`, and *exempt*
605
+ declarations (overrides, ``@SuppressWarnings``) yield none.
599
606
 
600
607
  Author: Ron Webb
601
608
  Since: 1.3.0
@@ -603,7 +610,7 @@ class _BindingCollector:
603
610
  for name_node in name_nodes:
604
611
  self.bindings.excluded_positions.add(name_node.start_byte)
605
612
  decoded_name = _decode(self._source, name_node)
606
- if (class_name, decoded_name) in self._seen_fields:
613
+ if exempt or (class_name, decoded_name) in self._seen_fields:
607
614
  continue
608
615
  self._seen_fields.add((class_name, decoded_name))
609
616
  self.bindings.definitions.append(
@@ -0,0 +1,192 @@
1
+ """Detection of declarations exempt from ``unused-symbol`` findings.
2
+
3
+ A declaration is exempt when it overrides an inherited member (it is invoked
4
+ through its base type, so it never has a textual usage) or, for Java and Gosu,
5
+ when it (or an enclosing class) carries ``@SuppressWarnings`` with ``"unused"``
6
+ or ``"all"``.
7
+
8
+ Author: Ron Webb
9
+ Since: 1.5.0
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ from tree_sitter import Node # type: ignore[import-untyped]
15
+
16
+ from .language_tables import (
17
+ ANNOTATION_NODE_TYPES,
18
+ OVERRIDE_ANNOTATION_NAMES,
19
+ OVERRIDE_MODIFIER_TYPES,
20
+ SUPPRESS_WARNINGS_NAME,
21
+ SUPPRESS_WARNINGS_VALUES,
22
+ SUPPRESSIBLE_LANGUAGES,
23
+ )
24
+
25
+ _STRING_NODE_TYPE = "string_literal"
26
+ _MODIFIERS_NODE_TYPE = "modifiers"
27
+ _DECORATED_NODE_TYPE = "decorated_definition"
28
+ _DECORATOR_NODE_TYPE = "decorator"
29
+ _PYTHON_OVERRIDE_NAME = "override"
30
+
31
+
32
+ def _decode(source_bytes: bytes, node: Node) -> str:
33
+ """Return the source text covered by *node*.
34
+
35
+ Author: Ron Webb
36
+ Since: 1.5.0
37
+ """
38
+ return source_bytes[node.start_byte : node.end_byte].decode(
39
+ "utf-8", errors="replace"
40
+ )
41
+
42
+
43
+ def _simple_name(text: str) -> str:
44
+ """Return the last dotted segment of *text* (``a.b.C`` -> ``C``).
45
+
46
+ Author: Ron Webb
47
+ Since: 1.5.0
48
+ """
49
+ return text.strip().rsplit(".", maxsplit=1)[-1]
50
+
51
+
52
+ def _modifier_nodes(node: Node, language: str) -> list[Node]:
53
+ """Return the ``modifiers`` nodes attached to the declaration *node*.
54
+
55
+ Java holds ``modifiers`` as a child; Gosu places it as the preceding
56
+ sibling of the declaration (so a Gosu node's own children are never its
57
+ modifiers, e.g. a ``class_body`` holds its members' modifiers).
58
+
59
+ Author: Ron Webb
60
+ Since: 1.5.0
61
+ """
62
+ if language == "Java":
63
+ return [child for child in node.children if child.type == _MODIFIERS_NODE_TYPE]
64
+ previous = node.prev_named_sibling
65
+ if previous is not None and previous.type == _MODIFIERS_NODE_TYPE:
66
+ return [previous]
67
+ return []
68
+
69
+
70
+ def _annotations(node: Node, language: str) -> list[Node]:
71
+ """Return the annotation nodes applied to the declaration *node*.
72
+
73
+ Author: Ron Webb
74
+ Since: 1.5.0
75
+ """
76
+ annotation_types = ANNOTATION_NODE_TYPES.get(language, frozenset())
77
+ return [
78
+ child
79
+ for modifiers in _modifier_nodes(node, language)
80
+ for child in modifiers.children
81
+ if child.type in annotation_types
82
+ ]
83
+
84
+
85
+ def _annotation_name(annotation: Node, source_bytes: bytes) -> str:
86
+ """Return the simple name of *annotation* (``@a.B(...)`` -> ``B``).
87
+
88
+ Author: Ron Webb
89
+ Since: 1.5.0
90
+ """
91
+ for child in annotation.named_children:
92
+ if child.type in {"identifier", "qualified_name", "scoped_identifier"}:
93
+ return _simple_name(_decode(source_bytes, child))
94
+ return ""
95
+
96
+
97
+ def _string_arguments(annotation: Node, source_bytes: bytes) -> set[str]:
98
+ """Return the lower-cased string literal values passed to *annotation*.
99
+
100
+ Author: Ron Webb
101
+ Since: 1.5.0
102
+ """
103
+ values: set[str] = set()
104
+ pending = list(annotation.children)
105
+ while pending:
106
+ node = pending.pop()
107
+ if node.type == _STRING_NODE_TYPE:
108
+ values.add(_decode(source_bytes, node).strip("\"'").lower())
109
+ else:
110
+ pending.extend(node.children)
111
+ return values
112
+
113
+
114
+ def _is_suppressed_here(node: Node, source_bytes: bytes, language: str) -> bool:
115
+ """Return whether *node* itself carries a matching ``@SuppressWarnings``.
116
+
117
+ Author: Ron Webb
118
+ Since: 1.5.0
119
+ """
120
+ return any(
121
+ _annotation_name(annotation, source_bytes) == SUPPRESS_WARNINGS_NAME
122
+ and _string_arguments(annotation, source_bytes) & SUPPRESS_WARNINGS_VALUES
123
+ for annotation in _annotations(node, language)
124
+ )
125
+
126
+
127
+ def _is_suppressed(node: Node, source_bytes: bytes, language: str) -> bool:
128
+ """Return whether *node* or any enclosing declaration suppresses ``unused``.
129
+
130
+ Author: Ron Webb
131
+ Since: 1.5.0
132
+ """
133
+ if language not in SUPPRESSIBLE_LANGUAGES:
134
+ return False
135
+ current: Node | None = node
136
+ while current is not None:
137
+ if _is_suppressed_here(current, source_bytes, language):
138
+ return True
139
+ current = current.parent
140
+ return False
141
+
142
+
143
+ def _python_overrides(node: Node, source_bytes: bytes) -> bool:
144
+ """Return whether the Python definition *node* is decorated ``@override``.
145
+
146
+ Author: Ron Webb
147
+ Since: 1.5.0
148
+ """
149
+ parent = node.parent
150
+ if parent is None or parent.type != _DECORATED_NODE_TYPE:
151
+ return False
152
+ return any(
153
+ _simple_name(_decode(source_bytes, child).lstrip("@")) == _PYTHON_OVERRIDE_NAME
154
+ for child in parent.children
155
+ if child.type == _DECORATOR_NODE_TYPE
156
+ )
157
+
158
+
159
+ def _is_override(node: Node, source_bytes: bytes, language: str) -> bool:
160
+ """Return whether *node* is explicitly marked as overriding a member.
161
+
162
+ Author: Ron Webb
163
+ Since: 1.5.0
164
+ """
165
+ if language == "Python":
166
+ return _python_overrides(node, source_bytes)
167
+ modifier_types = OVERRIDE_MODIFIER_TYPES.get(language, frozenset())
168
+ if any(child.type in modifier_types for child in node.children):
169
+ return True
170
+ if language not in SUPPRESSIBLE_LANGUAGES:
171
+ return False
172
+ if any(
173
+ child.type in modifier_types
174
+ for modifiers in _modifier_nodes(node, language)
175
+ for child in modifiers.children
176
+ ):
177
+ return True
178
+ return any(
179
+ _annotation_name(annotation, source_bytes) in OVERRIDE_ANNOTATION_NAMES
180
+ for annotation in _annotations(node, language)
181
+ )
182
+
183
+
184
+ def is_exempt_symbol(node: Node, source_bytes: bytes, language: str) -> bool:
185
+ """Return whether the declaration *node* must not be an unused-symbol finding.
186
+
187
+ Author: Ron Webb
188
+ Since: 1.5.0
189
+ """
190
+ return _is_override(node, source_bytes, language) or _is_suppressed(
191
+ node, source_bytes, language
192
+ )
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "code-prune"
3
- version = "1.4.0"
3
+ version = "1.5.0"
4
4
  description = "Find unreachable, unused, and obsolete code before it becomes technical debt."
5
5
  authors = [
6
6
  {name = "Ron Webb"}
File without changes
File without changes