-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from koxudaxi/support_dataclass
Support pydantic.dataclasses.dataclass
- Loading branch information
Showing
11 changed files
with
81 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.koxudaxi.pydantic | ||
|
||
import com.intellij.psi.util.QualifiedName | ||
import com.jetbrains.python.psi.PyCallExpression | ||
import com.jetbrains.python.psi.PyClass | ||
import com.jetbrains.python.psi.PyKeywordArgument | ||
import com.jetbrains.python.psi.PyReferenceExpression | ||
import com.jetbrains.python.psi.resolve.PyResolveUtil | ||
import com.jetbrains.python.psi.types.TypeEvalContext | ||
|
||
|
||
fun getPyClassByPyKeywordArgument(pyKeywordArgument: PyKeywordArgument): PyClass? { | ||
val pyCallExpression = pyKeywordArgument.parent?.parent as? PyCallExpression ?: return null | ||
return pyCallExpression.callee?.reference?.resolve() as? PyClass ?: return null | ||
} | ||
|
||
fun isPydanticModel(pyClass: PyClass, context: TypeEvalContext? = null): Boolean { | ||
return isSubClassOfPydanticBaseModel(pyClass, context) || isPydanticDataclass(pyClass) | ||
} | ||
|
||
fun isPydanticBaseModel(pyClass: PyClass): Boolean { | ||
return pyClass.qualifiedName == "pydantic.main.BaseModel" | ||
} | ||
|
||
fun isSubClassOfPydanticBaseModel(pyClass: PyClass, context: TypeEvalContext?): Boolean { | ||
return pyClass.isSubclass("pydantic.main.BaseModel", context) | ||
} | ||
|
||
fun isPydanticDataclass(pyClass: PyClass): Boolean { | ||
val decorators = pyClass.decoratorList?.decorators ?: return false | ||
for (decorator in decorators) { | ||
val callee = (decorator.callee as? PyReferenceExpression) ?: continue | ||
|
||
for (decoratorQualifiedName in PyResolveUtil.resolveImportedElementQNameLocally(callee)) { | ||
if (decoratorQualifiedName == QualifiedName.fromDottedString("pydantic.dataclasses.dataclass")) return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
fun isPydanticField(pyClass: PyClass, context: TypeEvalContext? = null): Boolean { | ||
return pyClass.isSubclass("pydantic.schema.Schema", context) || pyClass.isSubclass("pydantic.field.Field", context) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters