Skip to content

Commit

Permalink
Merge pull request #661 from koxudaxi/use_multiResolveCalleeFunction_…
Browse files Browse the repository at this point in the history
…instead_of_getResolvedPsiElements

Use multiResolveCalleeFunction instead of getResolvedPsiElements
  • Loading branch information
koxudaxi committed Mar 1, 2023
2 parents c65e94f + 81f6b3a commit 89081b9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Optimize resolving pydantic class [[#658](https://github.com/koxudaxi/pydantic-pycharm-plugin/pull/658)]
- Improve dynamic model field detection [[#659](https://github.com/koxudaxi/pydantic-pycharm-plugin/pull/659)]
- Improve test coverage [[#660](https://github.com/koxudaxi/pydantic-pycharm-plugin/pull/660)]
- Use multiResolveCalleeFunction instead of getResolvedPsiElements [[#661](https://github.com/koxudaxi/pydantic-pycharm-plugin/pull/661)]

## 0.3.17 - 2022-12-16
- Support Union operator [[#602](https://github.com/koxudaxi/pydantic-pycharm-plugin/pull/602)]
Expand Down
14 changes: 6 additions & 8 deletions src/com/koxudaxi/pydantic/Pydantic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,9 @@ internal fun isPydanticRegex(stringLiteralExpression: StringLiteralExpression):
val pyKeywordArgument = stringLiteralExpression.parent as? PyKeywordArgument ?: return false
if (pyKeywordArgument.keyword != "regex") return false
val pyCallExpression = pyKeywordArgument.parent.parent as? PyCallExpression ?: return false
val referenceExpression = pyCallExpression.callee as? PyReferenceExpression ?: return false
val context = TypeEvalContext.userInitiated(referenceExpression.project, referenceExpression.containingFile)
return getResolvedPsiElements(referenceExpression, context)
.filterIsInstance<PyFunction>().any { pyFunction -> pyFunction.isPydanticField || pyFunction.isConStr || pyFunction.isCustomModelField }
val context = TypeEvalContext.userInitiated(pyCallExpression.project, pyCallExpression.containingFile)
return pyCallExpression.multiResolveCalleeFunction(PyResolveContext.defaultContext(context)).filterIsInstance<PyFunction>()
.any { pyFunction -> pyFunction.isPydanticField || pyFunction.isConStr || pyFunction.isCustomModelField }
}

internal fun getClassVariables(pyClass: PyClass, context: TypeEvalContext): Sequence<PyTargetExpression> {
Expand Down Expand Up @@ -586,11 +585,10 @@ internal fun getFieldFromPyExpression(
context: TypeEvalContext,
pydanticVersion: KotlinVersion?,
): PyCallExpression? {
val callee = (psiElement as? PyCallExpression)
?.let { it.callee as? PyReferenceExpression }
?: return null
if (psiElement !is PyCallExpression) return null
val versionZero = pydanticVersion?.major == 0
if (!getResolvedPsiElements(callee, context).any {
if (
!psiElement.multiResolveCalleeFunction(PyResolveContext.defaultContext(context)).any {
when {
versionZero -> isPydanticSchemaByPsiElement(it, context)
else -> it.isPydanticField || it.isCustomModelField
Expand Down
4 changes: 2 additions & 2 deletions src/com/koxudaxi/pydantic/PydanticTypeProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.jetbrains.python.codeInsight.typing.PyTypingTypeProvider
import com.jetbrains.python.codeInsight.typing.PyTypingTypeProvider.isBitwiseOrUnionAvailable
import com.jetbrains.python.psi.*
import com.jetbrains.python.psi.impl.*
import com.jetbrains.python.psi.resolve.PyResolveContext
import com.jetbrains.python.psi.types.*
import com.koxudaxi.pydantic.PydanticConfigService.Companion.getInstance
import one.util.streamex.StreamEx
Expand Down Expand Up @@ -262,8 +263,7 @@ class PydanticTypeProvider : PyTypeProviderBase() {
): PydanticDynamicModelClassType? {
val arguments = pyCallExpression.arguments.toList()
if (arguments.isEmpty()) return null
val referenceExpression = (pyCallExpression.callee as? PyReferenceExpression) ?: return null
val pyFunction = getResolvedPsiElements(referenceExpression, context)
val pyFunction = pyCallExpression.multiResolveCalleeFunction(PyResolveContext.defaultContext(context))
.asSequence()
.filterIsInstance<PyFunction>()
.map { it.takeIf { pyFunction -> pyFunction.isPydanticCreateModel } }.firstOrNull()
Expand Down

0 comments on commit 89081b9

Please sign in to comment.