Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cpp_linter_hooks/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ def get_version_from_dependency(tool: str) -> Optional[str]:
return None
with open(pyproject_path, "rb") as f:
data = tomllib.load(f)
# Check build-system.requires
build_system = data.get("build-system", {})
requires = build_system.get("requires", [])
for dep in requires:
# Check dependencies
dependencies = data.get("dependencies", [])
for dep in dependencies:
if dep.startswith(f"{tool}=="):
return dep.split("==")[1]
return None
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix: read from [project].dependencies (not top-level) and strip env markers.

pyproject.toml stores deps under [project].dependencies. Current lookup uses a missing top-level dependencies key, so defaults will always be None. Also, strip any ; ... markers before matching.

Apply this diff:

-    # Check dependencies
-    dependencies = data.get("dependencies", [])
-    for dep in dependencies:
-        if dep.startswith(f"{tool}=="):
-            return dep.split("==")[1]
+    # Check [project].dependencies
+    dependencies = (data.get("project") or {}).get("dependencies", [])  # PEP 621
+    for dep in dependencies:
+        dep_no_marker = dep.split(";", 1)[0].strip()  # drop env markers/extras
+        if dep_no_marker.startswith(f"{tool}=="):
+            return dep_no_marker.split("==", 1)[1]

Optional hardening (outside this hunk): add a fallback to installed metadata if pyproject.toml isn’t present at runtime.

# at top:
from importlib import metadata as _ilmd

# inside get_version_from_dependency(), before final `return None`:
try:
    dist = _ilmd.distribution("cpp_linter_hooks")
    for req in dist.requires or []:
        req_no_marker = req.split(";", 1)[0].strip()
        if req_no_marker.startswith(f"{tool}=="):
            return req_no_marker.split("==", 1)[1]
except _ilmd.PackageNotFoundError:
    pass
🤖 Prompt for AI Agents
In cpp_linter_hooks/util.py around lines 23 to 28, the function currently reads
top-level "dependencies" (which is absent) and doesn't strip environment
markers, so it always returns None for pyproject.toml deps; modify it to read
data.get("project", {}).get("dependencies", []) instead of
data.get("dependencies", []), and when comparing split any marker by taking
dep.split(";", 1)[0].strip() before checking startswith(f"{tool}==") and
extracting the version. Optionally add the fallback: import importlib.metadata
as _ilmd at top and before returning None try to read installed distribution
requirements (catching _ilmd.PackageNotFoundError) and apply the same
marker-stripping logic to find the dependency version.

Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools>=45", "setuptools-scm", "clang-format==21.1.0", "clang-tidy==21.1.0"]
requires = ["setuptools>=45", "setuptools-scm"]
build-backend = "setuptools.build_meta"

requires-python = ">=3.9"
Expand Down Expand Up @@ -34,6 +34,8 @@ classifiers = [
dependencies = [
"tomli>=1.1.0; python_version < '3.11'",
"setuptools>=45.0.0", # Required for pkg_resources in clang-tidy
"clang-format==21.1.0",
"clang-tidy==21.1.0",
]
dynamic = ["version"]

Expand Down
Loading