Skip to content

fix(pipeline): never bind an external Python import to a same-named project symbol (#2127) - #2345

Open
DeusData wants to merge 2 commits into
mainfrom
fix/issue-2127
Open

DeusData wants to merge 2 commits into
mainfrom
fix/issue-2127

Conversation

@DeusData

Copy link
Copy Markdown
Owner

For Python, an import from a module outside the project (from unittest.mock import patch, import json) could be bound by the import resolver's name-only fallback to any same-named project definition. That gave import_map CALLS edges at 0.95; when the import edge was missing, the weak unique_name/suffix_match fallbacks did the same.

  • Import side (pass_pkgmap.c, shared by both drivers): for external Python imports, a Strategy-3 fallback hit must contain the import's module chain as an ordered subsequence, and module-naming segments only bind Module/File nodes. Project-internal imports and re-exports are unchanged.
  • Call side, identical gates in the sequential and parallel resolvers: weak-strategy CALLS (unique_name, suffix_match, field_type_hint, fuzzy) whose root is bound by an external import and contradict its chain are not emitted. import_map, same_module and lsp_* edges are untouched. Per-language (Python only), keyed on the module chain rather than a name list.

django/django (fresh index each, parallel pipeline): CALLS 62,430 → 60,123, TESTS 29,872 → 28,571, IMPORTS 16,068 → 15,491. A random sample of 25 removed CALLS was 25/25 false (e.g. os.path.join → a template filter, json.loads → signing.loads, mock.patch → RedirectView.patch); 6 IMPORTS were re-targeted to the real definition.

Tests: edge_imports::ei_py_external_import_never_binds_project_symbol (sequential and parallel legs, with a recall pin for re-exports) plus registry unit tests import_binding_suppress_drops_only_weak_contradicted_calls and python_import_binding_contradicts_only_foreign_chains. RED before the fix; RED on revert, and disabling only the call guard also goes RED, so both halves bind.

Known leftovers (not in this PR): a plain import X of a stdlib name can still bind to a nested project module of the same name; internal re-exported names can still get weak false targets for bare calls.


Touches pass_calls.c / pass_parallel.c (one extra operand in drop_plain_call) alongside #2332 and #2339.

Fixes #2127

…roject symbol (#2127)

`from unittest.mock import patch` names a module outside the project.
Strategy 1 of cbm_pipeline_resolve_import_node cannot resolve it, so the
Strategy-3 symbol-name fallback bound the import to the only project
definition whose leaf is `patch` -- a REST view's PkgConfigView.patch --
and every `patch(...)` call became an import_map CALLS edge at confidence
0.95. Once that IMPORTS edge is gone the same call falls through to the
weak short-name strategies (unique_name 0.75, suffix_match), which bind it
again; `mock.patch(...)`, `json.loads(...)`, `os.path.join(...)` reached
project methods the same way, because the member guard exempts
import-rooted receivers.

Two per-language (Python-only) rules, both keyed on the import's module
chain rather than on any spelling list:

- Import side (pass_pkgmap.c, shared by both drivers): for an import
  whose module is not in the project, a Strategy-3 hit must spell the
  import's module chain as an ordered subsequence of its QN (a sys.path
  root or src/ layout the resolver missed still qualifies), and whatever
  names a module (an enclosing path segment, any segment of a plain
  `import x`) may only bind a Module/File node. Imports of project
  modules are unchanged (they may re-export from anywhere).
- Call side (pass_calls.c + pass_parallel.c, identical gates): a call
  whose root identifier is bound by an external import (no IMPORTS edge
  materialized it) and whose weak-strategy target contradicts that
  import's chain is not emitted as a plain CALLS edge. import_map /
  same_module / lsp_* edges are never touched.

django/django (parallel pipeline): CALLS 62,430 -> 60,123, TESTS
29,872 -> 28,571, IMPORTS 16,068 -> 15,491. Removed CALLS are
member calls through stdlib imports (1,900: os.path.join ->
defaultfilters.join, json.loads -> signing.loads, mock.patch ->
RedirectView.patch) and bare calls of stdlib names (398: date ->
defaultfilters.date, urlsplit -> MultiPartParser.parse); a random
25-edge sample was 25/25 false. Six IMPORTS are re-targeted to the real
definition (template_tests.utils.setup, multiple_database TestRouter,
pr_quality errors.Message).

Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
The memory-core ratchet flagged src/pipeline/pass_pkgmap.c growing by 2 raw
allocator sites (82 -> 84). Both are frees of strings returned by
cbm_pipeline_fqn_compute and cbm_pipeline_resolve_module, which the core
never counted, so they now go through safe_free as the rest of the codebase
does rather than raw free or cbm_free. Behaviour is unchanged;
pass_pkgmap.c is back at its baseline of 82.

Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

High-confidence import_map collides on common method names

1 participant