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 dynamic model field detection #659

Merged
merged 2 commits into from
Mar 1, 2023
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 @@ -4,6 +4,7 @@
- Fix wrong inspections when a model has a __call__ method [[#655](https://github.com/koxudaxi/pydantic-pycharm-plugin/pull/655)]
- Reduce unnecessary resolve in type providers [[#656](https://github.com/koxudaxi/pydantic-pycharm-plugin/pull/656)]
- 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)]

## 0.3.17 - 2022-12-16
- Support Union operator [[#602](https://github.com/koxudaxi/pydantic-pycharm-plugin/pull/602)]
Expand Down
20 changes: 3 additions & 17 deletions src/com/koxudaxi/pydantic/Pydantic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ fun getConfigValue(name: String, value: Any?, context: TypeEvalContext): Any? {
ConfigType.LIST_PYTYPE -> {
if (value is PyElement) {
when (val tupleValue = PsiTreeUtil.findChildOfType(value, PyTupleExpression::class.java)) {
is PyTupleExpression -> tupleValue.toList().mapNotNull { getPyTypeFromPyExpression(it, context) }
is PyTupleExpression -> tupleValue.toList().mapNotNull { context.getType(it) }
else -> null
}
} else null
Expand Down Expand Up @@ -558,28 +558,14 @@ val PyCallableParameter.required: Boolean
get() = !hasDefaultValue() || (defaultValue !is PyNoneLiteralExpression && defaultValueText == "...")


fun getPyTypeFromPyExpression(pyExpression: PyExpression, context: TypeEvalContext): PyType? {
return when (pyExpression) {
is PyType -> pyExpression
is PyReferenceExpression -> {
getResolvedPsiElements(pyExpression, context)
.asSequence()
.filterIsInstance<PyClass>()
.map { pyClass -> pyClass.getType(context)?.getReturnType(context) }
.firstOrNull()
}
else -> null
}
}

internal fun hasTargetPyType(
pyExpression: PyExpression,
targetPyTypes: List<PyType>,
context: TypeEvalContext,
): Boolean {
val callee = (pyExpression as? PyCallExpression)?.callee ?: return false
val pyType = getPyTypeFromPyExpression(callee, context) ?: return false
val defaultValueTypeClassQName = pyType.declarationElement?.qualifiedName ?: return false
val pyType = callee.getType(context)?.pyClassTypes?.firstOrNull()?.getReturnType(context)
val defaultValueTypeClassQName = pyType?.declarationElement?.qualifiedName ?: return false
return targetPyTypes.any { it.declarationElement?.qualifiedName == defaultValueTypeClassQName }
}

Expand Down
2 changes: 1 addition & 1 deletion src/com/koxudaxi/pydantic/PydanticCompletionContributor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class PydanticCompletionContributor : CompletionContributor() {
genericTypeMap: Map<PyGenericType, PyType>?,
): String? {

val parameter = typeProvider.fieldToParameter(pyTargetExpression,
val parameter = typeProvider.dynamicModelFieldToParameter(pyTargetExpression,
ellipsis,
typeEvalContext,
pyClass,
Expand Down
23 changes: 10 additions & 13 deletions src/com/koxudaxi/pydantic/PydanticTypeProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class PydanticTypeProvider : PyTypeProviderBase() {
context: TypeEvalContext, pyClass: PyClass,
pydanticVersion: KotlinVersion?,
): PyType? {
return fieldToParameter(
return dynamicModelFieldToParameter(
pyTargetExpression,
ellipsis,
context,
Expand Down Expand Up @@ -333,7 +333,7 @@ class PydanticTypeProvider : PyTypeProviderBase() {
continue
}
baseClassCollected.putAll(getClassVariables(current, context)
.map { it to fieldToParameter(it, context, typed) }
.map { it to dynamicModelFieldToParameter(it, context, typed) }
.mapNotNull { (field, parameter) ->
parameter.name?.let { name -> Triple(field, parameter, name) }
}
Expand All @@ -359,7 +359,7 @@ class PydanticTypeProvider : PyTypeProviderBase() {
.filter { (name, _) -> name.isValidFieldName && !name.startsWith('_') }
.filter { (name, _) -> (newVersion || name != "model_name") }
.map { (name, field) ->
val parameter = fieldToParameter(field, context, typed)
val parameter = dynamicModelFieldToParameter(field, context, typed)
name to PydanticDynamicModel.createAttribute(name, parameter, field, context, false)
}
)
Expand Down Expand Up @@ -483,7 +483,7 @@ class PydanticTypeProvider : PyTypeProviderBase() {
getClassVariables(current, context)
.filterNot { isUntouchedClass(it.findAssignedValue(), config, context) }
.mapNotNull {
fieldToParameter(
dynamicModelFieldToParameter(
it,
ellipsis,
context,
Expand All @@ -510,7 +510,7 @@ class PydanticTypeProvider : PyTypeProviderBase() {
}


internal fun fieldToParameter(
internal fun dynamicModelFieldToParameter(
field: PyTargetExpression,
ellipsis: PyNoneLiteralExpression,
context: TypeEvalContext,
Expand Down Expand Up @@ -572,7 +572,7 @@ class PydanticTypeProvider : PyTypeProviderBase() {
)
}

private fun fieldToParameter(
private fun dynamicModelFieldToParameter(
field: PyExpression,
context: TypeEvalContext,
typed: Boolean = true,
Expand All @@ -581,17 +581,14 @@ class PydanticTypeProvider : PyTypeProviderBase() {
var defaultValue: PyExpression? = null
when (val tupleValue = PsiTreeUtil.findChildOfType(field, PyTupleExpression::class.java)) {
is PyTupleExpression -> {
tupleValue.toList().let {
if (it.size > 1) {
type = getPyTypeFromPyExpression(it[0], context)
defaultValue = it[1]
tupleValue.firstOrNull()?.let {
type = context.getType(it)?.pyClassTypes?.first()?.getReturnType(context)
defaultValue = it
}
}
}

else -> {
type = context.getType(field)
defaultValue = (field as? PyKeywordArgumentImpl)?.valueExpression
defaultValue = (field as? PyKeywordArgument)?.valueExpression
}
}
val typeForParameter = when {
Expand Down