-
Notifications
You must be signed in to change notification settings - Fork 65
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
Add On Import Translation of Python Files in Include Path #1964
base: main
Are you sure you want to change the base?
Conversation
…the interfac file is a/b.py or a/b.pyi. or if the import statement looks like from a import b
…ing imports on a component level
…a condition to decide when the on demand inclusion should be performed
…hofer-AISEC/cpg into feature/import-external-interfaces
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there is some discussion needed before merging this into main
. See internal chat + comments here.
@@ -140,15 +142,24 @@ private constructor( | |||
result: TranslationResult, | |||
): Set<LanguageFrontend<*, *>> { | |||
val usedFrontends = mutableSetOf<LanguageFrontend<*, *>>() | |||
|
|||
val externalSources: MutableList<File> = mutableListOf() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we please have some doc here? What are these variables used for?
return usedFrontends | ||
} | ||
|
||
private fun extractConfiguredSources(path: Path): MutableList<File> { | ||
val rootFile = path.toFile() | ||
return if (rootFile.exists()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please refactor this code. Add some {
as a start.
} | ||
.toMutableList() | ||
|
||
val importRe = "(?m)^(?:from[ ]+(\\S+)[ ]+)?import[ ]+(\\S+)[ ]*\$".toRegex() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I object having this regex here.
- It's not documented.
- I don't like parsing the sources manually with a regex -> can't we use the
ast.parse
result for this? - Can we please name groups, if we have to use a regex? This would make it much easier to understand what's going on.
- What about
import foo
without thefrom
part? - What about
as
in the import?
importRe.findAll(source.readText()).forEach { imp -> | ||
|
||
// Only try to find file containing imports if we did not process this import so far | ||
if (!processedImports.contains(imp.groupValues.get(0))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this approach works. An from foo ...
import can refer to different imports, depending on the location of the file you're parsing here.
…extLanguageFrontend
The PR adds a preprocessing function to all languages that is supposed to return files located in the includePaths that are supposed to be analyzed under their own component.
The current implementation for python identifies which file in the import paths should be used to fulfill which import statement and then recursively resolves additional imports in that file.