dycw-pre-commit-hooks 0.11.0__py3-none-any.whl → 0.11.2__py3-none-any.whl

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.

Potentially problematic release.


This version of dycw-pre-commit-hooks might be problematic. Click here for more details.

@@ -1,11 +1,13 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dycw-pre-commit-hooks
3
- Version: 0.11.0
3
+ Version: 0.11.2
4
4
  Author-email: Derek Wan <d.wan@icloud.com>
5
5
  Requires-Python: >=3.12
6
6
  Requires-Dist: click<8.3,>=8.2.1
7
- Requires-Dist: dycw-utilities<0.126,>=0.125.10
7
+ Requires-Dist: dycw-utilities<0.151,>=0.150.11
8
+ Requires-Dist: libcst<1.9,>=1.8.2
8
9
  Requires-Dist: loguru<0.8,>=0.7.3
10
+ Requires-Dist: packaging<25.1,>=25.0
9
11
  Requires-Dist: tomlkit<0.14,>=0.13.2
10
12
  Description-Content-Type: text/markdown
11
13
 
@@ -26,6 +28,7 @@ My [`pre-commit`](https://pre-commit.com/) hooks.
26
28
  - repo: https://github.com/dycw/pre-commit-hooks
27
29
  rev: master
28
30
  hooks:
31
+ - id: replace-sequence-str
29
32
  - id: run-bump-my-version
30
33
  - id: run-ruff-format
31
34
  ```
@@ -0,0 +1,15 @@
1
+ pre_commit_hooks/__init__.py,sha256=lAKdthArx0NmOOdvqV2oArnC7ufTxG7U4dfzJERu5Rg,59
2
+ pre_commit_hooks/common.py,sha256=JDpKC4-T4ucPZkK3bAQZ9xrYKsJ7wi1mTo5gVXFGe80,695
3
+ pre_commit_hooks/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ pre_commit_hooks/format_requirements/__init__.py,sha256=OXqdd3J5p9sstWcdEOCQb5wH3tDAZStecO6CKRZvBuA,3381
5
+ pre_commit_hooks/format_requirements/__main__.py,sha256=15JSp_rhjI_Ddoj4MRkHFShfnYxs6GggUhLRlGtrQ0E,156
6
+ pre_commit_hooks/replace_sequence_str/__init__.py,sha256=bPkc3KHbu8RMFykIsegW06hAKUXtXjJD36KtECDsNR8,1607
7
+ pre_commit_hooks/replace_sequence_str/__main__.py,sha256=B1dxOxngV4vUVnDVrXSywiySOs1P_zF30_4ZMRsOSaY,157
8
+ pre_commit_hooks/run_bump_my_version/__init__.py,sha256=9RpVVqTmtmKkV8rMR4sMfI-i_7gaLOmNrfi2GWgtNSk,1953
9
+ pre_commit_hooks/run_bump_my_version/__main__.py,sha256=w2V3y61jrSau-zxjl8ciHtWPlJQwXbYxNJ2tGYVyI4s,156
10
+ pre_commit_hooks/run_ruff_format/__init__.py,sha256=4QYVE-ktOZjJZGqLMpy4_Te1dASzEkEYVBHzpa1EdGM,2062
11
+ pre_commit_hooks/run_ruff_format/__main__.py,sha256=faesqqpMaesg5r-LvkkQt1W9kahvNr-60K3SMYv1NgY,152
12
+ dycw_pre_commit_hooks-0.11.2.dist-info/METADATA,sha256=bY5QDsslyHwR37tAwZiLdBPROljDO1uNx0gUbr4JQAY,884
13
+ dycw_pre_commit_hooks-0.11.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
14
+ dycw_pre_commit_hooks-0.11.2.dist-info/entry_points.txt,sha256=V5pxLTDfSr4UJAWI-1bNEazF1SCSkkpYr0nRx3B0QTg,268
15
+ dycw_pre_commit_hooks-0.11.2.dist-info/RECORD,,
@@ -1,4 +1,5 @@
1
1
  [console_scripts]
2
+ format-requirements = pre_commit_hooks.format_requirements:main
3
+ replace-sequence-str = pre_commit_hooks.replace_sequence_str:main
2
4
  run-bump-my-version = pre_commit_hooks.run_bump_my_version:main
3
- run-bump2version = pre_commit_hooks.run_bump2version:main
4
5
  run-ruff-format = pre_commit_hooks.run_ruff_format:main
@@ -1,3 +1,3 @@
1
1
  from __future__ import annotations
2
2
 
3
- __version__ = "0.11.0"
3
+ __version__ = "0.11.2"
@@ -4,7 +4,7 @@ from dataclasses import dataclass
4
4
 
5
5
  from loguru import logger
6
6
  from tomlkit import TOMLDocument, parse
7
- from utilities.git import get_repo_root
7
+ from utilities.pathlib import get_repo_root
8
8
 
9
9
  _ROOT = get_repo_root()
10
10
  PYPROJECT_TOML = _ROOT.joinpath("pyproject.toml")
@@ -0,0 +1,109 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING, Any, override
4
+
5
+ from click import argument, command
6
+ from packaging._tokenizer import ParserSyntaxError
7
+ from packaging.requirements import (
8
+ InvalidRequirement,
9
+ Requirement,
10
+ _parse_requirement, # pyright: ignore[reportPrivateImportUsage]
11
+ )
12
+ from packaging.specifiers import Specifier, SpecifierSet
13
+ from tomlkit import array, dumps, loads, string
14
+ from tomlkit.items import Array, Table
15
+ from utilities.atomicwrites import writer
16
+ from utilities.click import FilePath
17
+
18
+ from pre_commit_hooks.common import PYPROJECT_TOML
19
+
20
+ if TYPE_CHECKING:
21
+ from collections.abc import Iterator
22
+ from pathlib import Path
23
+
24
+ from tomlkit.toml_document import TOMLDocument
25
+
26
+
27
+ @command()
28
+ @argument("paths", nargs=-1, type=FilePath)
29
+ def main(*, paths: tuple[Path, ...]) -> bool:
30
+ """CLI for the `format_requirements` hook."""
31
+ results = list(map(_process, paths))
32
+ return all(results)
33
+
34
+
35
+ def _process(path: Path, /) -> bool:
36
+ doc = loads(path.read_text())
37
+ expected = _format_path(path)
38
+ if doc == expected:
39
+ return True
40
+ with writer(path, overwrite=True) as temp:
41
+ _ = temp.write_text(dumps(expected))
42
+ return False
43
+
44
+
45
+ def _format_path(path: Path, /) -> TOMLDocument:
46
+ doc = loads(path.read_text())
47
+ if isinstance(dep_grps := doc.get("dependency-groups"), Table):
48
+ for key, value in dep_grps.items():
49
+ if isinstance(value, Array):
50
+ dep_grps[key] = _format_array(value)
51
+ if isinstance(project := doc["project"], Table):
52
+ if isinstance(deps := project["dependencies"], Array):
53
+ project["dependencies"] = _format_array(deps)
54
+ if isinstance(optional := project.get("optional-dependencies"), Table):
55
+ for key, value in optional.items():
56
+ if isinstance(value, Array):
57
+ optional[key] = _format_array(value)
58
+ return doc
59
+
60
+
61
+ def _format_array(dependencies: Array, /) -> Array:
62
+ new = array().multiline(multiline=True)
63
+ new.extend(map(_format_item, dependencies))
64
+ return new
65
+
66
+
67
+ def _format_item(item: Any, /) -> Any:
68
+ if not isinstance(item, str):
69
+ return item
70
+ return string(str(_CustomRequirement(item)))
71
+
72
+
73
+ class _CustomRequirement(Requirement):
74
+ @override
75
+ def __init__(self, requirement_string: str) -> None:
76
+ super().__init__(requirement_string)
77
+ try:
78
+ parsed = _parse_requirement(requirement_string)
79
+ except ParserSyntaxError as e:
80
+ raise InvalidRequirement(str(e)) from e
81
+ self.specifier = _CustomSpecifierSet(parsed.specifier)
82
+
83
+ @override
84
+ def _iter_parts(self, name: str) -> Iterator[str]:
85
+ yield name
86
+ if self.extras:
87
+ formatted_extras = ",".join(sorted(self.extras))
88
+ yield f"[{formatted_extras}]"
89
+ if self.specifier:
90
+ yield f" {self.specifier}"
91
+ if self.url:
92
+ yield f"@ {self.url}"
93
+ if self.marker:
94
+ yield " "
95
+ if self.marker:
96
+ yield f"; {self.marker}"
97
+
98
+
99
+ class _CustomSpecifierSet(SpecifierSet):
100
+ @override
101
+ def __str__(self) -> str:
102
+ specs = sorted(self._specs, key=self._key)
103
+ return ", ".join(map(str, specs))
104
+
105
+ def _key(self, spec: Specifier, /) -> int:
106
+ return [">=", "<"].index(spec.operator)
107
+
108
+
109
+ __all__ = ["PYPROJECT_TOML", "main"]
@@ -0,0 +1,6 @@
1
+ from __future__ import annotations
2
+
3
+ from pre_commit_hooks.format_requirements import main
4
+
5
+ if __name__ == "__main__":
6
+ raise SystemExit(int(not main()))
@@ -0,0 +1,55 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING, override
4
+
5
+ from click import argument, command
6
+ from libcst import CSTTransformer, Name, Subscript, parse_module
7
+ from libcst.matchers import Index as MIndex
8
+ from libcst.matchers import Name as MName
9
+ from libcst.matchers import Subscript as MSubscript
10
+ from libcst.matchers import SubscriptElement as MSubscriptElement
11
+ from libcst.matchers import matches
12
+ from libcst.metadata import MetadataWrapper
13
+ from utilities.click import FilePath
14
+
15
+ if TYPE_CHECKING:
16
+ from pathlib import Path
17
+
18
+
19
+ @command()
20
+ @argument("paths", nargs=-1, type=FilePath)
21
+ def main(*, paths: tuple[Path, ...]) -> bool:
22
+ """CLI for the `replace_sequence_str` hook."""
23
+ results = list(map(_process, paths))
24
+ return all(results)
25
+
26
+
27
+ def _process(path: Path, /) -> bool:
28
+ existing = path.read_text()
29
+ wrapper = MetadataWrapper(parse_module(existing))
30
+ transformed = wrapper.module.visit(SequenceToListTransformer())
31
+ new = transformed.code
32
+ if existing == new:
33
+ return True
34
+ _ = path.write_text(new)
35
+ return False
36
+
37
+
38
+ class SequenceToListTransformer(CSTTransformer):
39
+ @override
40
+ def leave_Subscript(
41
+ self, original_node: Subscript, updated_node: Subscript
42
+ ) -> Subscript:
43
+ _ = original_node
44
+ if matches(
45
+ updated_node,
46
+ MSubscript(
47
+ value=MName("Sequence"),
48
+ slice=[MSubscriptElement(slice=MIndex(value=MName("str")))],
49
+ ),
50
+ ):
51
+ return updated_node.with_changes(value=Name("list"))
52
+ return updated_node
53
+
54
+
55
+ __all__ = ["main"]
@@ -0,0 +1,6 @@
1
+ from __future__ import annotations
2
+
3
+ from pre_commit_hooks.replace_sequence_str import main
4
+
5
+ if __name__ == "__main__":
6
+ raise SystemExit(int(not main()))
@@ -1,11 +0,0 @@
1
- pre_commit_hooks/__init__.py,sha256=lh8ioZVWPAfeUxjmKuyUNzuciJwPUGFpTLYQARsjt5s,59
2
- pre_commit_hooks/common.py,sha256=sApXaiGpUrPfzlneJQYPYonm8ikWQWNdI6BK7Is8bcA,691
3
- pre_commit_hooks/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- pre_commit_hooks/run_bump_my_version/__init__.py,sha256=9RpVVqTmtmKkV8rMR4sMfI-i_7gaLOmNrfi2GWgtNSk,1953
5
- pre_commit_hooks/run_bump_my_version/__main__.py,sha256=w2V3y61jrSau-zxjl8ciHtWPlJQwXbYxNJ2tGYVyI4s,156
6
- pre_commit_hooks/run_ruff_format/__init__.py,sha256=4QYVE-ktOZjJZGqLMpy4_Te1dASzEkEYVBHzpa1EdGM,2062
7
- pre_commit_hooks/run_ruff_format/__main__.py,sha256=faesqqpMaesg5r-LvkkQt1W9kahvNr-60K3SMYv1NgY,152
8
- dycw_pre_commit_hooks-0.11.0.dist-info/METADATA,sha256=G02WHjfL3dfXNyD9pl0Xj05cTiK-bbUGQrif_pVkbto,777
9
- dycw_pre_commit_hooks-0.11.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
10
- dycw_pre_commit_hooks-0.11.0.dist-info/entry_points.txt,sha256=URODtkWLHrg6PagXnVbubNWnEUjExjCM_lCPf2LEllo,196
11
- dycw_pre_commit_hooks-0.11.0.dist-info/RECORD,,