Skip to content

Commit

Permalink
Reformat imports
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi committed May 3, 2021
1 parent 056b136 commit 8eb041f
Show file tree
Hide file tree
Showing 21 changed files with 591 additions and 522 deletions.
7 changes: 5 additions & 2 deletions src/com/koxudaxi/pydantic/Pydantic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ import com.jetbrains.extensions.QNameResolveContext
import com.jetbrains.extensions.resolveToElement
import com.jetbrains.python.codeInsight.typing.PyTypingTypeProvider
import com.jetbrains.python.psi.*
import com.jetbrains.python.psi.impl.*
import com.jetbrains.python.psi.impl.PyStarArgumentImpl
import com.jetbrains.python.psi.impl.PyTargetExpressionImpl
import com.jetbrains.python.psi.resolve.PyResolveContext
import com.jetbrains.python.psi.resolve.PyResolveUtil
import com.jetbrains.python.psi.types.*
import com.jetbrains.python.sdk.*
import com.jetbrains.python.sdk.PythonSdkUtil
import com.jetbrains.python.sdk.isAssociatedWithModule
import com.jetbrains.python.sdk.pythonSdk
import com.jetbrains.python.statistics.modules
import java.util.regex.Pattern

Expand Down
13 changes: 6 additions & 7 deletions src/com/koxudaxi/pydantic/PydanticConfigPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

public class PydanticConfigPanel {

private JPanel configPanel;
private JCheckBox initTypedCheckBox;
private JTextPane ifEnabledIncludeTheTextPane;
private JCheckBox warnUntypedFieldsCheckBox;
private JTextPane ifEnabledRaiseATextPane;
private JTextPane textPane1;
PydanticConfigPanel(Project project) {
PydanticConfigService pydanticConfigService = PydanticConfigService.Companion.getInstance(project);

Expand All @@ -33,13 +39,6 @@ public class PydanticConfigPanel {
setHyperlinkHtml(this.textPane1, "See <a href=\"https://koxudaxi.github.io/pydantic-pycharm-plugin/\">documentation</a> for more details.</p>");
}

private JPanel configPanel;
private JCheckBox initTypedCheckBox;
private JTextPane ifEnabledIncludeTheTextPane;
private JCheckBox warnUntypedFieldsCheckBox;
private JTextPane ifEnabledRaiseATextPane;
private JTextPane textPane1;

public Boolean getInitTyped() {
return initTypedCheckBox.isSelected();
}
Expand Down
3 changes: 2 additions & 1 deletion src/com/koxudaxi/pydantic/PydanticDataclassTypeProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package com.koxudaxi.pydantic
import com.intellij.psi.PsiElement
import com.jetbrains.python.codeInsight.stdlib.PyDataclassTypeProvider
import com.jetbrains.python.psi.*
import com.jetbrains.python.psi.impl.*
import com.jetbrains.python.psi.impl.PyCallExpressionImpl
import com.jetbrains.python.psi.impl.PyCallExpressionNavigator
import com.jetbrains.python.psi.types.*

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.koxudaxi.pydantic
import com.intellij.psi.PsiElement
import com.jetbrains.python.codeInsight.PyCustomMember
import com.jetbrains.python.psi.resolve.PyResolveContext
import com.jetbrains.python.psi.types.*
import com.jetbrains.python.psi.types.PyClassMembersProviderBase
import com.jetbrains.python.psi.types.PyClassType
import com.jetbrains.python.psi.types.TypeEvalContext

class PydanticDynamicModelMemberProvider : PyClassMembersProviderBase() {
override fun resolveMember(
Expand Down
4 changes: 3 additions & 1 deletion src/com/koxudaxi/pydantic/PydanticInsertArgumentsQuickFix.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.util.IncorrectOperationException
import com.intellij.util.containers.nullize
import com.jetbrains.python.psi.*
import com.jetbrains.python.psi.PyCallExpression
import com.jetbrains.python.psi.PyElementGenerator
import com.jetbrains.python.psi.PyFile
import com.jetbrains.python.psi.types.PyCallableParameter
import com.jetbrains.python.psi.types.TypeEvalContext

Expand Down
3 changes: 2 additions & 1 deletion src/com/koxudaxi/pydantic/PydanticRegexInjector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import com.intellij.psi.PsiLanguageInjectionHost
import com.jetbrains.python.codeInsight.PyInjectionUtil
import com.jetbrains.python.codeInsight.PyInjectorBase
import com.jetbrains.python.codeInsight.regexp.PythonRegexpLanguage
import com.jetbrains.python.psi.*
import com.jetbrains.python.psi.PyFile
import com.jetbrains.python.psi.StringLiteralExpression

class PydanticRegexInjector : PyInjectorBase() {
override fun registerInjection(
Expand Down
98 changes: 61 additions & 37 deletions src/com/koxudaxi/pydantic/PydanticTypeCheckerInspection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ import com.jetbrains.python.psi.types.PyLiteralType.Companion.promoteToLiteral
import com.jetbrains.python.psi.types.PyTypedDictType.Companion.match

class PydanticTypeCheckerInspection : PyTypeCheckerInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
override fun buildVisitor(
holder: ProblemsHolder,
isOnTheFly: Boolean,
session: LocalInspectionToolSession,
): PsiElementVisitor {
if (LOG.isDebugEnabled) {
session.putUserData(TIME_KEY, System.nanoTime())
}
return Visitor(holder, session)
}

class Visitor(holder: ProblemsHolder?, session: LocalInspectionToolSession) : PyTypeCheckerInspection.Visitor(holder, session) {
class Visitor(holder: ProblemsHolder?, session: LocalInspectionToolSession) :
PyTypeCheckerInspection.Visitor(holder, session) {

val pydanticConfigService = PydanticConfigService.getInstance(holder!!.project)

Expand All @@ -41,27 +46,34 @@ class PydanticTypeCheckerInspection : PyTypeCheckerInspection() {

private fun checkCallSiteForPydantic(callSite: PyCallSiteExpression) {
PyCallExpressionHelper.mapArguments(callSite, resolveContext)
.filter { mapping: PyArgumentsMapping? -> mapping!!.unmappedArguments.isEmpty() && mapping.unmappedParameters.isEmpty() }
.forEach { mapping: PyArgumentsMapping -> analyzeCallee(callSite, mapping) }
.filter { mapping: PyArgumentsMapping? -> mapping!!.unmappedArguments.isEmpty() && mapping.unmappedParameters.isEmpty() }
.forEach { mapping: PyArgumentsMapping -> analyzeCallee(callSite, mapping) }
}

private fun getParsableTypeFromTypeMap(typeForParameter: PyType, cache: MutableMap<PyType, PyType?>): PyType? {
return getTypeFromTypeMap(
{ project: Project -> PydanticConfigService.getInstance(project).parsableTypeMap },
typeForParameter,
cache
{ project: Project -> PydanticConfigService.getInstance(project).parsableTypeMap },
typeForParameter,
cache
)
}

private fun getAcceptableTypeFromTypeMap(typeForParameter: PyType, cache: MutableMap<PyType, PyType?>): PyType? {
private fun getAcceptableTypeFromTypeMap(
typeForParameter: PyType,
cache: MutableMap<PyType, PyType?>,
): PyType? {
return getTypeFromTypeMap(
{ project: Project -> PydanticConfigService.getInstance(project).acceptableTypeMap },
typeForParameter,
cache
{ project: Project -> PydanticConfigService.getInstance(project).acceptableTypeMap },
typeForParameter,
cache
)
}

private fun getTypeFromTypeMap(getTypeMap: (project: Project) -> (Map<String, List<String>>), typeForParameter: PyType, cache: MutableMap<PyType, PyType?>): PyType? {
private fun getTypeFromTypeMap(
getTypeMap: (project: Project) -> (Map<String, List<String>>),
typeForParameter: PyType,
cache: MutableMap<PyType, PyType?>,
): PyType? {
if (cache.containsKey(typeForParameter)) {
return cache[typeForParameter]
}
Expand Down Expand Up @@ -99,60 +111,72 @@ class PydanticTypeCheckerInspection : PyTypeCheckerInspection() {
val strictMatched = matchParameterAndArgument(expected, actual, argument, substitutions)
val strictResult = AnalyzeArgumentResult(expected, actual, strictMatched)
if (!strictResult.isMatched) {
val expectedType = PythonDocumentationProvider.getTypeName(strictResult.expectedType, myTypeEvalContext)
val expectedType =
PythonDocumentationProvider.getTypeName(strictResult.expectedType, myTypeEvalContext)
val actualType = PythonDocumentationProvider.getTypeName(strictResult.actualType, myTypeEvalContext)
if (expected is PyType) {
val parsableType = getParsableTypeFromTypeMap(expected, cachedParsableTypeMap)
if (parsableType != null) {
val parsableMatched = matchParameterAndArgument(parsableType, actual, argument, substitutions)
val parsableMatched =
matchParameterAndArgument(parsableType, actual, argument, substitutions)
if (AnalyzeArgumentResult(parsableType, actual, parsableMatched).isMatched) {
registerProblem(
argument,
String.format("Field is of type '%s', '%s' may not be parsable to '%s'",
expectedType,
actualType,
expectedType),
pydanticConfigService.parsableTypeHighlightType
argument,
String.format("Field is of type '%s', '%s' may not be parsable to '%s'",
expectedType,
actualType,
expectedType),
pydanticConfigService.parsableTypeHighlightType
)
continue
}
}
val acceptableType = getAcceptableTypeFromTypeMap(expected, cachedAcceptableTypeMap)
if (acceptableType != null) {
val acceptableMatched = matchParameterAndArgument(acceptableType, actual, argument, substitutions)
val acceptableMatched =
matchParameterAndArgument(acceptableType, actual, argument, substitutions)
if (AnalyzeArgumentResult(acceptableType, actual, acceptableMatched).isMatched) {
registerProblem(
argument,
String.format("Field is of type '%s', '%s' is set as an acceptable type in pyproject.toml",
expectedType,
actualType,
expectedType),
pydanticConfigService.acceptableTypeHighlightType
argument,
String.format("Field is of type '%s', '%s' is set as an acceptable type in pyproject.toml",
expectedType,
actualType,
expectedType),
pydanticConfigService.acceptableTypeHighlightType
)
continue
}
}
}
registerProblem(argument, String.format("Expected type '%s', got '%s' instead",
expectedType,
actualType)
expectedType,
actualType)
)
}
}
}

private fun matchParameterAndArgument(parameterType: PyType?,
argumentType: PyType?,
argument: PyExpression?,
substitutions: Map<PyGenericType, PyType>): Boolean {
return if (parameterType is PyTypedDictType && argument is PyDictLiteralExpression) match((parameterType as PyTypedDictType?)!!, (argument as PyDictLiteralExpression?)!!, myTypeEvalContext) else PyTypeChecker.match(parameterType, argumentType, myTypeEvalContext, substitutions) &&
private fun matchParameterAndArgument(
parameterType: PyType?,
argumentType: PyType?,
argument: PyExpression?,
substitutions: Map<PyGenericType, PyType>,
): Boolean {
return if (parameterType is PyTypedDictType && argument is PyDictLiteralExpression) match((parameterType as PyTypedDictType?)!!,
(argument as PyDictLiteralExpression?)!!,
myTypeEvalContext) else PyTypeChecker.match(parameterType,
argumentType,
myTypeEvalContext,
substitutions) &&
!matchingProtocolDefinitions(parameterType, argumentType, myTypeEvalContext)
}
}

internal class AnalyzeArgumentResult(val expectedType: PyType?,
val actualType: PyType?,
val isMatched: Boolean)
internal class AnalyzeArgumentResult(
val expectedType: PyType?,
val actualType: PyType?,
val isMatched: Boolean,
)

companion object {
private val LOG = Logger.getInstance(PydanticTypeCheckerInspection::class.java.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

/**
* Project descriptor (extracted from {@link com.jetbrains.python.fixtures.PyTestCase}) and should be used with it.
*
* @author Ilya.Kazakevich
*/
public class PyLightProjectDescriptor extends LightProjectDescriptor {
Expand Down
Loading

0 comments on commit 8eb041f

Please sign in to comment.