Skip to content

Commit f074d5e

Browse files
manage-repositories-app[bot]renovate[bot]myakovernetser
authored
CherryPicked: [v4.20] ci(deps): lock file maintenance (main) (#2715)
* ci(deps): lock file maintenance (main) (#2702) * ci(deps): lock file maintenance * Update Python compatibility markers and modernize code Update resolution markers to support Python 3.12 as primary version with 3.11 as fallback. Modernize Python 3.10+ syntax by replacing typing constructs with built-in generics (Optional → None union, List → list, Tuple → tuple). Update imports to use traceback at module level and refactor error handling from assert to raise AssertionError. Simplify failure summary output by removing truncation logic. Replace uv.lock with updated dependencies reflecting Python version changes. * chore: update pre-commit dependencies Update ruff to v0.15.12, gitleaks to v8.30.1, and mypy to v1.20.2. Also remove unnecessary space in string join operation in test fixture. * Fix CLI tool invocations with missing configuration flags Update pytest, prek, and ruff commands to include their required configuration flags. Add `--group tests` to pytest invocation, add `-c .pre-commit-config.yaml` to prek invocation, and add `--config pyproject.toml` to ruff invocation. Also remove extra space in string join operation for consistency. * test: Update pytest invocation with test dependency group The pytest command in the CLI test now includes the `--group tests` flag to ensure the test dependencies are available when running pytest through the uv package manager. * Refactor imports in class_generator_template.j2 Reorganize imports to improve readability and add conditional import for MissingRequiredArgumentError. The exception import is now only included when there are required arguments that will actually use it, reducing unnecessary dependencies in generated classes with no required arguments. * Remove extra blank line in class_generator_template.j2 * Improve file formatting with prek and ruff fallback Add ruff as a fallback formatter when prek fails. Extract project root path calculation to module level and pass configuration paths explicitly to prek. Introduce _run_ruff_fallback() helper that runs ruff check and ruff format sequentially. Update logging to clarify fallback behavior and fix typo in prek stdout log message. --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Meni Yakove <myakove@gmail.com> * fix: revert ruff to v0.14.2, fix mock tests, regenerate manifests - Revert ruff from v0.15.12 to v0.14.2 in pre-commit config to avoid 90+ new lint errors from stricter rules on maintenance branch - Fix TestCheckAndUpdateClusterVersion mock tests by adding _get_open_mode() helper to handle missing positional mode argument - Regenerate 18 test manifest files to match current generator output Assisted-by: Claude <noreply@anthropic.com> Signed-off-by: rnetser <rnetser@redhat.com> --------- Signed-off-by: rnetser <rnetser@redhat.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Meni Yakove <myakove@gmail.com> Co-authored-by: rnetser <rnetser@redhat.com>
1 parent 33bdbbd commit f074d5e

27 files changed

Lines changed: 102 additions & 43 deletions

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ repos:
5151
- id: ruff-format
5252

5353
- repo: https://github.com/gitleaks/gitleaks
54-
rev: v8.28.0
54+
rev: v8.30.1
5555
hooks:
5656
- id: gitleaks
5757

5858
- repo: https://github.com/pre-commit/mirrors-mypy
59-
rev: v1.18.2
59+
rev: v1.20.2
6060
hooks:
6161
- id: mypy
6262
exclude: ^(tests/|examples/|docs/)

class_generator/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ def handle_test_generation(add_tests: bool) -> None:
450450
import os
451451

452452
test_file = "class_generator/tests/test_class_generator.py"
453-
exit_code = os.system(f"uv run pytest {test_file}")
453+
exit_code = os.system(f"uv run --group tests pytest {test_file}")
454454

455455
# os.system returns the exit status shifted left by 8 bits
456456
if exit_code != 0:
Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
"""File writing utilities for generated code."""
22

3+
from pathlib import Path
4+
35
from pyhelper_utils.shell import run_command
46
from simple_logger.logger import get_logger
57

68
LOGGER = get_logger(name=__name__)
79

10+
# Project root directory (contains pyproject.toml and .pre-commit-config.yaml)
11+
_PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent
12+
813

914
def write_and_format_rendered(filepath: str, output: str) -> None:
1015
"""
11-
Write rendered content to file and format it with prek.
16+
Write rendered content to file and format with prek, falling back to ruff.
1217
1318
Args:
1419
filepath: Path to write the file to
@@ -17,20 +22,50 @@ def write_and_format_rendered(filepath: str, output: str) -> None:
1722
with open(filepath, "w", encoding="utf-8") as fd:
1823
fd.write(output)
1924

20-
# Run prek on the file
25+
pre_commit_config = str(_PROJECT_ROOT / ".pre-commit-config.yaml")
26+
pyproject_toml = str(_PROJECT_ROOT / "pyproject.toml")
27+
28+
# Try prek first (runs all pre-commit hooks including ruff)
29+
prek_success = False
2130
try:
2231
rc, stdout, stderr = run_command(
23-
command=["uvx", "prek", "run", "--files", filepath],
32+
command=["uvx", "prek", "-c", pre_commit_config, "run", "--files", filepath],
2433
verify_stderr=False,
2534
check=False,
2635
log_errors=False,
2736
)
2837
if not rc:
2938
if stderr:
30-
LOGGER.warning(f"Prek hooks failed for {filepath}. This is non-fatal and generation will continue.")
39+
LOGGER.warning(f"Prek hooks failed for {filepath}, falling back to ruff.")
3140
LOGGER.debug(f"prek stderr: {stderr}")
41+
else:
42+
prek_success = True
43+
3244
if stdout:
3345
LOGGER.info(f"{filepath} fixed by prek")
34-
LOGGER.debug(f"rek stdout: {stdout}")
46+
LOGGER.debug(f"prek stdout: {stdout}")
3547
except Exception as e:
36-
LOGGER.error(f"Failed to run prek hooks for {filepath}: {e}. This is non-fatal and generation will continue.")
48+
LOGGER.warning(f"Failed to run prek for {filepath}, falling back to ruff: {e}")
49+
50+
if not prek_success:
51+
_run_ruff_fallback(filepath=filepath, pyproject_toml=pyproject_toml)
52+
53+
54+
def _run_ruff_fallback(filepath: str, pyproject_toml: str) -> None:
55+
"""Run ruff check --fix and ruff format as fallback when prek fails."""
56+
for ruff_cmd in (
57+
["uvx", "ruff", "check", "--fix", "--config", pyproject_toml, filepath],
58+
["uvx", "ruff", "format", "--config", pyproject_toml, filepath],
59+
):
60+
try:
61+
_, stdout, _ = run_command(
62+
command=ruff_cmd,
63+
verify_stderr=False,
64+
check=False,
65+
log_errors=False,
66+
)
67+
if stdout:
68+
LOGGER.info(f"{filepath} fixed by ruff")
69+
LOGGER.debug(f"ruff stdout: {stdout}")
70+
except Exception as e:
71+
LOGGER.error(f"Ruff fallback failed for {filepath}: {e}")

class_generator/manifests/class_generator_template.j2

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
{% endfor %}
1515

1616
from typing import Any
17-
from ocp_resources.resource import {{ base_class }}
17+
18+
{% if all_required_args %}
1819
from ocp_resources.exceptions import MissingRequiredArgumentError
20+
{% endif %}
21+
from ocp_resources.resource import {{ base_class }}
22+
1923

2024
class {{ kind }}({{ base_class }}):
2125
"""

class_generator/tests/manifests/APIServer/api_server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
from typing import Any
5+
56
from ocp_resources.resource import Resource
67

78

class_generator/tests/manifests/ClusterOperator/cluster_operator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
from typing import Any
5+
56
from ocp_resources.resource import Resource
67

78

class_generator/tests/manifests/ConfigMap/config_map.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
from typing import Any
5+
56
from ocp_resources.resource import NamespacedResource
67

78

class_generator/tests/manifests/DNS/dns_config_openshift_io.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
from typing import Any
5+
56
from ocp_resources.resource import Resource
67

78

class_generator/tests/manifests/DNS/dns_operator_openshift_io.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
from typing import Any
5+
56
from ocp_resources.resource import Resource
67

78

class_generator/tests/manifests/Deployment/deployment.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33

44
from typing import Any
5-
from ocp_resources.resource import NamespacedResource
5+
66
from ocp_resources.exceptions import MissingRequiredArgumentError
7+
from ocp_resources.resource import NamespacedResource
78

89

910
class Deployment(NamespacedResource):

0 commit comments

Comments
 (0)