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

ignore protected fields #79

Merged
merged 3 commits into from
Oct 10, 2019
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
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