PY-76862 Report forward references in unions

GitOrigin-RevId: 120a79d42e21bb1bd63922cc1d81d3d70ae908c6
This commit is contained in:
Daniil Kalinin
2025-04-24 15:52:02 +02:00
committed by intellij-monorepo-bot
parent 171dcd928c
commit 246fd7cd88
3 changed files with 30 additions and 1 deletions

View File

@@ -1183,6 +1183,7 @@ INSP.type.hints.type.alias.must.be.top.level.declaration=Type alias must be top-
INSP.type.hints.type.alias.invalid.assigned.value=Assigned value of type alias must be a correct type
INSP.type.hints.type.alias.cannot.be.parameterized='TypeAlias' cannot be parameterized
INSP.type.hints.type.hint.is.not.valid=Type hint is invalid or refers to the expression which is not a correct type
INSP.type.hints.forward.reference.in.union=Union type annotations with forward references must be wrapped in quotes entirely
INSP.type.hints.typeIs.has.zero.parameters=User-defined TypeGuard or TypeIs functions must have at least one parameter
INSP.type.hints.typeIs.does.not.match=Return type of TypeIs ''{0}'' is not consistent with the type of the first parameter ''{1}''
INSP.type.hints.self.use.in.staticmethod=Cannot use 'Self' in staticmethod
@@ -1292,4 +1293,4 @@ INSP.NAME.new.type.cannot.be.subclassed=''{0}'' cannot be subclassed
INSP.NAME.new.type.variable.name.does.not.match.new.type.name=Variable name ''{0}'' does not match NewType name ''{1}''
INSP.NAME.new.type.expected.class=Expected class
INSP.NAME.new.type.new.type.cannot.be.used.with=NewType cannot be used with ''{0}''
INSP.NAME.new.type.new.type.cannot.be.generic=NewType cannot be generic
INSP.NAME.new.type.new.type.cannot.be.generic=NewType cannot be generic

View File

@@ -267,6 +267,8 @@ class PyTypeHintsInspection : PyInspection() {
registerProblem(annotationValue, PyPsiBundle.message("INSP.type.hints.type.hint.is.not.valid"))
}
checkForwardReferencesInBinaryExpression(annotationValue)
checkRawConcatenateUsage(annotationValue)
fun PyAnnotation.findSelvesInAnnotation(context: TypeEvalContext): List<PyReferenceExpression> =
@@ -1214,6 +1216,20 @@ class PyTypeHintsInspection : PyInspection() {
}
}
private fun checkForwardReferencesInBinaryExpression(expression: PyExpression) {
if (expression is PyBinaryExpression && expression.operator == PyTokenTypes.OR) {
expression.accept(object : PyRecursiveElementVisitor() {
override fun visitPyStringLiteralExpression(node: PyStringLiteralExpression) {
if (node.parent is PyBinaryExpression) {
registerProblem(node, PyPsiBundle.message("INSP.type.hints.forward.reference.in.union"),
ProblemHighlightType.GENERIC_ERROR)
}
super.visitPyStringLiteralExpression(node)
}
})
}
}
private fun checkTypingMemberParameters(index: PyExpression, isCallable: Boolean) {
val parameters = if (index is PyTupleExpression) index.elements else arrayOf(index)