wexample-filestate-python 9.4.1__tar.gz → 10.0.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 (99) hide show
  1. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/PKG-INFO +3 -4
  2. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/README.md +1 -1
  3. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/pyproject.toml +2 -3
  4. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/format_option.py +1 -5
  5. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/modernize_typing_option.py +1 -5
  6. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python_option.py +3 -36
  7. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/python_blank_lines_utils.py +4 -4
  8. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/python_class_docstring_utils.py +6 -12
  9. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/python_docstring_utils.py +12 -9
  10. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/relocate_imports/python_usage_collector.py +1 -37
  11. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/tests/helper/test_toml.py +23 -0
  12. {wexample_filestate_python-9.4.1/tests/wexample_tests → wexample_filestate_python-10.0.0/tests/unit/utils/relocate_imports}/__init__.py +0 -0
  13. wexample_filestate_python-10.0.0/tests/unit/utils/relocate_imports/test_python_usage_collector.py +104 -0
  14. wexample_filestate_python-10.0.0/tests/unit/utils/test_python_attrs_utils.py +35 -0
  15. wexample_filestate_python-10.0.0/tests/unit/utils/test_python_blank_lines_utils.py +52 -0
  16. wexample_filestate_python-10.0.0/tests/unit/utils/test_python_class_attributes_utils.py +49 -0
  17. wexample_filestate_python-10.0.0/tests/unit/utils/test_python_class_docstring_utils.py +43 -0
  18. wexample_filestate_python-10.0.0/tests/unit/utils/test_python_class_methods_utils.py +61 -0
  19. wexample_filestate_python-10.0.0/tests/unit/utils/test_python_constants_utils.py +60 -0
  20. wexample_filestate_python-10.0.0/tests/unit/utils/test_python_docstring_utils.py +39 -0
  21. wexample_filestate_python-10.0.0/tests/unit/utils/test_python_functions_utils.py +67 -0
  22. wexample_filestate_python-10.0.0/tests/unit/utils/test_python_main_guard_utils.py +33 -0
  23. wexample_filestate_python-10.0.0/tests/unit/utils/test_python_module_metadata_utils.py +26 -0
  24. wexample_filestate_python-10.0.0/tests/unit/utils/test_python_type_checking_utils.py +41 -0
  25. wexample_filestate_python-10.0.0/tests/wexample_tests/__init__.py +0 -0
  26. wexample_filestate_python-9.4.1/tests/unit/helper/test_package.py +0 -16
  27. wexample_filestate_python-9.4.1/tests/unit/helper/test_toml.py +0 -19
  28. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/__init__.py +0 -0
  29. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/common/__init__.py +0 -0
  30. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/common/pipy_gateway.py +0 -0
  31. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/config_option/__init__.py +0 -0
  32. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/config_option/mixin/__init__.py +0 -0
  33. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/config_option/mixin/with_stdout_wrapping_mixin.py +0 -0
  34. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/config_value/__init__.py +0 -0
  35. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/config_value/python_config_value.py +0 -0
  36. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/const/__init__.py +0 -0
  37. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/const/name_pattern.py +0 -0
  38. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/const/path.py +0 -0
  39. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/const/python_file.py +0 -0
  40. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/event/__init__.py +0 -0
  41. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/event/src/__init__.py +0 -0
  42. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/event/src/wexample_event/__init__.py +0 -0
  43. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/file/__init__.py +0 -0
  44. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/file/python_file.py +0 -0
  45. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/file/python_test_stub_file.py +0 -0
  46. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/helper/__init__.py +0 -0
  47. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/helper/package.py +0 -0
  48. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/helper/python/__init__.py +0 -0
  49. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/helper/toml.py +0 -0
  50. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/__init__.py +0 -0
  51. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/__init__.py +0 -0
  52. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/abstract_python_file_content_option.py +0 -0
  53. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/add_future_annotations_option.py +0 -0
  54. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/add_return_types_option.py +0 -0
  55. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/class_name_matches_file_name_option.py +0 -0
  56. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/fix_attrs_option.py +0 -0
  57. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/fix_blank_lines_option.py +0 -0
  58. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/fstringify_option.py +0 -0
  59. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/order_class_attributes_option.py +0 -0
  60. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/order_class_docstring_option.py +0 -0
  61. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/order_class_methods_option.py +0 -0
  62. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/order_constants_option.py +0 -0
  63. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/order_iterable_items_option.py +0 -0
  64. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/order_main_guard_option.py +0 -0
  65. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/order_module_docstring_option.py +0 -0
  66. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/order_module_functions_option.py +0 -0
  67. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/order_module_metadata_option.py +0 -0
  68. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/order_type_checking_block_option.py +0 -0
  69. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/relocate_imports_option.py +0 -0
  70. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/remove_unused_option.py +0 -0
  71. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/sort_imports_option.py +0 -0
  72. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/option/python/unquote_annotations_option.py +0 -0
  73. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/options_provider/__init__.py +0 -0
  74. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/options_provider/python_options_provider.py +0 -0
  75. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/py.typed +0 -0
  76. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/__init__.py +0 -0
  77. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/cst_cache.py +0 -0
  78. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/python_attrs_utils.py +0 -0
  79. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/python_class_attributes_utils.py +0 -0
  80. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/python_class_methods_utils.py +0 -0
  81. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/python_constants_utils.py +0 -0
  82. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/python_functions_utils.py +0 -0
  83. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/python_iterable_utils.py +0 -0
  84. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/python_main_guard_utils.py +0 -0
  85. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/python_module_metadata_utils.py +0 -0
  86. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/python_type_checking_utils.py +0 -0
  87. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/relocate_imports/__init__.py +0 -0
  88. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/relocate_imports/python_import_rewriter.py +0 -0
  89. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/relocate_imports/python_localize_runtime_imports.py +0 -0
  90. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/relocate_imports/python_parser_import_index.py +0 -0
  91. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/src/wexample_filestate_python/utils/relocate_imports/python_runtime_symbol_collector.py +0 -0
  92. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/tests/.gitkeep +0 -0
  93. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/tests/helper/__init__.py +0 -0
  94. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/tests/helper/test_package.py +0 -0
  95. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/tests/tests/__init__.py +0 -0
  96. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/tests/unit/__init__.py +0 -0
  97. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/tests/unit/helper/__init__.py +0 -0
  98. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/tests/unit/utils/__init__.py +0 -0
  99. {wexample_filestate_python-9.4.1 → wexample_filestate_python-10.0.0}/tests/unit/utils/test_python_iterable_utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: wexample-filestate-python
3
- Version: 9.4.1
3
+ Version: 10.0.0
4
4
  Summary: Extends wexample-filestate with Python file options: format, sort imports, modernize typing, and reorder class members.
5
5
  Author-Email: weeger <contact@wexample.com>
6
6
  License: MIT
@@ -12,7 +12,6 @@ Requires-Python: >=3.10
12
12
  Requires-Dist: attrs>=23.1.0
13
13
  Requires-Dist: autoflake
14
14
  Requires-Dist: black
15
- Requires-Dist: cattrs>=23.1.0
16
15
  Requires-Dist: flynt
17
16
  Requires-Dist: isort
18
17
  Requires-Dist: libcst
@@ -20,7 +19,7 @@ Requires-Dist: packaging
20
19
  Requires-Dist: ruff
21
20
  Requires-Dist: tomli
22
21
  Requires-Dist: wexample-api>=6.8.0
23
- Requires-Dist: wexample-filestate>=18.2.0
22
+ Requires-Dist: wexample-filestate>=19.0.0
24
23
  Provides-Extra: dev
25
24
  Requires-Dist: pytest; extra == "dev"
26
25
  Requires-Dist: pytest-cov; extra == "dev"
@@ -28,7 +27,7 @@ Description-Content-Type: text/markdown
28
27
 
29
28
  # filestate_python
30
29
 
31
- Version: 9.4.1
30
+ Version: 10.0.0
32
31
 
33
32
  Extends wexample-filestate with Python file options: format, sort imports, modernize typing, and reorder class members.
34
33
 
@@ -1,6 +1,6 @@
1
1
  # filestate_python
2
2
 
3
- Version: 9.4.1
3
+ Version: 10.0.0
4
4
 
5
5
  Extends wexample-filestate with Python file options: format, sort imports, modernize typing, and reorder class members.
6
6
 
@@ -6,7 +6,7 @@ build-backend = "pdm.backend"
6
6
 
7
7
  [project]
8
8
  name = "wexample-filestate-python"
9
- version = "9.4.1"
9
+ version = "10.0.0"
10
10
  description = "Extends wexample-filestate with Python file options: format, sort imports, modernize typing, and reorder class members."
11
11
  authors = [
12
12
  { name = "weeger", email = "contact@wexample.com" },
@@ -21,7 +21,6 @@ dependencies = [
21
21
  "attrs>=23.1.0",
22
22
  "autoflake",
23
23
  "black",
24
- "cattrs>=23.1.0",
25
24
  "flynt",
26
25
  "isort",
27
26
  "libcst",
@@ -29,7 +28,7 @@ dependencies = [
29
28
  "ruff",
30
29
  "tomli",
31
30
  "wexample-api>=6.8.0",
32
- "wexample-filestate>=18.2.0",
31
+ "wexample-filestate>=19.0.0",
33
32
  ]
34
33
 
35
34
  [project.readme]
@@ -23,11 +23,7 @@ class FormatOption(WithBatchOptionMixin, AbstractPythonFileContentOption):
23
23
  return "Format the Python file content using Black."
24
24
 
25
25
  def _apply_content_change(self, target: TargetFileOrDirectoryType) -> str:
26
- cache = self._get_or_build_batch_cache(target)
27
- path_key = str(target.get_path())
28
- if path_key in cache:
29
- return cache[path_key]
30
- return target.read_text()
26
+ return self._get_batch_rectified_content(target)
31
27
 
32
28
  def _run_batch_on_paths(
33
29
  self,
@@ -40,11 +40,7 @@ class ModernizeTypingOption(WithBatchOptionMixin, AbstractPythonFileContentOptio
40
40
  return "Modernize Python syntax (PEP 585/604, …) using Ruff's UP rules."
41
41
 
42
42
  def _apply_content_change(self, target: TargetFileOrDirectoryType) -> str:
43
- cache = self._get_or_build_batch_cache(target)
44
- path_key = str(target.get_path())
45
- if path_key in cache:
46
- return cache[path_key]
47
- return target.read_text()
43
+ return self._get_batch_rectified_content(target)
48
44
 
49
45
  def _run_batch_on_paths(
50
46
  self,
@@ -1,41 +1,19 @@
1
1
  from __future__ import annotations
2
2
 
3
- from typing import TYPE_CHECKING, Any, Union
3
+ from typing import Any, Union
4
4
 
5
5
  from wexample_config.config_option.abstract_config_option import AbstractConfigOption
6
- from wexample_config.config_option.abstract_nested_config_option import (
7
- AbstractNestedConfigOption,
8
- )
9
- from wexample_filestate.enum.scopes import Scope
10
- from wexample_filestate.operation.abstract_operation import AbstractOperation
11
- from wexample_filestate.option.mixin.option_mixin import OptionMixin
6
+ from wexample_filestate.option.abstract_container_option import AbstractContainerOption
12
7
  from wexample_helpers.decorator.base_class import base_class
13
8
 
14
- if TYPE_CHECKING:
15
- from wexample_filestate.const.types_state_items import TargetFileOrDirectoryType
16
-
17
9
  # Module-level caches (safe per the @base_class note below; only instance-level
18
10
  # annotations become attrs fields, not plain module variables).
19
- _SCOPES: list[Scope] = [Scope.CONTENT]
20
11
  _RAW_VALUE_ALLOWED_TYPE: Any = None
21
12
  _ALLOWED_OPTIONS: list[type[AbstractConfigOption]] | None = None
22
13
 
23
14
 
24
15
  @base_class
25
- class PythonOption(OptionMixin, AbstractNestedConfigOption):
26
- @classmethod
27
- def get_nature(cls) -> None:
28
- """A container: no nature of its own, each of its rules has one."""
29
- return None
30
-
31
- # Do NOT cache results in `cls._<name>_cache` class attributes. With
32
- # `@base_class`, attrs treats class-level annotations as instance fields;
33
- # `PythonOption._cache` then raises AttributeError at runtime. Use
34
- # module-level constants or `functools.cache` on methods if needed.
35
- @classmethod
36
- def get_scopes(cls) -> list[Scope]:
37
- return _SCOPES
38
-
16
+ class PythonOption(AbstractContainerOption):
39
17
  @staticmethod
40
18
  def get_raw_value_allowed_type() -> Any:
41
19
  global _RAW_VALUE_ALLOWED_TYPE
@@ -47,11 +25,6 @@ class PythonOption(OptionMixin, AbstractNestedConfigOption):
47
25
  _RAW_VALUE_ALLOWED_TYPE = Union[list[str], dict, PythonConfigValue]
48
26
  return _RAW_VALUE_ALLOWED_TYPE
49
27
 
50
- def create_required_operation(
51
- self, target: TargetFileOrDirectoryType, scopes: set[Scope]
52
- ) -> AbstractOperation | None:
53
- return self._create_child_required_operation(target=target, scopes=scopes)
54
-
55
28
  def get_allowed_options(self) -> list[type[AbstractConfigOption]]:
56
29
  global _ALLOWED_OPTIONS
57
30
  if _ALLOWED_OPTIONS is not None:
@@ -150,9 +123,3 @@ class PythonOption(OptionMixin, AbstractNestedConfigOption):
150
123
  ]
151
124
  return _ALLOWED_OPTIONS
152
125
 
153
- def set_value(self, raw_value: Any) -> None:
154
- # Convert list form to dict form for consistency
155
- if isinstance(raw_value, list):
156
- raw_value = dict.fromkeys(raw_value, True)
157
-
158
- super().set_value(raw_value=raw_value)
@@ -142,7 +142,7 @@ def _fix_module_docstring_spacing(module: cst.Module) -> cst.Module:
142
142
  if isinstance(next_stmt, cst.SimpleStatementLine):
143
143
  # Remove all blank leading lines and add exactly one
144
144
  non_blank_leading = _filter_blank_lines(next_stmt.leading_lines)
145
- new_leading = [cst.EmptyLine()] + non_blank_leading
145
+ new_leading = [cst.EmptyLine(indent=False)] + non_blank_leading
146
146
  body_list[next_idx] = next_stmt.with_changes(leading_lines=new_leading)
147
147
  changed = True
148
148
 
@@ -346,7 +346,7 @@ def _normalize_class_properties_spacing(
346
346
 
347
347
  if blank_count != target_blanks:
348
348
  non_blank_leading = _filter_blank_lines(current_node.leading_lines)
349
- new_leading = [cst.EmptyLine()] * target_blanks + non_blank_leading
349
+ new_leading = [cst.EmptyLine(indent=False)] * target_blanks + non_blank_leading
350
350
  body_list[i] = current_node.with_changes(leading_lines=new_leading)
351
351
  changed = True
352
352
 
@@ -362,7 +362,7 @@ def _normalize_class_properties_spacing(
362
362
 
363
363
  if blank_count != 1:
364
364
  non_blank_leading = _filter_blank_lines(method_node.leading_lines)
365
- new_leading = [cst.EmptyLine()] + non_blank_leading
365
+ new_leading = [cst.EmptyLine(indent=False)] + non_blank_leading
366
366
  body_list[first_method_idx] = method_node.with_changes(
367
367
  leading_lines=new_leading
368
368
  )
@@ -395,7 +395,7 @@ def _normalize_double_blank_lines_in_suite(suite: cst.Suite) -> cst.Suite:
395
395
  if blank_count > 1:
396
396
  # Keep non-blank leading lines and add exactly 1 blank line
397
397
  non_blank_leading = _filter_blank_lines(current_node.leading_lines)
398
- new_leading = [cst.EmptyLine()] + non_blank_leading
398
+ new_leading = [cst.EmptyLine(indent=False)] + non_blank_leading
399
399
  body_list[i] = current_node.with_changes(leading_lines=new_leading)
400
400
  changed = True
401
401
 
@@ -66,17 +66,11 @@ def move_class_docstring_to_top(classdef: cst.ClassDef) -> cst.ClassDef:
66
66
  def normalize_docstring_quotes_stmt(
67
67
  stmt: cst.SimpleStatementLine,
68
68
  ) -> cst.SimpleStatementLine:
69
- """Normalize class docstring quotes to double quotes, similar to module behavior."""
69
+ """Normalize class docstring quotes to double quotes, as for the module docstring."""
70
+ from wexample_filestate_python.utils.python_docstring_utils import (
71
+ normalize_docstring_quotes,
72
+ )
73
+
70
74
  if not (isinstance(stmt, cst.SimpleStatementLine) and len(stmt.body) == 1):
71
75
  return stmt
72
- small = stmt.body[0]
73
- if not (isinstance(small, cst.Expr) and isinstance(small.value, cst.SimpleString)):
74
- return stmt
75
- q = small.value.quote
76
- if q.startswith('"'):
77
- return stmt
78
- # Convert starting quote to double
79
- new_quote = '"""' if q.startswith("'''") else '"'
80
- new_value = small.value.with_changes(quote=new_quote)
81
- new_small = small.with_changes(value=new_value)
82
- return stmt.with_changes(body=[new_small])
76
+ return normalize_docstring_quotes(stmt)
@@ -94,18 +94,21 @@ def normalize_docstring_quotes(
94
94
 
95
95
  string_value = expr.value
96
96
  quote = string_value.quote
97
-
98
- # Convert single quotes to double quotes
99
- if quote.startswith("'''"):
100
- new_quote = '"""'
101
- elif quote.startswith("'"):
102
- new_quote = '"'
103
- else:
97
+ if quote.startswith('"'):
104
98
  # Already using double quotes
105
99
  return docstring_node
106
100
 
107
- # Create new string with double quotes
108
- new_string = string_value.with_changes(quote=new_quote)
101
+ # `quote` is read from the literal, not a field: the literal is rebuilt.
102
+ new_quote = '"""' if len(quote) == 3 else '"'
103
+ prefix = string_value.prefix
104
+ inner = string_value.value[len(prefix) + len(quote) : -len(quote)]
105
+ if '"' in inner:
106
+ # A double quote inside would end the string, or change it: left as is.
107
+ return docstring_node
108
+
109
+ new_string = string_value.with_changes(
110
+ value=f"{prefix}{new_quote}{inner}{new_quote}"
111
+ )
109
112
  new_expr = expr.with_changes(value=new_string)
110
113
  new_body = [new_expr, *docstring_node.body[1:]]
111
114
 
@@ -242,42 +242,6 @@ class PythonUsageCollector(cst.CSTVisitor):
242
242
  return
243
243
  self.functions_needing_local[self.func_stack[-1]].add(val)
244
244
 
245
- # Track param default context; defaults are evaluated at definition time (module init), not runtime
246
- def visit_Param(self, node: cst.Param) -> bool: # type: ignore[override]
247
- has_default = node.default is not None
248
- self._in_param_default_stack.append(has_default)
249
- # Mark that we are in a parameter annotation while traversing children
250
- in_annot = node.annotation is not None
251
- self._in_param_annot_stack.append(in_annot)
252
- # Record parameter annotation types into C (annotation usage)
253
- if node.annotation is not None:
254
- self._record_type_names(node.annotation.annotation, self.used_in_C_annot)
255
- # Names in defaults are needed at definition time: treat as B
256
- if has_default:
257
- try:
258
- # Collect base identifiers even if not recognized as imported yet
259
- collected: set[str] = set()
260
-
261
- def _collect_base_names(expr: cst.BaseExpression) -> None:
262
- if isinstance(expr, cst.Name):
263
- collected.add(expr.value)
264
- elif isinstance(expr, cst.Attribute):
265
- _collect_base_names(expr.value)
266
- elif isinstance(expr, cst.Subscript):
267
- _collect_base_names(expr.value)
268
- for e in expr.slice:
269
- if isinstance(e, cst.SubscriptElement) and isinstance(
270
- e.slice, cst.Index
271
- ):
272
- _collect_base_names(e.slice.value)
273
-
274
- _collect_base_names(node.default)
275
- self.used_in_B.update(collected)
276
- except Exception:
277
- pass
278
- # We still record annotation as C elsewhere
279
- return True
280
-
281
245
  # ----- C: function param annotations -----
282
246
  def visit_Param(self, node: cst.Param) -> None: # type: ignore[override]
283
247
  if node.annotation is not None:
@@ -286,7 +250,7 @@ class PythonUsageCollector(cst.CSTVisitor):
286
250
  if node.default is not None:
287
251
  self._in_param_default_count += 1
288
252
 
289
- # Fallback: explicitly scan Parameters node for defaults (some environments may not trigger visit_Param)
253
+ # Names in parameter defaults are needed at definition time: treated as B.
290
254
  def visit_Parameters(self, node: cst.Parameters) -> bool: # type: ignore[override]
291
255
  try:
292
256
  # Aggregate all parameter-like collections
@@ -12,6 +12,29 @@ def test_toml_ensure_array_creates_when_missing() -> None:
12
12
  assert "deps" in tbl
13
13
 
14
14
 
15
+ def test_toml_ensure_array_multiline_creates_a_multiline_array() -> None:
16
+ from tomlkit import document
17
+
18
+ from wexample_filestate_python.helper.toml import toml_ensure_array_multiline
19
+
20
+ doc = document()
21
+ arr, changed = toml_ensure_array_multiline(doc, "deps")
22
+ assert changed is True
23
+ arr.extend(["a", "b"])
24
+ assert doc.as_string() == 'deps = [\n "a",\n "b",\n]\n'
25
+
26
+
27
+ def test_toml_ensure_array_multiline_reformats_an_existing_array() -> None:
28
+ import tomlkit
29
+
30
+ from wexample_filestate_python.helper.toml import toml_ensure_array_multiline
31
+
32
+ doc = tomlkit.parse('deps = ["a", "b"]\n')
33
+ _, changed = toml_ensure_array_multiline(doc, "deps")
34
+ assert changed is False
35
+ assert "\n" in doc["deps"].as_string()
36
+
37
+
15
38
  def test_toml_ensure_array_returns_existing_unchanged() -> None:
16
39
  from tomlkit import array
17
40
 
@@ -0,0 +1,104 @@
1
+ from __future__ import annotations
2
+
3
+ SOURCE = '''from __future__ import annotations
4
+
5
+ from pathlib import Path
6
+
7
+ from foo.bar import Helper
8
+ from foo.base import Model
9
+ from foo.const import DEFAULT
10
+ from foo.deco import command
11
+ from foo.types import OnlyType
12
+
13
+
14
+ @command
15
+ class A(Model):
16
+ def run(self, x: OnlyType, y=DEFAULT) -> Path:
17
+ return Helper().go(x)
18
+ '''
19
+
20
+
21
+ def _collect(src: str):
22
+ from collections import defaultdict
23
+
24
+ import libcst as cst
25
+
26
+ from wexample_filestate_python.utils.relocate_imports.python_parser_import_index import (
27
+ PythonParserImportIndex,
28
+ )
29
+ from wexample_filestate_python.utils.relocate_imports.python_usage_collector import (
30
+ PythonUsageCollector,
31
+ )
32
+
33
+ module = cst.parse_module(src)
34
+ idx = PythonParserImportIndex()
35
+ module.visit(idx)
36
+ result = {
37
+ "local": defaultdict(set),
38
+ "class_level": set(),
39
+ "annotations": set(),
40
+ "casts": set(),
41
+ }
42
+ module.visit(
43
+ PythonUsageCollector(
44
+ imported_value_names=set(idx.name_to_from),
45
+ functions_needing_local=result["local"],
46
+ used_in_B=result["class_level"],
47
+ used_in_C_annot=result["annotations"],
48
+ cast_type_names_anywhere=result["casts"],
49
+ idx=idx,
50
+ )
51
+ )
52
+ return idx, result
53
+
54
+
55
+ def test_import_index() -> None:
56
+ idx, _ = _collect(SOURCE)
57
+ assert idx.name_to_from["Helper"] == ("foo.bar", None)
58
+ assert idx.name_to_from["Path"] == ("pathlib", None)
59
+
60
+
61
+ def test_usage_categories() -> None:
62
+ _, result = _collect(SOURCE)
63
+ # Used inside a method body: to import locally there.
64
+ assert dict(result["local"]) == {"A.run": {"Helper"}}
65
+ # Needed when the class is defined: base, decorator, parameter default.
66
+ assert result["class_level"] == {"Model", "command", "DEFAULT"}
67
+ # Annotations only.
68
+ assert result["annotations"] == {"OnlyType", "Path"}
69
+ assert result["casts"] == set()
70
+
71
+
72
+ def test_cast_types_are_recorded() -> None:
73
+ src = (
74
+ "from typing import cast\n"
75
+ "from foo import Thing\n"
76
+ "def f(x):\n"
77
+ " return cast(Thing, x)\n"
78
+ )
79
+ _, result = _collect(src)
80
+ assert "Thing" in result["casts"]
81
+
82
+
83
+ def test_runtime_symbol_collector_sees_import_statements_too() -> None:
84
+ """Pins a known flaw: the names of the import statements themselves are
85
+ taken for runtime uses, so every imported name reads as runtime-used and
86
+ RelocateImportsOption finds nothing to move. See the 2026-11-14 report."""
87
+ import libcst as cst
88
+
89
+ from wexample_filestate_python.utils.relocate_imports.python_runtime_symbol_collector import (
90
+ PythonRuntimeSymbolCollector,
91
+ )
92
+
93
+ collector = PythonRuntimeSymbolCollector(
94
+ imported_value_names={"Path", "Helper", "Model", "DEFAULT", "OnlyType", "command"}
95
+ )
96
+ cst.parse_module(SOURCE).visit(collector)
97
+ assert collector.runtime_used_anywhere == {
98
+ "Path",
99
+ "Helper",
100
+ "Model",
101
+ "DEFAULT",
102
+ "OnlyType",
103
+ "command",
104
+ }
@@ -0,0 +1,35 @@
1
+ from __future__ import annotations
2
+
3
+
4
+ def _fix(src: str) -> str:
5
+ import libcst as cst
6
+
7
+ from wexample_filestate_python.utils.python_attrs_utils import fix_attrs_kw_only
8
+
9
+ return fix_attrs_kw_only(cst.parse_module(src)).code
10
+
11
+
12
+ def test_adds_kw_only_to_attrs_decorators() -> None:
13
+ src = (
14
+ "import attrs\n"
15
+ "@attrs.define\n"
16
+ "class A:\n"
17
+ " x: int\n"
18
+ "@attrs.define(slots=False)\n"
19
+ "class B:\n"
20
+ " y: int\n"
21
+ )
22
+ out = _fix(src)
23
+ assert "@attrs.define(kw_only = True)\nclass A" in out
24
+ assert "@attrs.define(slots=False, kw_only = True)\nclass B" in out
25
+ assert _fix(out) == out
26
+
27
+
28
+ def test_forces_an_explicit_kw_only_false_to_true() -> None:
29
+ src = "import attrs\n@attrs.define(kw_only=False)\nclass A:\n x: int\n"
30
+ assert _fix(src) == "import attrs\n@attrs.define(kw_only=True)\nclass A:\n x: int\n"
31
+
32
+
33
+ def test_leaves_other_decorators_alone() -> None:
34
+ src = "import dataclasses\n@other\nclass A:\n x: int\n@dataclasses.dataclass\nclass B:\n y: int\n"
35
+ assert _fix(src) == src
@@ -0,0 +1,52 @@
1
+ from __future__ import annotations
2
+
3
+
4
+ def _fix(src: str) -> str:
5
+ import libcst as cst
6
+
7
+ from wexample_filestate_python.utils.python_blank_lines_utils import (
8
+ fix_function_blank_lines,
9
+ )
10
+
11
+ return fix_function_blank_lines(cst.parse_module(src)).code
12
+
13
+
14
+ def test_removes_leading_blanks_and_collapses_doubles_in_bodies() -> None:
15
+ src = (
16
+ "def f():\n"
17
+ "\n\n"
18
+ " x = 1\n"
19
+ "\n\n\n"
20
+ " return x\n"
21
+ )
22
+ assert _fix(src) == "def f():\n x = 1\n\n return x\n"
23
+
24
+
25
+ def test_inserted_blank_lines_carry_no_indentation() -> None:
26
+ src = "class A:\n def m(self):\n x = 1\n\n\n return x\n"
27
+ out = _fix(src)
28
+ assert out == "class A:\n def m(self):\n x = 1\n\n return x\n"
29
+ assert not any(line and not line.strip() for line in out.splitlines())
30
+
31
+
32
+ def test_removes_leading_blanks_from_class_and_method_bodies() -> None:
33
+ src = "class A:\n\n\n name: str\n def m(self):\n\n pass\n"
34
+ assert _fix(src) == "class A:\n name: str\n def m(self):\n pass\n"
35
+
36
+
37
+ def test_separates_the_module_docstring_from_the_code() -> None:
38
+ assert _fix('"""Doc."""\nimport os\n') == '"""Doc."""\n\nimport os\n'
39
+
40
+
41
+ def test_is_idempotent() -> None:
42
+ src = (
43
+ '"""Doc."""\nimport os\ndef f():\n\n\n x = 1\n\n\n\n return x\n'
44
+ "class A:\n\n\n name: str\n other: int = 0\n def m(self):\n\n pass\n"
45
+ )
46
+ once = _fix(src)
47
+ assert _fix(once) == once
48
+
49
+
50
+ def test_leaves_a_clean_module_unchanged() -> None:
51
+ src = '"""Doc."""\n\nimport os\n\n\ndef f():\n return 1\n'
52
+ assert _fix(src) == src
@@ -0,0 +1,49 @@
1
+ from __future__ import annotations
2
+
3
+
4
+ def _order(src: str) -> str:
5
+ import libcst as cst
6
+
7
+ from wexample_filestate_python.utils.python_class_attributes_utils import (
8
+ ensure_order_class_attributes_in_module,
9
+ )
10
+
11
+ return ensure_order_class_attributes_in_module(cst.parse_module(src)).code
12
+
13
+
14
+ def test_sorts_public_attributes_before_private_ones() -> None:
15
+ src = (
16
+ "class A:\n"
17
+ " zeta: int = 1\n"
18
+ " _private: int = 2\n"
19
+ ' alpha: str = "a"\n'
20
+ " def m(self): pass\n"
21
+ )
22
+ assert _order(src) == (
23
+ "class A:\n"
24
+ ' alpha: str = "a"\n'
25
+ " zeta: int = 1\n"
26
+ " _private: int = 2\n"
27
+ " def m(self): pass\n"
28
+ )
29
+
30
+
31
+ def test_is_idempotent_and_leaves_sorted_classes_alone() -> None:
32
+ src = "class A:\n alpha: int = 1\n beta: int = 2\n _gamma: int = 3\n"
33
+ assert _order(src) == src
34
+ shuffled = "class A:\n beta: int = 2\n _gamma: int = 3\n alpha: int = 1\n"
35
+ once = _order(shuffled)
36
+ assert _order(once) == once
37
+
38
+
39
+ def test_a_dataclass_keeps_fields_without_default_first() -> None:
40
+ src = (
41
+ "from dataclasses import dataclass\n"
42
+ "@dataclass\n"
43
+ "class A:\n"
44
+ " zeta: int = 1\n"
45
+ " alpha: int\n"
46
+ )
47
+ out = _order(src)
48
+ # Reordering would put a field without default after one with: a TypeError.
49
+ assert out.index("alpha: int\n") < out.index("zeta: int = 1")
@@ -0,0 +1,43 @@
1
+ from __future__ import annotations
2
+
3
+
4
+ def _first(src: str) -> str:
5
+ import libcst as cst
6
+
7
+ from wexample_filestate_python.utils.python_class_docstring_utils import (
8
+ ensure_all_classes_docstring_first,
9
+ )
10
+
11
+ return ensure_all_classes_docstring_first(cst.parse_module(src)).code
12
+
13
+
14
+ def test_moves_a_misplaced_docstring_first_and_normalizes_its_quotes() -> None:
15
+ src = "class A:\n x = 1\n '''Doc.'''\n"
16
+ assert _first(src) == 'class A:\n """Doc."""\n x = 1\n'
17
+
18
+
19
+ def test_moves_a_double_quoted_docstring() -> None:
20
+ src = 'class A:\n x = 1\n\n """Doc."""\n'
21
+ assert _first(src) == 'class A:\n """Doc."""\n x = 1\n'
22
+
23
+
24
+ def test_leaves_well_placed_docstrings_and_nested_classes() -> None:
25
+ src = 'class A:\n """Doc."""\n class B:\n y = 2\n """Inner."""\n'
26
+ out = _first(src)
27
+ assert out.startswith('class A:\n """Doc."""\n')
28
+ assert _first(out) == out
29
+
30
+
31
+ def test_is_class_docstring_first() -> None:
32
+ import libcst as cst
33
+
34
+ from wexample_filestate_python.utils.python_class_docstring_utils import (
35
+ is_class_docstring_first,
36
+ )
37
+
38
+ first, misplaced, none = cst.parse_module(
39
+ 'class A:\n """D."""\nclass B:\n x = 1\n """D."""\nclass C:\n x = 1\n'
40
+ ).body
41
+ assert is_class_docstring_first(first) is True
42
+ assert is_class_docstring_first(misplaced) is False
43
+ assert is_class_docstring_first(none) is False