mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
cleanup [python]: Convert isProtocol to extention function
GitOrigin-RevId: 9a6b4b14f52ab44644eedd2d122f2067d7593ad7
This commit is contained in:
committed by
intellij-monorepo-bot
parent
923c08b3f7
commit
d9b6a0fa68
+3
-7
@@ -3,12 +3,8 @@ package com.jetbrains.python.codeInsight.stdlib
|
||||
import com.jetbrains.python.codeInsight.PyDataclassParameters
|
||||
import com.jetbrains.python.codeInsight.parseDataclassParameters
|
||||
import com.jetbrains.python.codeInsight.typing.isProtocol
|
||||
import com.jetbrains.python.psi.types.PyClassType
|
||||
import com.jetbrains.python.psi.types.PyType
|
||||
import com.jetbrains.python.psi.types.PyTypeChecker
|
||||
import com.jetbrains.python.psi.types.PyTypeCheckerExtension
|
||||
import com.jetbrains.python.psi.types.TypeEvalContext
|
||||
import java.util.Optional
|
||||
import com.jetbrains.python.psi.types.*
|
||||
import java.util.*
|
||||
|
||||
class PyDataclassInstanceProtocolTypeCheckerExtension : PyTypeCheckerExtension {
|
||||
override fun match(
|
||||
@@ -17,7 +13,7 @@ class PyDataclassInstanceProtocolTypeCheckerExtension : PyTypeCheckerExtension {
|
||||
context: TypeEvalContext,
|
||||
substitutions: PyTypeChecker.GenericSubstitutions,
|
||||
): Optional<Boolean> {
|
||||
if (expected is PyClassType && actual is PyClassType && expected.pyClass.name == "DataclassInstance" && isProtocol(expected, context)) {
|
||||
if (expected is PyClassType && actual is PyClassType && expected.pyClass.name == "DataclassInstance" && expected.isProtocol(context)) {
|
||||
return Optional.of(parseDataclassParameters(actual.pyClass, context)?.type == PyDataclassParameters.PredefinedType.STD)
|
||||
}
|
||||
return Optional.empty()
|
||||
|
||||
@@ -13,9 +13,9 @@ import com.jetbrains.python.psi.resolve.PyResolveContext
|
||||
import com.jetbrains.python.psi.types.*
|
||||
|
||||
|
||||
fun isProtocol(classLikeType: PyClassLikeType, context: TypeEvalContext): Boolean = containsProtocol(classLikeType.getSuperClassTypes(context))
|
||||
fun PyClassLikeType.isProtocol(context: TypeEvalContext): Boolean = containsProtocol(getSuperClassTypes(context))
|
||||
|
||||
fun isProtocol(cls: PyClass, context: TypeEvalContext): Boolean = containsProtocol(cls.getSuperClassTypes(context))
|
||||
fun PyClass.isProtocol(context: TypeEvalContext): Boolean = containsProtocol(getSuperClassTypes(context))
|
||||
|
||||
fun PyClassType.isRuntimeCheckable(context: TypeEvalContext): Boolean =
|
||||
PyKnownDecoratorUtil.getKnownDecorators(pyClass, context).any {
|
||||
@@ -26,8 +26,8 @@ fun matchingProtocolDefinitions(expected: PyType?, actual: PyType?, context: Typ
|
||||
actual is PyClassLikeType &&
|
||||
expected.isDefinition &&
|
||||
actual.isDefinition &&
|
||||
isProtocol(expected, context) &&
|
||||
isProtocol(actual, context)
|
||||
expected.isProtocol(context) &&
|
||||
actual.isProtocol(context)
|
||||
|
||||
typealias ProtocolAndSubclassElements = Pair<PyTypeMember, List<PyTypeMember>>
|
||||
|
||||
@@ -37,7 +37,7 @@ fun inspectProtocolSubclass(protocol: PyClassType, subclass: PyClassType, contex
|
||||
|
||||
val protocolMembers = protocol.toInstance().getAllMembers(resolveContext)
|
||||
val superClassesMembers = protocol.toInstance().getSuperClassTypes(context)
|
||||
.filter { isProtocol(it, context) }
|
||||
.filter { it.isProtocol(context) }
|
||||
.flatMap { it.toInstance().getAllMembers(resolveContext).asIterable() }
|
||||
protocolMembers.addAll(superClassesMembers)
|
||||
|
||||
@@ -45,7 +45,7 @@ fun inspectProtocolSubclass(protocol: PyClassType, subclass: PyClassType, contex
|
||||
val protocolElement = protocolMember.element ?: continue
|
||||
if (protocolElement is PyPossibleClassMember) {
|
||||
val cls = protocolElement.containingClass
|
||||
if (cls != null && !isProtocol(cls, context)) {
|
||||
if (cls != null && !cls.isProtocol(context)) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -18,7 +18,7 @@ import com.jetbrains.python.psi.impl.PyClassImpl
|
||||
import com.jetbrains.python.psi.types.TypeEvalContext
|
||||
import com.jetbrains.python.pyi.PyiFile
|
||||
import com.jetbrains.python.pyi.PyiUtil
|
||||
import java.util.EnumSet
|
||||
import java.util.*
|
||||
|
||||
class PyOverloadsInspection : PyInspection() {
|
||||
|
||||
@@ -66,7 +66,7 @@ class PyOverloadsInspection : PyInspection() {
|
||||
requiresImplementation = false
|
||||
}
|
||||
else if (owner is PyClass) {
|
||||
if (isProtocol(owner, myTypeEvalContext)) {
|
||||
if (owner.isProtocol(myTypeEvalContext)) {
|
||||
requiresImplementation = false
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -57,7 +57,7 @@ class PyProtocolInspection : PyInspection() {
|
||||
superClassTypes
|
||||
.asSequence()
|
||||
.filterIsInstance<PyClassType>()
|
||||
.filter { isProtocol(it, myTypeEvalContext) }
|
||||
.filter { it.isProtocol(myTypeEvalContext) }
|
||||
.forEach { protocol ->
|
||||
inspectProtocolSubclass(protocol, type, myTypeEvalContext).forEach {
|
||||
val subclassElements = it.second
|
||||
@@ -69,7 +69,7 @@ class PyProtocolInspection : PyInspection() {
|
||||
}
|
||||
|
||||
private fun checkProtocolBases(type: PyClassType, superClassTypes: List<PyClassLikeType?>) {
|
||||
if (!isProtocol(type, myTypeEvalContext)) return
|
||||
if (!type.isProtocol(myTypeEvalContext)) return
|
||||
|
||||
val correctBase: (PyClassLikeType?) -> Boolean = {
|
||||
if (it == null) true
|
||||
@@ -78,7 +78,7 @@ class PyProtocolInspection : PyInspection() {
|
||||
|
||||
classQName == PyTypingTypeProvider.PROTOCOL ||
|
||||
classQName == PyTypingTypeProvider.PROTOCOL_EXT ||
|
||||
it is PyClassType && isProtocol(it, myTypeEvalContext)
|
||||
it is PyClassType && it.isProtocol(myTypeEvalContext)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ class PyProtocolInspection : PyInspection() {
|
||||
}
|
||||
|
||||
val type = myTypeEvalContext.getType(base)
|
||||
if (type is PyClassType && isProtocol(type, myTypeEvalContext) && !type.isRuntimeCheckable(myTypeEvalContext)) {
|
||||
if (type is PyClassType && type.isProtocol(myTypeEvalContext) && !type.isRuntimeCheckable(myTypeEvalContext)) {
|
||||
registerProblem(base,
|
||||
PyPsiBundle.message("INSP.protocol.only.runtime.checkable.protocols.can.be.used.with.instance.class.checks"),
|
||||
GENERIC_ERROR)
|
||||
@@ -114,7 +114,7 @@ class PyProtocolInspection : PyInspection() {
|
||||
val base = node.arguments.getOrNull(1)
|
||||
if (base != null) {
|
||||
val type = myTypeEvalContext.getType(base)
|
||||
if (type is PyClassLikeType && isProtocol(type, myTypeEvalContext)) {
|
||||
if (type is PyClassLikeType && type.isProtocol(myTypeEvalContext)) {
|
||||
registerProblem(base, PyPsiBundle.message("INSP.protocol.newtype.cannot.be.used.with.protocol.classes"))
|
||||
}
|
||||
}
|
||||
@@ -150,7 +150,7 @@ class PyProtocolInspection : PyInspection() {
|
||||
val resolveResult = calleeReferenceExpression.followAssignmentsChain(resolveContext)
|
||||
val cls = resolveResult.getElement()
|
||||
if (cls is PyClass) {
|
||||
if (isProtocol(cls, myTypeEvalContext)) {
|
||||
if (cls.isProtocol(myTypeEvalContext)) {
|
||||
registerProblem(node, PyPsiBundle.message("INSP.protocol.cannot.instantiate.protocol.class", cls.name))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,7 +235,7 @@ class PyTypedDictType @JvmOverloads constructor(
|
||||
&& (elementTypes[1] == null || PyNames.OBJECT == elementTypes[1].name)
|
||||
}
|
||||
|
||||
if (expected is PyClassLikeType && isProtocol(expected, context)) {
|
||||
if (expected is PyClassLikeType && expected.isProtocol(context)) {
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user