Skip to content

Commit

Permalink
ignore protected fields (#79)
Browse files Browse the repository at this point in the history
* ignore protected fields
  • Loading branch information
koxudaxi committed Oct 10, 2019
1 parent 5e89bfd commit 6911986
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pydantic-pycharm-plugin.iml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<sourceFolder url="file://$MODULE_DIR$/testData" type="java-test-resource" />
<excludePattern pattern="out" />
</content>
<orderEntry type="jdk" jdkName="JavaSDK" jdkType="IDEA JDK" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Python 3.7 (pydantic-pycharm-plugin) interpreter library" level="application" />
</component>
Expand Down
7 changes: 6 additions & 1 deletion resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<idea-plugin url="https://github.com/koxudaxi/pydantic-pycharm-plugin">
<id>com.koxudaxi.pydantic</id>
<name>Pydantic</name>
<version>0.0.21</version>
<version>0.0.23</version>
<vendor email="koaxudai@gmail.com">Koudai Aono @koxudaxi</vendor>
<change-notes><![CDATA[
<h2>version 0.0.23</h2>
<p>Features</p>
<ul>
<li>Ignore protected and private fields [#79] </li>
</ul>
<h2>version 0.0.22</h2>
<p>Features, BugFixes</p>
<ul>
Expand Down
8 changes: 6 additions & 2 deletions src/com/koxudaxi/pydantic/Pydantic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ internal fun getAliasedFieldName(field: PyTargetExpression, context: TypeEvalCon
if (versionZero) {
isPydanticSchemaByPsiElement(it, context)
} else {
isPydanticFieldByPsiElement(it, context)
isPydanticFieldByPsiElement(it)
}

}
Expand Down Expand Up @@ -164,7 +164,7 @@ internal fun isPydanticSchemaByPsiElement(psiElement: PsiElement, context: TypeE
return false
}

internal fun isPydanticFieldByPsiElement(psiElement: PsiElement, context: TypeEvalContext): Boolean {
internal fun isPydanticFieldByPsiElement(psiElement: PsiElement): Boolean {
when (psiElement) {
is PyFunction -> return isPydanticField(psiElement)
else -> PsiTreeUtil.getContextOfType(psiElement, PyFunction::class.java)
Expand All @@ -190,4 +190,8 @@ internal fun getPydanticVersion(project: Project, context: TypeEvalContext): Kot
pydanticVersionCache[versionString] = pydanticVersion
pydanticVersion
})
}

internal fun isValidFieldName(name: String): Boolean {
return name.first() != '_'
}
12 changes: 10 additions & 2 deletions src/com/koxudaxi/pydantic/PydanticCompletionContributor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class PydanticCompletionContributor : CompletionContributor() {
val pydanticVersion = getPydanticVersion(pyClass.project, typeEvalContext)
getClassVariables(pyClass, typeEvalContext)
.filter { it.name != null }
.filter {isValidFieldName(it.name!!)}
.forEach {
val elementName = getLookupNameFromFieldName(it, typeEvalContext, pydanticVersion)
if (excludes == null || !excludes.contains(elementName)) {
Expand Down Expand Up @@ -109,10 +110,17 @@ class PydanticCompletionContributor : CompletionContributor() {

pyClass.getAncestorClasses(typeEvalContext)
.filter { isPydanticModel(it) }
.forEach { fieldElements.addAll(it.classAttributes.mapNotNull { attribute -> attribute?.name }) }
.forEach { fieldElements.addAll(it.classAttributes
.filter {attribute
-> attribute.name?.let { name -> isValidFieldName(name)} ?: false}
.mapNotNull { attribute -> attribute?.name }) }


fieldElements.addAll(pyClass.classAttributes.mapNotNull { attribute -> attribute?.name })

fieldElements.addAll(pyClass.classAttributes
.filter {attribute
-> attribute.name?.let { name -> isValidFieldName(name)} ?: false}
.mapNotNull { attribute -> attribute?.name })

result.runRemainingContributors(parameters)
{ completionResult ->
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 @@ -132,7 +132,7 @@ class PydanticTypeProvider : PyTypeProviderBase() {
context: TypeEvalContext,
pyClass: PyClass,
pydanticVersion: KotlinVersion?): PyCallableParameter? {

if (field.name == null || ! isValidFieldName(field.name!!)) return null
if (!hasAnnotationValue(field) && !field.hasAssignedValue()) return null // skip fields that are invalid syntax

val defaultValueFromField = getDefaultValueForParameter(field, ellipsis, context, pydanticVersion)
Expand Down Expand Up @@ -203,7 +203,7 @@ class PydanticTypeProvider : PyTypeProviderBase() {
.any {
when {
versionZero -> isPydanticSchemaByPsiElement(it, context)
else -> isPydanticFieldByPsiElement(it, context)
else -> isPydanticFieldByPsiElement(it)
}

}
Expand Down
14 changes: 14 additions & 0 deletions testData/completion/fieldIgnore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from builtins import *
from pydantic import BaseModel


class A(BaseModel):
_abc: str = str('abc')
__cde: str = str('abc')


class B(A):
_efg: str = str('abc')
__hij: str = str('abc')

A().<caret>
15 changes: 15 additions & 0 deletions testData/completion/keywordArgumentIgnore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from builtins import *

from pydantic import BaseModel


class A(BaseModel):
_abc: str = str('abc')
__cde: str = str('abc')


class B(A):
_efg: str = str('abc')
__hij: str = str('abc')

B(<caret>)
15 changes: 15 additions & 0 deletions testSrc/com/koxudaxi/pydantic/PydanticCompletionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ open class PydanticCompletionTest : PydanticTestCase() {
)
}

fun testKeywordArgumentIgnore() {
doFieldTest(
listOf(
)
)
}

fun testKeywordArgumentParent() {
doFieldTest(
listOf(
Expand Down Expand Up @@ -344,6 +351,14 @@ open class PydanticCompletionTest : PydanticTestCase() {
)
}

fun testFieldIgnore() {
doFieldTest(
listOf(
Pair("___slots__", "BaseModel")
)
)
}

fun testFieldOptional() {
doFieldTest(
listOf(
Expand Down

0 comments on commit 6911986

Please sign in to comment.