-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pysrc2cpg: expand dynamic type hints based on imports (#2327)
* Add a pass to exapnd type hints * Add test --------- Co-authored-by: Fabian Yamaguchi <[email protected]>
- Loading branch information
Showing
4 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
32 changes: 32 additions & 0 deletions
32
...i/frontends/pysrc2cpg/src/main/scala/io/joern/pysrc2cpg/DynamicTypeHintFullNamePass.scala
This file contains 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,32 @@ | ||
package io.joern.pysrc2cpg | ||
|
||
import io.shiftleft.codepropertygraph.Cpg | ||
import io.shiftleft.codepropertygraph.generated.PropertyNames | ||
import io.shiftleft.passes.CpgPass | ||
import overflowdb.BatchedUpdate | ||
import io.shiftleft.semanticcpg.language._ | ||
|
||
/** The type hints we pick up via the parser are not full names. This pass fixes that by retrieving the import for each | ||
* dynamic type hint and adjusting the dynamic type hint full name field accordingly. | ||
*/ | ||
class DynamicTypeHintFullNamePass(cpg: Cpg) extends CpgPass(cpg) { | ||
override def run(diffGraph: BatchedUpdate.DiffGraphBuilder): Unit = { | ||
val fileToImports = cpg.imports.l | ||
.flatMap { imp => | ||
imp.call.file.l.map { f => f.name -> imp } | ||
} | ||
.groupBy(_._1) | ||
.view | ||
.mapValues(_.map(_._2)) | ||
|
||
for { | ||
methodReturn <- cpg.methodReturn.filter(x => x.dynamicTypeHintFullName.nonEmpty) | ||
typeHint <- methodReturn.dynamicTypeHintFullName | ||
file <- methodReturn.file | ||
imports <- fileToImports.get(file.name) | ||
importedEntity <- imports.importedAsExact(typeHint).importedEntity | ||
} { | ||
diffGraph.setNodeProperty(methodReturn, PropertyNames.DYNAMIC_TYPE_HINT_FULL_NAME, importedEntity) | ||
} | ||
} | ||
} |
This file contains 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
22 changes: 22 additions & 0 deletions
22
...pysrc2cpg/src/test/scala/io/joern/pysrc2cpg/passes/DynamicTypeHintFullNamePassTests.scala
This file contains 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,22 @@ | ||
package io.joern.pysrc2cpg.passes | ||
|
||
import io.joern.pysrc2cpg.PySrc2CpgFixture | ||
import io.shiftleft.semanticcpg.language._ | ||
|
||
class DynamicTypeHintFullNamePassTests extends PySrc2CpgFixture(withOssDataflow = false) { | ||
|
||
"dynamic type hints" should { | ||
lazy val cpg = code(""" | ||
|from foo.bar import Woo | ||
| | ||
|def m() -> Woo: | ||
| x | ||
| | ||
|""".stripMargin) | ||
|
||
"take into accounts imports" in { | ||
cpg.method("m").methodReturn.dynamicTypeHintFullName.l shouldBe List("foo.bar.Woo") | ||
} | ||
} | ||
|
||
} |