pyprojkit 0.3.0__tar.gz → 0.3.1__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,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyprojkit
3
- Version: 0.3.0
3
+ Version: 0.3.1
4
4
  Summary: Development workflow toolkit for Python projects: pyproject.toml sync, doit tasks, nox sessions
5
5
  Author: mm21
6
6
  Author-email: mm21 <mm21.dev@gmail.com>
@@ -27,7 +27,7 @@ license-files = ["LICENSE"]
27
27
  name = "pyprojkit"
28
28
  readme = "README.md"
29
29
  requires-python = ">=3.12,<3.15" # managed by pyprojkit
30
- version = "0.3.0"
30
+ version = "0.3.1"
31
31
 
32
32
  [project.optional-dependencies]
33
33
  all = [
@@ -27,13 +27,13 @@ class ConfigError(Exception):
27
27
  @dataclass(kw_only=True)
28
28
  class BaseToolConfig(ABC):
29
29
  """
30
- Configuration for a tool which reads its settings from a table in
31
- `pyproject.toml`, e.g. `[tool.black]`.
30
+ Configuration for a tool which reads its settings from a table in `pyproject.toml`,
31
+ e.g. `[tool.black]`.
32
32
 
33
- Dataclass fields map to table entries; a field whose name differs from its
34
- TOML key declares the key via `field(metadata={"toml": "some-key"})`.
35
- Fields set to `None` are omitted. Values derived from project-wide config
36
- (e.g. black's `target-version`) are contributed by `extra_toml()`.
33
+ Dataclass fields map to table entries; a field whose name differs from its TOML key
34
+ declares the key via `field(metadata={"toml": "some-key"})`. Fields set to `None`
35
+ are omitted. Values derived from project-wide config (e.g. black's `target-version`)
36
+ are contributed by `extra_toml()`.
37
37
  """
38
38
 
39
39
  table_path: ClassVar[str]
@@ -1,9 +1,9 @@
1
1
  """
2
2
  Profile registries: named factories for pre-canned configurations.
3
3
 
4
- A tools profile encompasses all tool categories; a formatting profile covers
5
- just the formatter chain. Third parties can register their own profiles and
6
- reuse them across projects:
4
+ A tools profile encompasses all tool categories; a formatting profile covers just the
5
+ formatter chain. Third parties can register their own profiles and reuse them across
6
+ projects:
7
7
 
8
8
  ```python
9
9
  from pyprojkit import register_tools_profile
@@ -5,16 +5,16 @@ Managed content:
5
5
 
6
6
  - `project.requires-python`
7
7
  - Python version classifiers (`Programming Language :: Python :: 3[.X]`);
8
- other classifiers are left untouched
8
+ other classifiers are left untouched, as are comments in the list
9
9
  - One `[tool.X]` table per enabled tool (fully owned — any hand edits or
10
10
  comments inside are overwritten)
11
11
  - `[tool.pyprojkit].managed`: bookkeeping list of owned tables, enabling safe
12
12
  removal of tables for tools dropped from the configuration
13
13
 
14
- Everything else (dependencies, build-system, urls, unmanaged tool tables,
15
- etc.) is preserved. Output is normalized with toml-sort (as a library, using
16
- the same settings as the managed `[tool.tomlsort]` table), so a subsequent
17
- `toml-sort` run in the format task is a no-op.
14
+ Everything else (dependencies, build-system, urls, unmanaged tool tables, etc.) is
15
+ preserved. Output is normalized with toml-sort (as a library, using the same settings as
16
+ the managed `[tool.tomlsort]` table), so a subsequent `toml-sort` run in the format task
17
+ is a no-op.
18
18
  """
19
19
 
20
20
  from __future__ import annotations
@@ -22,11 +22,11 @@ from __future__ import annotations
22
22
  import difflib
23
23
  import re
24
24
  from pathlib import Path
25
- from typing import Any, cast
25
+ from typing import Any
26
26
 
27
27
  import tomlkit
28
28
  from tomlkit import TOMLDocument
29
- from tomlkit.items import Table
29
+ from tomlkit.items import Null, Table
30
30
 
31
31
  from .config.base import ConfigError
32
32
  from .config.project import ProjectConfig
@@ -140,22 +140,74 @@ def _update_classifiers(project: Table, config: ProjectConfig):
140
140
  """
141
141
  Replace python version classifiers with those derived from config, preserving all
142
142
  others; result is sorted, with managed entries marked by an inline comment.
143
+
144
+ Comments in the existing list are preserved: an inline comment stays with its entry,
145
+ and a standalone comment stays with the entry it precedes (or is emitted at the end
146
+ of the list if it precedes none).
143
147
  """
144
- existing = cast(list[str], [str(c) for c in project.get("classifiers", [])])
145
- kept = [c for c in existing if not _CLASSIFIER_RE.match(c)]
146
148
  managed = set(config.python.classifiers)
149
+ existing = project.get("classifiers", [])
150
+ kept = [str(c) for c in existing if not _CLASSIFIER_RE.match(str(c))]
151
+ leading, inline, trailing = _collect_classifier_comments(
152
+ existing, set(kept) | managed
153
+ )
147
154
 
148
155
  array = tomlkit.array()
149
156
  for entry in sorted(set(kept) | managed):
157
+ for comment in leading.get(entry, []):
158
+ array.add_line(comment=comment, indent=" ")
150
159
  array.add_line(
151
160
  entry,
152
161
  indent=" ",
153
- comment=_MANAGED_COMMENT if entry in managed else None,
162
+ comment=_MANAGED_COMMENT if entry in managed else inline.get(entry),
154
163
  )
164
+ for comment in trailing:
165
+ array.add_line(comment=comment, indent=" ")
155
166
  array.add_line(indent="")
156
167
  project["classifiers"] = array
157
168
 
158
169
 
170
+ def _collect_classifier_comments(
171
+ array: Any, surviving: set[str]
172
+ ) -> tuple[dict[str, list[str]], dict[str, str], list[str]]:
173
+ """
174
+ Extract comments from an existing classifiers array, anchored to the entries they
175
+ accompany. Comments attached to entries which don't survive the sync are carried
176
+ forward to the next surviving entry.
177
+ """
178
+ leading: dict[str, list[str]] = {}
179
+ inline: dict[str, str] = {}
180
+ pending: list[str] = []
181
+
182
+ for group in getattr(array, "_value", []):
183
+ comment = _comment_text(group)
184
+ if group.value is None or isinstance(group.value, Null):
185
+ # standalone comment line
186
+ if comment:
187
+ pending.append(comment)
188
+ continue
189
+
190
+ entry = str(group.value)
191
+ if entry in surviving:
192
+ if pending:
193
+ leading.setdefault(entry, []).extend(pending)
194
+ pending = []
195
+ if comment and comment != _MANAGED_COMMENT:
196
+ inline[entry] = comment
197
+ elif comment and comment != _MANAGED_COMMENT:
198
+ # entry is going away; keep its comment as a standalone one
199
+ pending.append(comment)
200
+
201
+ return leading, inline, pending
202
+
203
+
204
+ def _comment_text(group: Any) -> str | None:
205
+ comment = getattr(group, "comment", None)
206
+ if comment is None:
207
+ return None
208
+ return comment.trivia.comment.lstrip("#").strip() or None
209
+
210
+
159
211
  def _get_managed_list(doc: TOMLDocument) -> list[str]:
160
212
  try:
161
213
  return list(doc["tool"]["pyprojkit"]["managed"]) # type: ignore[index]
File without changes
File without changes