Skip to content

Commit

Permalink
Fixed 'no code for' error for with and comprehensions (#1883)
Browse files Browse the repository at this point in the history
Not all Python nodes provide `codeAndLocation`. This resulted in warnings which are now prevented by using the code and location from the respective parent nodes.

Co-authored-by: Maximilian Kaul <[email protected]>
  • Loading branch information
oxisto and maximiliankaul authored Dec 5, 2024
1 parent d534bce commit db340a1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,19 @@ class ExpressionHandler(frontend: PythonLanguageFrontend) :
*
* Connects multiple predicates by `and`.
*/
private fun handleComprehension(node: Python.AST.comprehension): ComprehensionExpression {
return newComprehensionExpression(rawNode = node).apply {
private fun handleComprehension(
node: Python.AST.comprehension,
parent: Python.AST.BaseExpr
): ComprehensionExpression {
return newComprehensionExpression(rawNode = parent).apply {
variable = handle(node.target)
iterable = handle(node.iter)
val predicates = node.ifs.map { handle(it) }
if (predicates.size == 1) {
predicate = predicates.single()
} else if (predicates.size > 1) {
predicate =
joinListWithBinOp(operatorCode = "and", nodes = predicates, rawNode = node)
joinListWithBinOp(operatorCode = "and", nodes = predicates, rawNode = parent)
}
if (node.is_async != 0L)
additionalProblems +=
Expand All @@ -104,7 +107,7 @@ class ExpressionHandler(frontend: PythonLanguageFrontend) :
private fun handleGeneratorExp(node: Python.AST.GeneratorExp): CollectionComprehension {
return newCollectionComprehension(rawNode = node).apply {
statement = handle(node.elt)
comprehensionExpressions += node.generators.map { handleComprehension(it) }
comprehensionExpressions += node.generators.map { handleComprehension(it, node) }
type = objectType("Generator")
}
}
Expand All @@ -116,7 +119,7 @@ class ExpressionHandler(frontend: PythonLanguageFrontend) :
private fun handleListComprehension(node: Python.AST.ListComp): CollectionComprehension {
return newCollectionComprehension(rawNode = node).apply {
statement = handle(node.elt)
comprehensionExpressions += node.generators.map { handleComprehension(it) }
comprehensionExpressions += node.generators.map { handleComprehension(it, node) }
type = objectType("list") // TODO: Replace this once we have dedicated types
}
}
Expand All @@ -128,7 +131,7 @@ class ExpressionHandler(frontend: PythonLanguageFrontend) :
private fun handleSetComprehension(node: Python.AST.SetComp): CollectionComprehension {
return newCollectionComprehension(rawNode = node).apply {
this.statement = handle(node.elt)
this.comprehensionExpressions += node.generators.map { handleComprehension(it) }
this.comprehensionExpressions += node.generators.map { handleComprehension(it, node) }
this.type = objectType("set") // TODO: Replace this once we have dedicated types
}
}
Expand All @@ -145,7 +148,7 @@ class ExpressionHandler(frontend: PythonLanguageFrontend) :
value = handle(node.value),
rawNode = node
)
this.comprehensionExpressions += node.generators.map { handleComprehension(it) }
this.comprehensionExpressions += node.generators.map { handleComprehension(it, node) }
this.type = objectType("dict") // TODO: Replace this once we have dedicated types
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) :
base = newReference(name = managerName).implicit()
)
.implicit(),
rawNode = withItem
rawNode = node
)
.implicit()
exitCallWithNone.addArgument(newLiteral(null).implicit())
Expand All @@ -314,7 +314,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) :
base = newReference(name = managerName).implicit()
)
.implicit(),
rawNode = withItem
rawNode = node
)
.implicit()
val starOp = newUnaryOperator("*", false, false)
Expand Down Expand Up @@ -352,7 +352,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) :
base = newReference(name = managerName).implicit()
)
.implicit(),
rawNode = withItem
rawNode = node
)
.implicit()

Expand Down

0 comments on commit db340a1

Please sign in to comment.