mirror of
https://github.com/github/codeql.git
synced 2026-03-21 15:06:46 +01:00
This turned out to be fairly simple. Given an import such as ```python from foo.bar.baz import quux ``` we create an API-graph node for each valid dotted prefix of `foo.bar.baz`, i.e. `foo`, `foo.bar`, and `foo.bar.baz`. For these, we then insert nodes in the API graph, such that `foo` steps to `foo.bar` along an edge labeled `bar`, etc. Finally, we only allow undotted names to hang off of the API-graph root. Thus, `foo` will have a `moduleImport` edge off of the root, and a `getMember` edge for `bar` (which in turn has a `getMember` edge for `baz`). Relative imports are explicitly ignored. Finally, this commit also adds inline tests for a variety of ways of importing modules, including a copy of the "import-helper" tests (with a few modifications to allow a single annotation per line, as these get rather long quickly!).
34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
import a1 #$ use=moduleImport("a1")
|
|
|
|
x = a1.blah1 #$ use=moduleImport("a1").getMember("blah1")
|
|
|
|
import a2 as m2 #$ use=moduleImport("a2")
|
|
|
|
x2 = m2.blah2 #$ use=moduleImport("a2").getMember("blah2")
|
|
|
|
import a3.b3 as m3 #$ use=moduleImport("a3").getMember("b3")
|
|
|
|
x3 = m3.blah3 #$ use=moduleImport("a3").getMember("b3").getMember("blah3")
|
|
|
|
from a4.b4 import c4 as m4 #$ use=moduleImport("a4").getMember("b4").getMember("c4")
|
|
|
|
x4 = m4.blah4 #$ use=moduleImport("a4").getMember("b4").getMember("c4").getMember("blah4")
|
|
|
|
import a.b.c.d #$ use=moduleImport("a")
|
|
|
|
ab = a.b #$ use=moduleImport("a").getMember("b")
|
|
|
|
abc = ab.c #$ use=moduleImport("a").getMember("b").getMember("c")
|
|
|
|
abcd = abc.d #$ use=moduleImport("a").getMember("b").getMember("c").getMember("d")
|
|
|
|
x5 = abcd() #$ use=moduleImport("a").getMember("b").getMember("c").getMember("d").getReturn()
|
|
|
|
y5 = x5.method() #$ use=moduleImport("a").getMember("b").getMember("c").getMember("d").getReturn().getMember("method").getReturn()
|
|
|
|
|
|
# Relative imports. These are ignored
|
|
|
|
from .foo import bar
|
|
|
|
from ..foobar import baz |