refactor python: j2k PyTypeChecker

GitOrigin-RevId: 5bcb5920395cdd03f14b52b530b9cdf547d6b212
This commit is contained in:
Morgan Bartholomew
2026-02-15 12:37:37 +00:00
committed by intellij-monorepo-bot
parent 4436f7d5aa
commit cdad986c9a
5 changed files with 1341 additions and 1315 deletions
@@ -117,6 +117,7 @@ import com.jetbrains.python.psi.types.PySelfType
import com.jetbrains.python.psi.types.PyTupleType
import com.jetbrains.python.psi.types.PyType
import com.jetbrains.python.psi.types.PyTypeChecker
import com.jetbrains.python.psi.types.PyTypeChecker.collectGenerics
import com.jetbrains.python.psi.types.PyTypeParameterMapping
import com.jetbrains.python.psi.types.PyTypeParameterType
import com.jetbrains.python.psi.types.PyTypeParser
@@ -1076,7 +1077,7 @@ class PyTypingTypeProvider : PyTypeProviderWithCustomContext<Context?>() {
.map { getType(it!!, context) }
.map { Ref.deref(it) }
.flatMap {
val typeParams = PyTypeChecker.collectGenerics(it, context.typeContext)
val typeParams = it.collectGenerics(context.typeContext)
StreamEx.of<PyType>(typeParams.typeVars).append(typeParams.typeVarTuples)
.append(StreamEx.of(typeParams.paramSpecs))
}
@@ -2386,9 +2387,9 @@ class PyTypingTypeProvider : PyTypeProviderWithCustomContext<Context?>() {
}
.append(PyTypingTypeProvider().getReturnType(function, context))
.map { Ref.deref(it) }
.map { PyTypeChecker.collectGenerics(it, context) }
.map { it.collectGenerics(context) }
.flatMap {
StreamEx.of<PyTypeParameterType>(it!!.typeVars)
StreamEx.of<PyTypeParameterType>(it.typeVars)
.append(it.paramSpecs)
.append(it.typeVarTuples)
}
@@ -2507,7 +2508,7 @@ class PyTypingTypeProvider : PyTypeProviderWithCustomContext<Context?>() {
if (typeHint is PyReferenceExpression) {
if (assignedType !is PyTypeParameterType) {
val typeAliasTypeParams =
PyTypeChecker.collectGenerics(assignedType, context.typeContext).allTypeParameters
assignedType.collectGenerics(context.typeContext).allTypeParameters
if (!typeAliasTypeParams.isEmpty()) {
return Ref(
PyTypeChecker.parameterizeType(
@@ -97,6 +97,8 @@ import com.jetbrains.python.psi.types.PySelfType
import com.jetbrains.python.psi.types.PyTupleType
import com.jetbrains.python.psi.types.PyType
import com.jetbrains.python.psi.types.PyTypeChecker
import com.jetbrains.python.psi.types.PyTypeChecker.collectGenerics
import com.jetbrains.python.psi.types.PyTypeChecker.hasGenerics
import com.jetbrains.python.psi.types.PyTypeParameterMapping
import com.jetbrains.python.psi.types.PyTypeParameterType
import com.jetbrains.python.psi.types.PyTypeVarTupleType
@@ -607,7 +609,7 @@ class PyTypeHintsInspection : PyInspection() {
if (it != null) {
val type = PyTypingTypeProvider.getType(it, myTypeEvalContext)?.get()
if (PyTypeChecker.hasGenerics(type, myTypeEvalContext)) {
if (type.hasGenerics(myTypeEvalContext)) {
registerProblem(it, PyPsiBundle.message("INSP.type.hints.typevar.constraints.cannot.be.parametrized.by.type.variables"))
}
}
@@ -1230,12 +1232,12 @@ class PyTypeHintsInspection : PyInspection() {
private fun collectTypeParametersFromTypeAlias(assignedValue: PyExpression, assignedValueType: PyType, isExplicitTypeAlias: Boolean): PyTypeChecker.Generics {
if (isExplicitTypeAlias || !(assignedValue is PyReferenceExpression && assignedValueType is PyClassType)) {
return PyTypeChecker.collectGenerics(assignedValueType, myTypeEvalContext)
return assignedValueType.collectGenerics(myTypeEvalContext)
}
else {
val genericDefinitionType = PyTypeChecker.findGenericDefinitionType(assignedValueType.pyClass, myTypeEvalContext)
?: return PyTypeChecker.Generics()
return PyTypeChecker.collectGenerics(genericDefinitionType, myTypeEvalContext)
return genericDefinitionType.collectGenerics(myTypeEvalContext)
}
}
@@ -1323,7 +1325,7 @@ class PyTypeHintsInspection : PyInspection() {
PyPsiBundle.message("INSP.type.hints.default.type.var.cannot.follow.type.var.tuple"),
ProblemHighlightType.GENERIC_ERROR)
}
val genericTypesInDefaultExpr = PyTypeChecker.collectGenerics(Ref.deref(defaultType), myTypeEvalContext)
val genericTypesInDefaultExpr = Ref.deref(defaultType).collectGenerics(myTypeEvalContext)
val defaultOutOfScope = genericTypesInDefaultExpr.allTypeParameters
.firstOrNull { typeVar -> typeVar.declarationElement != null && typeVar.declarationElement !in typeParamDeclarations }
@@ -1554,7 +1556,7 @@ class PyTypeHintsInspection : PyInspection() {
val selfAnnotationValue = selfParameter.annotation?.value ?: return
val selfAnnotationType = PyTypingTypeProvider.getType(selfAnnotationValue, myTypeEvalContext) ?: return
val generics = PyTypeChecker.collectGenerics(selfAnnotationType.get(), myTypeEvalContext)
val generics = selfAnnotationType.get().collectGenerics(myTypeEvalContext)
if (generics.typeVars.any { it.scopeOwner === containingClass }) {
registerProblem(selfAnnotationValue,
PyPsiBundle.message("INSP.type.hints.cannot.use.class.scope.type.variables.in.annotation.for.self.parameter.of__init__"))
@@ -1237,7 +1237,7 @@ private fun PyFunction.matchesByArgumentTypes(callSite: PyCallSiteExpression, co
val receiver = callSite.getReceiver(this)
val mappedExplicitParameters = fullMapping.mappedParameters
val allMappedParameters = LinkedHashMap<PyExpression?, PyCallableParameter?>()
val allMappedParameters = LinkedHashMap<PyExpression, PyCallableParameter>()
val firstImplicit = fullMapping.implicitParameters.firstOrNull()
if (receiver != null && firstImplicit != null) {
allMappedParameters.put(receiver, firstImplicit)
@@ -302,7 +302,7 @@ object PyExpectedTypeJudgement {
mappedParameters: Map<PyExpression, PyCallableParameter>,
ctx: TypeEvalContext,
): PyType? {
if (!hasGenerics(paramType, ctx)) return paramType
if (!paramType.hasGenerics(ctx)) return paramType
val receiver = callSite.getReceiver(null)
val substitutions = unifyGenericCall(receiver, mappedParameters, ctx) // might cause recursion
File diff suppressed because it is too large Load Diff