-
Notifications
You must be signed in to change notification settings - Fork 2k
Python: Fix resolution of relative imports from namespace packages #21459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
48bf4fd
Python: Add test for missing relative import in namespace packages
tausbn e16bb22
Python: Fix resolution of relative imports from namespace packages
tausbn 3ee369b
Python: Add change note
tausbn e707275
Python: Rename `prints` tag to `flow`
tausbn 92718a9
Python: Add test for package inside namespace package
tausbn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
python/ql/lib/change-notes/2026-03-11-fix-unresolved-relative-imports.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| category: fix | ||
| --- | ||
|
|
||
| - Fixed the resolution of relative imports such as `from . import helper` inside namespace packages (directories without an `__init__.py` file), which previously did not work correctly, leading to missing flow. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
python/ql/test/experimental/import-resolution-namespace-relative/pkg/caller.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from . import helper | ||
|
|
||
| def use_relative(): | ||
| tainted = source() | ||
| helper.process(tainted) |
2 changes: 2 additions & 0 deletions
2
python/ql/test/experimental/import-resolution-namespace-relative/pkg/helper.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| def process(value): | ||
| sink(value) #$ prints=source |
1 change: 1 addition & 0 deletions
1
python/ql/test/experimental/import-resolution-namespace-relative/test.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
35 changes: 35 additions & 0 deletions
35
python/ql/test/experimental/import-resolution-namespace-relative/test.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import python | ||
| import semmle.python.dataflow.new.DataFlow | ||
| import semmle.python.dataflow.new.TaintTracking | ||
| import utils.test.InlineExpectationsTest | ||
|
|
||
| private module TestConfig implements DataFlow::ConfigSig { | ||
| predicate isSource(DataFlow::Node node) { | ||
| node.(DataFlow::CallCfgNode).getFunction().asCfgNode().(NameNode).getId() = "source" | ||
| } | ||
|
|
||
| predicate isSink(DataFlow::Node node) { | ||
| exists(DataFlow::CallCfgNode call | | ||
| call.getFunction().asCfgNode().(NameNode).getId() = "sink" and | ||
| node = call.getArg(0) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private module TestFlow = TaintTracking::Global<TestConfig>; | ||
|
|
||
| module FlowTest implements TestSig { | ||
| string getARelevantTag() { result = "prints" } | ||
|
|
||
| predicate hasActualResult(Location location, string element, string tag, string value) { | ||
| exists(DataFlow::Node sink | | ||
| TestFlow::flow(_, sink) and | ||
| tag = "prints" and | ||
| location = sink.getLocation() and | ||
| value = "source" and | ||
| element = sink.toString() | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| import MakeTest<FlowTest> | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.