Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use multiResolveCalleeFunction instead of getResolvedPsiElements #661

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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