code-prune 1.4.0__tar.gz → 1.6.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,23 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.6.0 - 2026-11-29
4
+
5
+ ### Changed
6
+
7
+ - `unused-symbol` no longer reports Gosu declarations (classes, functions,
8
+ fields) annotated with `@Export`.
9
+
10
+ ## 1.5.0 - 2026-11-29
11
+
12
+ ### Changed
13
+
14
+ - `unused-symbol` no longer reports overriding members: Java `@Override`, Gosu
15
+ `override` / `@Override`, TypeScript `override`, and Python `@override` /
16
+ `@typing.override` (including stacked decorators such as `@property`).
17
+ - `unused-symbol` no longer reports Java and Gosu declarations annotated with
18
+ `@SuppressWarnings("unused")` or `@SuppressWarnings("all")` (string, array,
19
+ or `value =` forms). An annotated class exempts all of its members.
20
+
3
21
  ## 1.4.0 - 2026-11-22
4
22
 
5
23
  ### Changed
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: code-prune
3
- Version: 1.4.0
3
+ Version: 1.6.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.6.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,13 @@ 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, as are
176
+ Gosu declarations annotated with `@Export`.
177
+
171
178
  ## Development
172
179
 
173
180
  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.6.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,13 @@ 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, as are
120
+ Gosu declarations annotated with `@Export`.
121
+
115
122
  ## Development
116
123
 
117
124
  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.6.0"
14
14
 
15
15
  _bootstrapper = EnvDirBootstrap(
16
16
  env_var="CODE_PRUNE_CONFIG_DIR",
@@ -145,6 +145,33 @@ 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
+ #: Annotation names marking a declaration as exported (used externally), per language.
167
+ EXPORT_ANNOTATION_NAMES: dict[str, frozenset[str]] = {"Gosu": frozenset({"Export"})}
168
+
169
+ #: Modifier node types marking a declaration as an override, per language.
170
+ OVERRIDE_MODIFIER_TYPES: dict[str, frozenset[str]] = {
171
+ "TypeScript": frozenset({"override_modifier"}),
172
+ "Gosu": frozenset({"override"}),
173
+ }
174
+
148
175
  #: Identifier-like node types counted as a "usage" of a name.
149
176
  USAGE_NODE_TYPES: dict[str, frozenset[str]] = {
150
177
  "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,208 @@
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"``, or when a Gosu declaration is annotated ``@Export``.
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
+ EXPORT_ANNOTATION_NAMES,
19
+ OVERRIDE_ANNOTATION_NAMES,
20
+ OVERRIDE_MODIFIER_TYPES,
21
+ SUPPRESS_WARNINGS_NAME,
22
+ SUPPRESS_WARNINGS_VALUES,
23
+ SUPPRESSIBLE_LANGUAGES,
24
+ )
25
+
26
+ _STRING_NODE_TYPE = "string_literal"
27
+ _MODIFIERS_NODE_TYPE = "modifiers"
28
+ _DECORATED_NODE_TYPE = "decorated_definition"
29
+ _DECORATOR_NODE_TYPE = "decorator"
30
+ _PYTHON_OVERRIDE_NAME = "override"
31
+
32
+
33
+ def _decode(source_bytes: bytes, node: Node) -> str:
34
+ """Return the source text covered by *node*.
35
+
36
+ Author: Ron Webb
37
+ Since: 1.5.0
38
+ """
39
+ return source_bytes[node.start_byte : node.end_byte].decode(
40
+ "utf-8", errors="replace"
41
+ )
42
+
43
+
44
+ def _simple_name(text: str) -> str:
45
+ """Return the last dotted segment of *text* (``a.b.C`` -> ``C``).
46
+
47
+ Author: Ron Webb
48
+ Since: 1.5.0
49
+ """
50
+ return text.strip().rsplit(".", maxsplit=1)[-1]
51
+
52
+
53
+ def _modifier_nodes(node: Node, language: str) -> list[Node]:
54
+ """Return the ``modifiers`` nodes attached to the declaration *node*.
55
+
56
+ Java holds ``modifiers`` as a child; Gosu places it as the preceding
57
+ sibling of the declaration (so a Gosu node's own children are never its
58
+ modifiers, e.g. a ``class_body`` holds its members' modifiers).
59
+
60
+ Author: Ron Webb
61
+ Since: 1.5.0
62
+ """
63
+ if language == "Java":
64
+ return [child for child in node.children if child.type == _MODIFIERS_NODE_TYPE]
65
+ previous = node.prev_named_sibling
66
+ if previous is not None and previous.type == _MODIFIERS_NODE_TYPE:
67
+ return [previous]
68
+ return []
69
+
70
+
71
+ def _annotations(node: Node, language: str) -> list[Node]:
72
+ """Return the annotation nodes applied to the declaration *node*.
73
+
74
+ Author: Ron Webb
75
+ Since: 1.5.0
76
+ """
77
+ annotation_types = ANNOTATION_NODE_TYPES.get(language, frozenset())
78
+ return [
79
+ child
80
+ for modifiers in _modifier_nodes(node, language)
81
+ for child in modifiers.children
82
+ if child.type in annotation_types
83
+ ]
84
+
85
+
86
+ def _annotation_name(annotation: Node, source_bytes: bytes) -> str:
87
+ """Return the simple name of *annotation* (``@a.B(...)`` -> ``B``).
88
+
89
+ Author: Ron Webb
90
+ Since: 1.5.0
91
+ """
92
+ for child in annotation.named_children:
93
+ if child.type in {"identifier", "qualified_name", "scoped_identifier"}:
94
+ return _simple_name(_decode(source_bytes, child))
95
+ return ""
96
+
97
+
98
+ def _string_arguments(annotation: Node, source_bytes: bytes) -> set[str]:
99
+ """Return the lower-cased string literal values passed to *annotation*.
100
+
101
+ Author: Ron Webb
102
+ Since: 1.5.0
103
+ """
104
+ values: set[str] = set()
105
+ pending = list(annotation.children)
106
+ while pending:
107
+ node = pending.pop()
108
+ if node.type == _STRING_NODE_TYPE:
109
+ values.add(_decode(source_bytes, node).strip("\"'").lower())
110
+ else:
111
+ pending.extend(node.children)
112
+ return values
113
+
114
+
115
+ def _is_suppressed_here(node: Node, source_bytes: bytes, language: str) -> bool:
116
+ """Return whether *node* itself carries a matching ``@SuppressWarnings``.
117
+
118
+ Author: Ron Webb
119
+ Since: 1.5.0
120
+ """
121
+ return any(
122
+ _annotation_name(annotation, source_bytes) == SUPPRESS_WARNINGS_NAME
123
+ and _string_arguments(annotation, source_bytes) & SUPPRESS_WARNINGS_VALUES
124
+ for annotation in _annotations(node, language)
125
+ )
126
+
127
+
128
+ def _is_suppressed(node: Node, source_bytes: bytes, language: str) -> bool:
129
+ """Return whether *node* or any enclosing declaration suppresses ``unused``.
130
+
131
+ Author: Ron Webb
132
+ Since: 1.5.0
133
+ """
134
+ if language not in SUPPRESSIBLE_LANGUAGES:
135
+ return False
136
+ current: Node | None = node
137
+ while current is not None:
138
+ if _is_suppressed_here(current, source_bytes, language):
139
+ return True
140
+ current = current.parent
141
+ return False
142
+
143
+
144
+ def _python_overrides(node: Node, source_bytes: bytes) -> bool:
145
+ """Return whether the Python definition *node* is decorated ``@override``.
146
+
147
+ Author: Ron Webb
148
+ Since: 1.5.0
149
+ """
150
+ parent = node.parent
151
+ if parent is None or parent.type != _DECORATED_NODE_TYPE:
152
+ return False
153
+ return any(
154
+ _simple_name(_decode(source_bytes, child).lstrip("@")) == _PYTHON_OVERRIDE_NAME
155
+ for child in parent.children
156
+ if child.type == _DECORATOR_NODE_TYPE
157
+ )
158
+
159
+
160
+ def _is_override(node: Node, source_bytes: bytes, language: str) -> bool:
161
+ """Return whether *node* is explicitly marked as overriding a member.
162
+
163
+ Author: Ron Webb
164
+ Since: 1.5.0
165
+ """
166
+ if language == "Python":
167
+ return _python_overrides(node, source_bytes)
168
+ modifier_types = OVERRIDE_MODIFIER_TYPES.get(language, frozenset())
169
+ if any(child.type in modifier_types for child in node.children):
170
+ return True
171
+ if language not in SUPPRESSIBLE_LANGUAGES:
172
+ return False
173
+ if any(
174
+ child.type in modifier_types
175
+ for modifiers in _modifier_nodes(node, language)
176
+ for child in modifiers.children
177
+ ):
178
+ return True
179
+ return any(
180
+ _annotation_name(annotation, source_bytes) in OVERRIDE_ANNOTATION_NAMES
181
+ for annotation in _annotations(node, language)
182
+ )
183
+
184
+
185
+ def _is_exported(node: Node, source_bytes: bytes, language: str) -> bool:
186
+ """Return whether *node* carries an ``@Export``-style annotation.
187
+
188
+ Author: Ron Webb
189
+ Since: 1.6.0
190
+ """
191
+ export_names = EXPORT_ANNOTATION_NAMES.get(language, frozenset())
192
+ return any(
193
+ _annotation_name(annotation, source_bytes) in export_names
194
+ for annotation in _annotations(node, language)
195
+ )
196
+
197
+
198
+ def is_exempt_symbol(node: Node, source_bytes: bytes, language: str) -> bool:
199
+ """Return whether the declaration *node* must not be an unused-symbol finding.
200
+
201
+ Author: Ron Webb
202
+ Since: 1.5.0
203
+ """
204
+ return (
205
+ _is_override(node, source_bytes, language)
206
+ or _is_exported(node, source_bytes, language)
207
+ or _is_suppressed(node, source_bytes, language)
208
+ )
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "code-prune"
3
- version = "1.4.0"
3
+ version = "1.6.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