Bladeren bron

IT IS POSSIBLE

Khaleel Al-Adhami 1 week geleden
bovenliggende
commit
6f34a6912b
4 gewijzigde bestanden met toevoegingen van 145 en 90 verwijderingen
  1. 0 48
      .pre-commit-config.yaml
  2. 52 3
      pyproject.toml
  3. 92 27
      scripts/hatch_build.py
  4. 1 12
      uv.lock

+ 0 - 48
.pre-commit-config.yaml

@@ -1,48 +0,0 @@
-fail_fast: true
-
-repos:
-  - repo: https://github.com/astral-sh/ruff-pre-commit
-    rev: v0.11.8
-    hooks:
-      - id: ruff-format
-        args: [reflex, tests]
-      - id: ruff
-        args: ["--fix", "--exit-non-zero-on-fix"]
-        exclude: "^integration/benchmarks/"
-
-  - repo: https://github.com/codespell-project/codespell
-    rev: v2.4.1
-    hooks:
-      - id: codespell
-        args: ["reflex"]
-        additional_dependencies:
-          - tomli
-
-  # Run pyi check before pyright because pyright can fail if pyi files are wrong.
-  - repo: local
-    hooks:
-      - id: update-pyi-files
-        name: update-pyi-files
-        always_run: true
-        language: system
-        require_serial: true
-        description: "Update pyi files as needed"
-        entry: python3 scripts/make_pyi.py
-
-  - repo: https://github.com/RobertCraigie/pyright-python
-    rev: v1.1.400
-    hooks:
-      - id: pyright
-        args: [reflex, tests]
-        language: system
-
-  - repo: https://github.com/terrencepreilly/darglint
-    rev: v1.8.1
-    hooks:
-      - id: darglint
-        exclude: "^reflex/reflex.py"
-  - repo: https://github.com/pre-commit/mirrors-prettier
-    rev: f62a70a3a7114896b062de517d72829ea1c884b6
-    hooks:
-      - id: prettier
-        require_serial: true

+ 52 - 3
pyproject.toml

@@ -73,7 +73,7 @@ artifacts = ["*.pyi"]
 
 [tool.hatch.build.hooks.custom]
 path = "scripts/hatch_build.py"
-dependencies = ["plotly", "ruff", "pre_commit", "diff_match_patch"]
+dependencies = ["plotly", "ruff", "pre_commit", "diff_match_patch", "toml"]
 require-runtime-dependencies = true
 
 [tool.pyright]
@@ -142,7 +142,6 @@ dev = [
   "asynctest >=0.13",
   "darglint >=1.8",
   "dill >=0.3",
-  "diff-match-patch >=20241021",
   "hatchling >=1.27",
   "libsass >=0.23",
   "numpy >=2.2",
@@ -150,7 +149,7 @@ dev = [
   "pillow >=11",
   "playwright >=1.51",
   "plotly >=6.0",
-  "pre-commit >=4.2",
+  "pre-commit ==4.2.0",
   "psycopg[binary] >=3.2",
   "pyright >=1.1.400",
   "pytest >=8.3",
@@ -208,3 +207,53 @@ exclude_also = [
 
 [tool.coverage.html]
 directory = "coverage_html_report"
+
+[tool.pre-commit]
+fail_fast = true
+
+[[tool.pre-commit.repos]]
+repo = "https://github.com/astral-sh/ruff-pre-commit"
+rev = "v0.11.8"
+hooks = [
+  { id = "ruff-format", args = [
+    "reflex",
+    "tests",
+  ] },
+  { id = "ruff", args = [
+    "--fix",
+    "--exit-non-zero-on-fix",
+  ], exclude = "^integration/benchmarks/" },
+]
+
+[[tool.pre-commit.repos]]
+repo = "https://github.com/codespell-project/codespell"
+rev = "v2.4.1"
+hooks = [
+  { id = "codespell", args = [
+    "reflex",
+  ], additional_dependencies = [
+    "tomli",
+  ] },
+]
+
+# Run pyi check before pyright because pyright can fail if pyi files are wrong.
+[[tool.pre-commit.repos]]
+repo = "local"
+hooks = [
+  { id = "update-pyi-files", name = "update-pyi-files", always_run = true, language = "system", require_serial = true, description = "Update pyi files as needed", entry = "python3 scripts/make_pyi.py" },
+]
+
+[[tool.pre-commit.repos]]
+repo = "https://github.com/RobertCraigie/pyright-python"
+rev = "v1.1.400"
+hooks = [{ id = "pyright", args = ["reflex", "tests"], language = "system" }]
+
+[[tool.pre-commit.repos]]
+repo = "https://github.com/terrencepreilly/darglint"
+rev = "v1.8.1"
+hooks = [{ id = "darglint", exclude = "^reflex/reflex.py" }]
+
+[[tool.pre-commit.repos]]
+repo = "https://github.com/pre-commit/mirrors-prettier"
+rev = "f62a70a3a7114896b062de517d72829ea1c884b6"
+hooks = [{ id = "prettier", require_serial = true }]

+ 92 - 27
scripts/hatch_build.py

@@ -34,39 +34,104 @@ class CustomBuilder(BuildHookInterface):
             version: The version of the package.
             build_data: The build data.
             artifact_path: The path to the artifact.
+
+        Raises:
+            RuntimeError: If the pre-commit patches are not applied correctly.
         """
         if self.marker().exists():
             return
 
         if importlib.util.find_spec("pre_commit"):
+            import pre_commit.constants
             import pre_commit.yaml
-            from diff_match_patch import diff_match_patch
-
-            patch = """@@ -82,16 +82,28 @@
- ort yaml
-+%0Aimport toml
- %0A%0ALoader
-@@ -209,24 +209,28 @@
- der=Loader)%0A
-+def 
- yaml_load = 
-@@ -226,37 +226,145 @@
- aml_load
-- = functools.partial(
-+(stream):%0A    try:%0A        return toml.loads(stream).get(%22tool%22, %7B%7D).get(%22pre-commit%22, %7B%7D)%0A    except ValueError:%0A        return 
- yaml.loa
-@@ -364,16 +364,23 @@
- aml.load
-+(stream
- , Loader
-"""  # noqa: W291
-
-            dmp = diff_match_patch()
-            patches = dmp.patch_fromText(patch)
-            new_text, _ = dmp.patch_apply(
-                patches, pathlib.Path(pre_commit.yaml.__file__).read_text()
-            )
-            pathlib.Path(pre_commit.yaml.__file__).write_text(new_text)
+
+            patches = [
+                (
+                    pre_commit.constants.__file__,
+                    """from __future__ import annotations
+
+import importlib.metadata
+
+CONFIG_FILE = '.pre-commit-config.yaml'
+MANIFEST_FILE = '.pre-commit-hooks.yaml'
+
+# Bump when modifying `empty_template`
+LOCAL_REPO_VERSION = '1'
+
+VERSION = importlib.metadata.version('pre_commit')
+
+DEFAULT = 'default'""",
+                    """from __future__ import annotations
+
+import importlib.metadata
+
+CONFIG_FILE = 'pyproject.toml'
+MANIFEST_FILE = '.pre-commit-hooks.yaml'
+
+# Bump when modifying `empty_template`
+LOCAL_REPO_VERSION = '1'
+
+VERSION = importlib.metadata.version('pre_commit')
+
+DEFAULT = 'default'""",
+                ),
+                (
+                    pre_commit.yaml.__file__,
+                    """from __future__ import annotations
+
+import functools
+from typing import Any
+
+import yaml
+
+Loader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)
+yaml_compose = functools.partial(yaml.compose, Loader=Loader)
+yaml_load = functools.partial(yaml.load, Loader=Loader)
+Dumper = getattr(yaml, 'CSafeDumper', yaml.SafeDumper)
+
+
+def yaml_dump(o: Any, **kwargs: Any) -> str:
+    # when python/mypy#1484 is solved, this can be `functools.partial`
+    return yaml.dump(
+        o, Dumper=Dumper, default_flow_style=False, indent=4, sort_keys=False,
+        **kwargs,
+    )""",
+                    """from __future__ import annotations
+
+import functools
+from typing import Any
+
+import yaml
+import toml
+
+Loader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)
+yaml_compose = functools.partial(yaml.compose, Loader=Loader)
+def yaml_load(stream):
+    try:
+        return toml.loads(stream).get("tool", {}).get("pre-commit", {})
+    except ValueError:
+        return yaml.load(stream, Loader=Loader)
+Dumper = getattr(yaml, 'CSafeDumper', yaml.SafeDumper)
+
+
+def yaml_dump(o: Any, **kwargs: Any) -> str:
+    # when python/mypy#1484 is solved, this can be `functools.partial`
+    return yaml.dump(
+        o, Dumper=Dumper, default_flow_style=False, indent=4, sort_keys=False,
+        **kwargs,
+    )""",
+                ),
+            ]
+
+            for file, old, new in patches:
+                file_path = pathlib.Path(file)
+                file_content = file_path.read_text()
+                if file_content != new and file_content != old:
+                    raise RuntimeError(
+                        f"Unexpected content in {file_path}. Did you update pre-commit without updating the patches?"
+                    )
+                if file_content == old:
+                    file_path.write_text(new)
 
         if not (pathlib.Path(self.root) / "scripts").exists():
             return

+ 1 - 12
uv.lock

@@ -312,15 +312,6 @@ wheels = [
     { url = "https://files.pythonhosted.org/packages/69/28/85d1e0396d64422c5218d68e5cdcc53153aa8a2c83c7dbc3ee1502adf3a1/darglint-1.8.1-py3-none-any.whl", hash = "sha256:5ae11c259c17b0701618a20c3da343a3eb98b3bc4b5a83d31cdd94f5ebdced8d", size = 120767, upload-time = "2021-10-18T03:40:35.034Z" },
 ]
 
-[[package]]
-name = "diff-match-patch"
-version = "20241021"
-source = { registry = "https://pypi.org/simple" }
-sdist = { url = "https://files.pythonhosted.org/packages/0e/ad/32e1777dd57d8e85fa31e3a243af66c538245b8d64b7265bec9a61f2ca33/diff_match_patch-20241021.tar.gz", hash = "sha256:beae57a99fa48084532935ee2968b8661db861862ec82c6f21f4acdd6d835073", size = 39962, upload-time = "2024-10-21T19:41:21.094Z" }
-wheels = [
-    { url = "https://files.pythonhosted.org/packages/f7/bb/2aa9b46a01197398b901e458974c20ed107935c26e44e37ad5b0e5511e44/diff_match_patch-20241021-py3-none-any.whl", hash = "sha256:93cea333fb8b2bc0d181b0de5e16df50dd344ce64828226bda07728818936782", size = 43252, upload-time = "2024-10-21T19:41:19.914Z" },
-]
-
 [[package]]
 name = "dill"
 version = "0.4.0"
@@ -1557,7 +1548,6 @@ dependencies = [
 dev = [
     { name = "asynctest" },
     { name = "darglint" },
-    { name = "diff-match-patch" },
     { name = "dill" },
     { name = "hatchling" },
     { name = "libsass" },
@@ -1612,7 +1602,6 @@ requires-dist = [
 dev = [
     { name = "asynctest", specifier = ">=0.13" },
     { name = "darglint", specifier = ">=1.8" },
-    { name = "diff-match-patch", specifier = ">=20241021" },
     { name = "dill", specifier = ">=0.3" },
     { name = "hatchling", specifier = ">=1.27" },
     { name = "libsass", specifier = ">=0.23" },
@@ -1621,7 +1610,7 @@ dev = [
     { name = "pillow", specifier = ">=11" },
     { name = "playwright", specifier = ">=1.51" },
     { name = "plotly", specifier = ">=6.0" },
-    { name = "pre-commit", specifier = ">=4.2" },
+    { name = "pre-commit", specifier = "==4.2.0" },
     { name = "psycopg", extras = ["binary"], specifier = ">=3.2" },
     { name = "pyright", specifier = ">=1.1.400" },
     { name = "pytest", specifier = ">=8.3" },