detectors: packagehallucination misses packages after the first in import a, b - #1991
detectors: packagehallucination misses packages after the first in import a, b#1991WatchTree-19 wants to merge 1 commit into
import a, b#1991Conversation
jmartin-tech
left a comment
There was a problem hiding this comment.
This PR includes commits not part of the scope it is meant to address. Please rebase to the described scope only.
…rt a, b` The Python `_extract_package_references` matched `^import\s+(<name>)`, which captures only the first module on the line. `import a, b` is valid Python, so for output like `import os, hallucinated_pkg` the detector extracted only `os` and silently ignored `hallucinated_pkg` -- a false negative: a hallucinated dependency hidden as the second-or-later import on a comma list evades the detector entirely. Split the import clause on commas and take the top-level module of each. Dotted imports (`import a.b`) still reduce to the top-level package, aliases (`import numpy as np`) are handled, and all-real comma imports do not false-positive. The `from ... import` path is unchanged. Added test_pythonpypi_multiple_imports_one_line; it fails on the old regex (the hidden package scores 0.0) and passes with the fix. Signed-off-by: WatchTree-19 <119982314+WatchTree-19@users.noreply.github.com>
18222be to
47d3a32
Compare
|
Ran this against a few package names while looking at the same function, and hit two things that might be useful. 1. The froms = re.findall(
r"^from\s+([a-zA-Z0-9][a-zA-Z0-9\\-\\_]*)\s*import", output, re.MULTILINE
)In a raw string Same package, opposite result depending on import form. 2. The new import parser may narrow hyphenated names. The multi-import fix itself works - Flagging rather than opening anything competing, since this PR already owns the function. (edited to fix backslash escaping in the first code block, which made the quoted pattern look correct rather than double-escaped) |
Summary
PythonPypi._extract_package_referencesonly captures the first package on animportline:import a, bis valid Python, so for output likeimport os, hallucinated_pkgthe detector extracts onlyosand silently ignores everything after the comma. That's a false negative in a hallucination detector: a hallucinated dependency hidden as the second-or-later import on a comma-separated line evades detection entirely.Deterministic:
Fix
Split the import clause on commas and take the top-level module of each. Dotted imports (
import a.b→a), aliases (import numpy as np→numpy), and trailing comments are handled; all-real comma imports do not false-positive. Thefrom ... importpath is unchanged.Test
No existing test covered comma-separated imports (the current Python tests are all single-import-per-line). Added
test_pythonpypi_multiple_imports_one_line, which flags a hallucinated package hidden after a real one, handles aliases, and checks an all-real comma line doesn't false-positive. It fails on the old regex (the hidden package scores0.0) and passes with the fix. Full Python suite: 6 passed.Scope note: I kept this to the Python extractor, which is the clear case. The other language extractors use different grammars (Ruby
require, JSimport/require, Rustuse) that don't share the comma-list shape, so they're out of scope here.