Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

Fix os.path alias calls in pathlib hook (#4995) (#5203)

Dongping Guo (Donny)364534eda3e3
diff --git a/.pre-commit-hooks/check-pathlib-usage.py b/.pre-commit-hooks/check-pathlib-usage.py
index f2efaf2a28..a01b5908bd 100755
--- a/.pre-commit-hooks/check-pathlib-usage.py
+++ b/.pre-commit-hooks/check-pathlib-usage.py
@@ -22,6 +22,9 @@ class OsPathChecker(ast.NodeVisitor):
# Local names bound to the ``os`` module. Includes aliases from
# ``import os as <alias>`` so they cannot bypass the check.
self.os_module_names: set[str] = set()
+ # Local names bound directly to ``os.path`` by
+ # ``from os import path [as <alias>]``.
+ self.os_path_names: set[str] = set()
self.has_os_path_import = False
def visit_Import(self, node: ast.Import) -> None:
@@ -36,6 +39,9 @@ class OsPathChecker(ast.NodeVisitor):
if node.module == "os" and any(
alias.name == "path" for alias in node.names
):
+ for alias in node.names:
+ if alias.name == "path":
+ self.os_path_names.add(alias.asname or "path")
self.has_os_path_import = True
self.violations.append(
(
@@ -78,27 +84,51 @@ class OsPathChecker(ast.NodeVisitor):
)
self.generic_visit(node)
+ def _get_os_path_call_method(self, func: ast.AST) -> str | None:
+ """Return the os.path method name for recognized call forms."""
+ if not isinstance(func, ast.Attribute):
+ return None
+
+ # os.path.join(...) or _os.path.join(...)
+ if (
+ isinstance(func.value, ast.Attribute)
+ and isinstance(func.value.value, ast.Name)
+ and func.value.value.id in self.os_module_names
+ and func.value.attr == "path"
+ ):
+ return func.attr
+
+ # path.join(...) or _path.join(...) from
+ # ``from os import path [as _path]``.
+ if (
+ isinstance(func.value, ast.Name)
+ and func.value.id in self.os_path_names
+ ):
+ return func.attr
+
+ return None
+
def visit_Call(self, node: ast.Call) -> None:
"""Check for direct calls to os.path functions."""
- if isinstance(node.func, ast.Attribute):
- # Store parent reference for better context
- node.func.parent = node
-
- # Check for os.path.* calls
- if (
- isinstance(node.func.value, ast.Attribute)
- and isinstance(node.func.value.value, ast.Name)
- and node.func.value.value.id in self.os_module_names
- and node.func.value.attr == "path"
- ):
- method = node.func.attr
- # Skip os.path.expandvars as it has no pathlib equivalent
- if method == "expandvars":
- return
+ method = self._get_os_path_call_method(node.func)
+ if method is not None:
+ if method != "expandvars":
suggestion = get_pathlib_equivalent(f"os.path.{method}")
self.violations.append(
- (node.lineno, f"Found os.path.{method}() - {suggestion}")
+ (
+ node.lineno,
+ f"Found os.path.{method}() - {suggestion}",
+ )
)
+
+ # Do not visit node.func here; visit_Attribute would otherwise
+ # report the same os.path.<method>(...) call a second time.
+ for arg in node.args:
+ self.visit(arg)
+ for keyword in node.keywords:
+ self.visit(keyword.value)
+ return
+
self.generic_visit(node)
diff --git a/changelog.d/4995.bugfix.md b/changelog.d/4995.bugfix.md
new file mode 100644
index 0000000000..5878fbea5a
--- /dev/null
+++ b/changelog.d/4995.bugfix.md
@@ -0,0 +1 @@
+The `check-pathlib-usage` pre-commit hook now detects direct `path.<method>()` calls from `from os import path` and `from os import path as <alias>` imports.
diff --git a/tests/hooks/test_check_pathlib_usage.py b/tests/hooks/test_check_pathlib_usage.py
index 6317b3cbad..b926fb82af 100644
--- a/tests/hooks/test_check_pathlib_usage.py
+++ b/tests/hooks/test_check_pathlib_usage.py
@@ -130,3 +130,64 @@ class TestAllowedFilesBypass:
filename="src/local_deep_research/config/paths.py",
)
assert result.returncode == 0
+
+
+class TestFromOsImportPathAlias:
+ """#4995: direct calls through ``from os import path [as <alias>]``."""
+
+ def test_aliased_path_join_is_caught(self):
+ result = _run_hook(
+ "from os import path as _path\nx = _path.join('a', 'b')\n"
+ )
+ assert result.returncode == 1
+ assert "from os import path" in result.stdout
+ assert "os.path.join" in result.stdout
+ assert result.stdout.count(":2:") == 1
+
+ def test_nonaliased_path_join_is_caught(self):
+ result = _run_hook("from os import path\nx = path.join('a', 'b')\n")
+ assert result.returncode == 1
+ assert "from os import path" in result.stdout
+ assert "os.path.join" in result.stdout
+ assert result.stdout.count(":2:") == 1
+
+ def test_aliased_path_exists_is_caught(self):
+ result = _run_hook(
+ "from os import path as p\nif p.exists('/x'):\n pass\n"
+ )
+ assert result.returncode == 1
+ assert "os.path.exists" in result.stdout
+ assert result.stdout.count(":2:") == 1
+
+ def test_aliased_path_expandvars_call_is_not_flagged(self):
+ result = _run_hook(
+ "from os import path as _path\nx = _path.expandvars('$HOME')\n"
+ )
+ assert result.returncode == 1 # import line only
+ assert "from os import path" in result.stdout
+ assert result.stdout.count(":2:") == 0
+ assert "os.path.expandvars" not in result.stdout
+
+
+class TestNoDoubleCounting:
+ """A single os.path call should produce one call violation."""
+
+ def test_plain_os_path_join_reported_once(self):
+ result = _run_hook("import os\nx = os.path.join('a', 'b')\n")
+ assert result.returncode == 1
+ assert "Found os.path.join()" in result.stdout
+ assert result.stdout.count(":2:") == 1
+
+ def test_aliased_os_path_join_reported_once(self):
+ result = _run_hook("import os as _os\nx = _os.path.join('a', 'b')\n")
+ assert result.returncode == 1
+ assert "Found os.path.join()" in result.stdout
+ assert result.stdout.count(":2:") == 1
+
+ def test_join_nested_inside_expandvars_is_still_caught(self):
+ result = _run_hook(
+ "import os\nx = os.path.expandvars(os.path.join('a', 'b'))\n"
+ )
+ assert result.returncode == 1
+ assert "Found os.path.join()" in result.stdout
+ assert result.stdout.count(":2:") == 1