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

Improve dataclass default Value detection #604

Merged
merged 2 commits into from
Dec 13, 2022
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
43 changes: 37 additions & 6 deletions src/com/koxudaxi/pydantic/PydanticDataclassTypeProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import com.jetbrains.python.psi.types.*
*/
class PydanticDataclassTypeProvider : PyTypeProviderBase() {
private val pyDataclassTypeProvider = PyDataclassTypeProvider()

private val pydanticTypeProvider = PydanticTypeProvider()
override fun getReferenceExpressionType(
referenceExpression: PyReferenceExpression,
context: TypeEvalContext,
): PyType? {
return getPydanticDataclass(referenceExpression,
TypeEvalContext.codeInsightFallback(referenceExpression.project))
return getPydanticDataclass(
referenceExpression,
TypeEvalContext.codeInsightFallback(referenceExpression.project)
)
}


Expand All @@ -49,16 +51,44 @@ class PydanticDataclassTypeProvider : PyTypeProviderBase() {
): PyType? {
val callSite = PyCallExpressionNavigator.getPyCallExpressionByCallee(pyReferenceExpression)
val dataclassCallableType = getDataclassCallableType(referenceTarget, context, callSite) ?: return null

val dataclassType = (dataclassCallableType).getReturnType(context) as? PyClassType ?: return null
if (!dataclassType.pyClass.isPydanticDataclass) return null
val ellipsis = PyElementGenerator.getInstance(referenceTarget.project).createEllipsis()
val injectedPyCallableType = PyCallableTypeImpl(
dataclassCallableType.getParameters(context)?.map {
when {
it.defaultValueText == "..." && it.defaultValue is PyNoneLiteralExpression ->
injectDefaultValue(dataclassType.pyClass, it, ellipsis, context) ?: it

else -> it
}
}, dataclassType
)
val injectedDataclassType = (injectedPyCallableType).getReturnType(context) as? PyClassType ?: return null
return when {
callSite is PyCallExpression && definition -> dataclassCallableType
definition -> dataclassType.toClass()
else -> dataclassType
callSite is PyCallExpression && definition -> injectedPyCallableType
definition -> injectedDataclassType.toClass()
else -> injectedDataclassType
}
}

private fun injectDefaultValue(
pyClass: PyClass,
pyCallableParameter: PyCallableParameter,
ellipsis: PyNoneLiteralExpression,
context: TypeEvalContext
): PyCallableParameter? {
val name = pyCallableParameter.name ?: return null
val attribute = pyClass.findClassAttribute(name, true, context) ?: return null
val defaultValue =
pydanticTypeProvider.getDefaultValueByAssignedValue(attribute, ellipsis, context, null, true)
return PyCallableParameterImpl.nonPsi(
name,
pyCallableParameter.getArgumentType(context),
defaultValue
)
}

private fun getPydanticDataclass(referenceExpression: PyReferenceExpression, context: TypeEvalContext): PyType? {
return getResolvedPsiElements(referenceExpression, context)
Expand All @@ -67,6 +97,7 @@ class PydanticDataclassTypeProvider : PyTypeProviderBase() {
when {
it is PyClass && it.isPydanticDataclass ->
getPydanticDataclassType(it, context, referenceExpression, true)

it is PyTargetExpression -> (it as? PyTypedElement)
?.getType(context)?.pyClassTypes
?.filter { pyClassType -> pyClassType.pyClass.isPydanticDataclass }
Expand Down
16 changes: 10 additions & 6 deletions src/com/koxudaxi/pydantic/PydanticTypeProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ class PydanticTypeProvider : PyTypeProviderBase() {
}


private fun getDefaultValueByAssignedValue(
internal fun getDefaultValueByAssignedValue(
field: PyTargetExpression,
ellipsis: PyNoneLiteralExpression,
context: TypeEvalContext,
Expand All @@ -721,7 +721,7 @@ class PydanticTypeProvider : PyTypeProviderBase() {
if (isDataclass) {
resolveResults
.any {
it.isDataclassField
it.isDataclassField || it.isPydanticField
}
.let {
return when {
Expand Down Expand Up @@ -760,9 +760,13 @@ class PydanticTypeProvider : PyTypeProviderBase() {
private fun getDefaultValueForDataclass(
assignedValue: PyCallExpression,
context: TypeEvalContext,
argumentName: String,
argumentName: String?,
): PyExpression? {
val defaultValue = assignedValue.getKeywordArgument(argumentName)
val defaultValue = if (argumentName is String) {
assignedValue.getKeywordArgument(argumentName)
} else {
assignedValue.argumentList?.arguments?.firstOrNull().takeIf { it !is PyKeywordArgument }
}
return when {
defaultValue == null -> null
defaultValue.text == "..." -> null
Expand All @@ -786,9 +790,9 @@ class PydanticTypeProvider : PyTypeProviderBase() {
val defaultValue = getDefaultValueForDataclass(assignedValue, context, "default")
val defaultFactoryValue = getDefaultValueForDataclass(assignedValue, context, "default_factory")
return when {
defaultValue == null && defaultFactoryValue == null -> null
defaultValue != null -> defaultValue
else -> defaultFactoryValue
defaultFactoryValue != null -> defaultFactoryValue
else -> getDefaultValueForDataclass(assignedValue, context, null)
}
}
}
5 changes: 4 additions & 1 deletion testData/completion/dataclassKeywordArgument.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

from dataclasses import field, MISSING

from pydantic.dataclasses import dataclass
from pydantic.dataclasses import dataclass, Field


def dummy():
return '123'
Expand All @@ -20,6 +21,8 @@ class A:
cda: str = field(default=MISSING, default_factory=MISSING)
edc: str = dummy()
gef: str = field(default=unresolved)
jih: str = field(..., title="empty", )
mlk: str = field(..., title="empty", )

@dataclass
class B(A):
Expand Down
2 changes: 2 additions & 0 deletions testSrc/com/koxudaxi/pydantic/PydanticCompletionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,9 @@ open class PydanticCompletionTest : PydanticTestCase() {
Pair("efg=", "str='xyz' A"),
Pair("gef=", "str=unresolved A"),
Pair("hij=", "str=lambda :'asd' A"),
Pair("jih=", "str A"),
Pair("klm=", "str='qwe' A"),
Pair("mlk=", "str A"),
Pair("qrs=", "str='fgh' A"),
Pair("tuw=", "str A"),
Pair("xyz=", "str A")
Expand Down