lapsed 2.5.0__tar.gz → 2.7.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.
Files changed (33) hide show
  1. {lapsed-2.5.0 → lapsed-2.7.0}/PKG-INFO +13 -2
  2. {lapsed-2.5.0 → lapsed-2.7.0}/README.md +12 -1
  3. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/actions/parse_arguments.py +11 -0
  4. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/data_types.py +1 -0
  5. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/visitor/dead_code_visitor.py +18 -0
  6. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/visitor/ignore.py +28 -1
  7. {lapsed-2.5.0 → lapsed-2.7.0}/pyproject.toml +1 -1
  8. {lapsed-2.5.0 → lapsed-2.7.0}/.gitignore +0 -0
  9. {lapsed-2.5.0 → lapsed-2.7.0}/LICENSE +0 -0
  10. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/__init__.py +0 -0
  11. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/actions/__init__.py +0 -0
  12. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/actions/find_python_filenames.py +0 -0
  13. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/actions/find_unused_names.py +0 -0
  14. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/actions/fix_or_show_unused_code.py +0 -0
  15. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/actions/get_unused_names_error_message.py +0 -0
  16. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/actions/merge_overlaping_file_parts.py +0 -0
  17. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/actions/parse_abstract_syntax_tree.py +0 -0
  18. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/actions/parse_tach_config.py +0 -0
  19. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/actions/remove_file_parts_from_content.py +0 -0
  20. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/cli.py +0 -0
  21. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/constants.py +0 -0
  22. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/utils/__init__.py +0 -0
  23. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/utils/add_colors_to_diff.py +0 -0
  24. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/utils/base_test_case.py +0 -0
  25. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/utils/fix_indent.py +0 -0
  26. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/utils/flatten_lists.py +0 -0
  27. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/utils/nested_scopes.py +0 -0
  28. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/utils/print_ast.py +0 -0
  29. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/visitor/__init__.py +0 -0
  30. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/visitor/code_item.py +0 -0
  31. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/visitor/lines.py +0 -0
  32. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/visitor/noqa.py +0 -0
  33. {lapsed-2.5.0 → lapsed-2.7.0}/deadcode/visitor/utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lapsed
3
- Version: 2.5.0
3
+ Version: 2.7.0
4
4
  Summary: Find and remove dead code.
5
5
  Project-URL: homepage, https://github.com/albertas/deadcode
6
6
  Project-URL: documentation, https://deadcode.readthedocs.io/
@@ -94,7 +94,7 @@ Create a `.pre-commit-config.yaml` file in the root of your project directory, i
94
94
  # See https://pre-commit.com/hooks.html for more hooks
95
95
  repos:
96
96
  - repo: https://github.com/albertas/deadcode
97
- rev: 2.5.0
97
+ rev: 2.7.0
98
98
  hooks:
99
99
  - id: deadcode
100
100
  ```
@@ -118,6 +118,7 @@ repos:
118
118
  |`--ignore-definitions-if-inherits-from` | list | Ignores definition (including name and body) of a class if it inherits from any of the provided class names. |
119
119
  |`--ignore-definitions-if-decorated-with` | list | Ignores definition (including name and body) of an expression, which is decorated with any of the provided decorator names. |
120
120
  |`--ignore-non-self-attributes` | - | Does not report unused attributes assigned on objects other than `self`, e.g. `foo.bar = 1`. Such assignments may have side effects or be consumed by code outside of the analysed files, so they cannot safely be assumed dead. |
121
+ |`--ignore-class-attributes` | - | Does not report unused attributes assigned directly in a class body, e.g. `THING = "blah"` inside a `class Foo:` block. Such attributes may control behaviour of inherited/overridden methods, external frameworks, or metaclasses, so they cannot safely be assumed dead. |
121
122
  |`--tach-config` | list | Paths to `tach.toml` files (see [Tach](https://docs.gauge.sh)). Names exposed via `[[interfaces]]` are treated as public API and never reported as unused; files in `unchecked = true` modules are skipped entirely. |
122
123
  |`--no-color` | - | Removes colors from the output. |
123
124
  |`--count` | - | Provides the count of the detected unused names instead of printing them all out. |
@@ -226,6 +227,16 @@ code base is implemented in.
226
227
  - [ ] Investigate ways of extracting and backporting Python3.10+ `ast` implementation to lower Python versions.
227
228
 
228
229
  ## Release notes
230
+ - v2.7.0:
231
+ - Treat `pytest_*` functions defined in `conftest.py` (e.g. `pytest_configure`,
232
+ `pytest_collection_modifyitems`, `pytest_addoption`) as pytest hooks, which pytest
233
+ calls automatically by name, and never report them as unused.
234
+ - Treat methods decorated with `@typing.override` (or `@typing_extensions.override`)
235
+ as used, since they are called via the base class they override, not by their own name.
236
+ - v2.6.0:
237
+ - Add `--ignore-class-attributes` option to not report unused attributes assigned directly
238
+ in a class body (e.g. `THING = "blah"` inside `class Foo:`), since such attributes may
239
+ control behaviour of inherited/overridden methods, external frameworks, or metaclasses.
229
240
  - v2.5.0:
230
241
  - Add `--ignore-non-self-attributes` option to not report unused attributes assigned on
231
242
  objects other than `self` (e.g. `foo.bar = 1`), since such assignments may have side
@@ -66,7 +66,7 @@ Create a `.pre-commit-config.yaml` file in the root of your project directory, i
66
66
  # See https://pre-commit.com/hooks.html for more hooks
67
67
  repos:
68
68
  - repo: https://github.com/albertas/deadcode
69
- rev: 2.5.0
69
+ rev: 2.7.0
70
70
  hooks:
71
71
  - id: deadcode
72
72
  ```
@@ -90,6 +90,7 @@ repos:
90
90
  |`--ignore-definitions-if-inherits-from` | list | Ignores definition (including name and body) of a class if it inherits from any of the provided class names. |
91
91
  |`--ignore-definitions-if-decorated-with` | list | Ignores definition (including name and body) of an expression, which is decorated with any of the provided decorator names. |
92
92
  |`--ignore-non-self-attributes` | - | Does not report unused attributes assigned on objects other than `self`, e.g. `foo.bar = 1`. Such assignments may have side effects or be consumed by code outside of the analysed files, so they cannot safely be assumed dead. |
93
+ |`--ignore-class-attributes` | - | Does not report unused attributes assigned directly in a class body, e.g. `THING = "blah"` inside a `class Foo:` block. Such attributes may control behaviour of inherited/overridden methods, external frameworks, or metaclasses, so they cannot safely be assumed dead. |
93
94
  |`--tach-config` | list | Paths to `tach.toml` files (see [Tach](https://docs.gauge.sh)). Names exposed via `[[interfaces]]` are treated as public API and never reported as unused; files in `unchecked = true` modules are skipped entirely. |
94
95
  |`--no-color` | - | Removes colors from the output. |
95
96
  |`--count` | - | Provides the count of the detected unused names instead of printing them all out. |
@@ -198,6 +199,16 @@ code base is implemented in.
198
199
  - [ ] Investigate ways of extracting and backporting Python3.10+ `ast` implementation to lower Python versions.
199
200
 
200
201
  ## Release notes
202
+ - v2.7.0:
203
+ - Treat `pytest_*` functions defined in `conftest.py` (e.g. `pytest_configure`,
204
+ `pytest_collection_modifyitems`, `pytest_addoption`) as pytest hooks, which pytest
205
+ calls automatically by name, and never report them as unused.
206
+ - Treat methods decorated with `@typing.override` (or `@typing_extensions.override`)
207
+ as used, since they are called via the base class they override, not by their own name.
208
+ - v2.6.0:
209
+ - Add `--ignore-class-attributes` option to not report unused attributes assigned directly
210
+ in a class body (e.g. `THING = "blah"` inside `class Foo:`), since such attributes may
211
+ control behaviour of inherited/overridden methods, external frameworks, or metaclasses.
201
212
  - v2.5.0:
202
213
  - Add `--ignore-non-self-attributes` option to not report unused attributes assigned on
203
214
  objects other than `self` (e.g. `foo.bar = 1`), since such assignments may have side
@@ -179,6 +179,17 @@ def parse_arguments(args: Optional[List[str]]) -> Args:
179
179
  default=False,
180
180
  )
181
181
 
182
+ parser.add_argument(
183
+ '--ignore-class-attributes',
184
+ help=(
185
+ 'Does not report unused attributes assigned directly in a class body, e.g. `THING = "blah"` '
186
+ 'inside a `class Foo:` block. Such attributes may control behaviour of inherited or overridden '
187
+ 'methods, external frameworks, or metaclasses, so they cannot safely be assumed dead.'
188
+ ),
189
+ action='store_true',
190
+ default=False,
191
+ )
192
+
182
193
  parser.add_argument(
183
194
  '--tach-config',
184
195
  help=(
@@ -32,6 +32,7 @@ class Args:
32
32
  ignore_names_if_inherits_from: Iterable[Pathname] = ()
33
33
  ignore_names_in_files: Iterable[Pathname] = ()
34
34
  ignore_non_self_attributes: bool = False
35
+ ignore_class_attributes: bool = False
35
36
  tach_config: Iterable[Pathname] = ()
36
37
  no_color: bool = False
37
38
  quiet: bool = False
@@ -33,6 +33,7 @@ from deadcode.visitor.ignore import (
33
33
  _ignore_import,
34
34
  _ignore_function,
35
35
  _ignore_method,
36
+ _ignore_override,
36
37
  _ignore_pytest_fixture,
37
38
  _ignore_variable,
38
39
  _is_self_attribute,
@@ -73,6 +74,11 @@ class DeadCodeVisitor(ast.NodeVisitor):
73
74
  # Note: scope is a stack containing current module name, class names, function names
74
75
  self.scope_parts: List[str] = []
75
76
 
77
+ # Parallel stack to scope_parts (module name excluded), tracking whether each
78
+ # pushed scope is a 'class' or a 'function', so that class-level attributes
79
+ # (assigned directly in a class body) can be told apart from local variables.
80
+ self.scope_kinds: List[str] = []
81
+
76
82
  # This flag is used to stop registering code definitions in a code item
77
83
  # during recursive its parsing
78
84
  self.should_ignore_new_definitions = False
@@ -101,6 +107,7 @@ class DeadCodeVisitor(ast.NodeVisitor):
101
107
  filename = os.path.basename(file_path)
102
108
  module_name = os.path.splitext(filename)[0]
103
109
  self.scope_parts = [module_name]
110
+ self.scope_kinds = []
104
111
 
105
112
  file_content = f.read()
106
113
  if file_content.strip() or (filename.startswith('__') and filename.endswith('__.py')):
@@ -285,7 +292,13 @@ class DeadCodeVisitor(ast.NodeVisitor):
285
292
  else:
286
293
  collection.append(code_item)
287
294
 
295
+ def _is_directly_in_class_body(self) -> bool:
296
+ return bool(self.scope_kinds) and self.scope_kinds[-1] == 'class'
297
+
288
298
  def _define_variable(self, name: str, node: ast.AST) -> None:
299
+ if self.args.ignore_class_attributes and self._is_directly_in_class_body():
300
+ return
301
+
289
302
  self._define(
290
303
  self.defined_vars,
291
304
  name,
@@ -430,6 +443,8 @@ class DeadCodeVisitor(ast.NodeVisitor):
430
443
  self._log(f'Ignoring {type_} "{node.name}" (decorator whitelisted)')
431
444
  elif _ignore_pytest_fixture(self.filename, decorator_names):
432
445
  self._log(f'Ignoring {type_} "{node.name}" (pytest fixture)')
446
+ elif _ignore_override(decorator_names):
447
+ self._log(f'Ignoring {type_} "{node.name}" (typing.override)')
433
448
  elif type_ == 'property':
434
449
  self._define(self.defined_props, node.name, node)
435
450
  elif type_ == 'method':
@@ -505,8 +520,10 @@ class DeadCodeVisitor(ast.NodeVisitor):
505
520
  was_scope_increased = True
506
521
  if isinstance(node, ast.ClassDef):
507
522
  self.scope_parts.append(node.name)
523
+ self.scope_kinds.append('class')
508
524
  elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
509
525
  self.scope_parts.append(node.name)
526
+ self.scope_kinds.append('function')
510
527
  else:
511
528
  was_scope_increased = False
512
529
 
@@ -544,6 +561,7 @@ class DeadCodeVisitor(ast.NodeVisitor):
544
561
  # TODO: use decorator for this code chunk
545
562
  if was_scope_increased:
546
563
  self.scope_parts.pop()
564
+ self.scope_kinds.pop()
547
565
 
548
566
  def _handle_ast_list(self, ast_list: List[ast.AST]) -> None:
549
567
  """
@@ -29,6 +29,11 @@ PYTEST_USEFIXTURES_DECORATOR_NAMES = {
29
29
  '@mark.usefixtures',
30
30
  '@usefixtures',
31
31
  }
32
+ OVERRIDE_DECORATOR_NAMES = {
33
+ '@typing.override',
34
+ '@typing_extensions.override',
35
+ '@override',
36
+ }
32
37
 
33
38
  ERROR_CODES = {
34
39
  'variable': b'DC01',
@@ -98,7 +103,19 @@ def _ignore_import(filename: Path, import_name: str) -> bool:
98
103
 
99
104
 
100
105
  def _ignore_function(filename: Path, function_name: str) -> bool:
101
- return (function_name in PYTEST_FUNCTION_NAMES or function_name.startswith('test_')) and _is_test_file(filename)
106
+ return (
107
+ (function_name in PYTEST_FUNCTION_NAMES or function_name.startswith('test_')) and _is_test_file(filename)
108
+ ) or _ignore_pytest_hook(filename, function_name)
109
+
110
+
111
+ def _ignore_pytest_hook(filename: Path, function_name: str) -> bool:
112
+ """
113
+ `pytest_*` functions in conftest.py (e.g. `pytest_configure`,
114
+ `pytest_collection_modifyitems`, `pytest_addoption`) are hook
115
+ implementations that pytest calls automatically by name, never directly,
116
+ so they would otherwise look unused.
117
+ """
118
+ return _is_conftest_file(filename) and function_name.startswith('pytest_')
102
119
 
103
120
 
104
121
  def _ignore_pytest_fixture(filename: Path, decorator_names: Iterable[str]) -> bool:
@@ -117,6 +134,16 @@ def _ignore_pytest_fixture(filename: Path, decorator_names: Iterable[str]) -> bo
117
134
  )
118
135
 
119
136
 
137
+ def _ignore_override(decorator_names: Iterable[str]) -> bool:
138
+ """
139
+ `@typing.override` (and `@typing_extensions.override`) marks a method as
140
+ overriding one declared on a base class. The base class's method is what
141
+ calling code actually calls, so this override is used even though nothing
142
+ calls it by its own name directly.
143
+ """
144
+ return _match_many(decorator_names, OVERRIDE_DECORATOR_NAMES)
145
+
146
+
120
147
  def _ignore_method(filename: Path, method_name: str) -> bool:
121
148
  return _is_special_name(method_name) or (
122
149
  (method_name in PYTEST_METHOD_NAMES or method_name.startswith('test_')) and _is_test_file(filename)
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "lapsed"
3
- version = "2.5.0"
3
+ version = "2.7.0"
4
4
  authors = [
5
5
  {name = "Albertas Gimbutas", email = "albertasgim@gmail.com"},
6
6
  ]
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes