diff --git a/pydantic-pycharm-plugin.iml b/pydantic-pycharm-plugin.iml
index ccc02fc3..d7cb8402 100644
--- a/pydantic-pycharm-plugin.iml
+++ b/pydantic-pycharm-plugin.iml
@@ -15,7 +15,7 @@
-
+
diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml
index 7543804a..7f642abf 100644
--- a/resources/META-INF/plugin.xml
+++ b/resources/META-INF/plugin.xml
@@ -1,9 +1,14 @@
com.koxudaxi.pydantic
Pydantic
- 0.0.21
+ 0.0.23
Koudai Aono @koxudaxi
version 0.0.23
+ Features
+
+ - Ignore protected and private fields [#79]
+
version 0.0.22
Features, BugFixes
diff --git a/src/com/koxudaxi/pydantic/Pydantic.kt b/src/com/koxudaxi/pydantic/Pydantic.kt
index a635c43d..9bc01c7a 100644
--- a/src/com/koxudaxi/pydantic/Pydantic.kt
+++ b/src/com/koxudaxi/pydantic/Pydantic.kt
@@ -114,7 +114,7 @@ internal fun getAliasedFieldName(field: PyTargetExpression, context: TypeEvalCon
if (versionZero) {
isPydanticSchemaByPsiElement(it, context)
} else {
- isPydanticFieldByPsiElement(it, context)
+ isPydanticFieldByPsiElement(it)
}
}
@@ -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)
@@ -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() != '_'
}
\ No newline at end of file
diff --git a/src/com/koxudaxi/pydantic/PydanticCompletionContributor.kt b/src/com/koxudaxi/pydantic/PydanticCompletionContributor.kt
index 7753a5ff..f9161e54 100644
--- a/src/com/koxudaxi/pydantic/PydanticCompletionContributor.kt
+++ b/src/com/koxudaxi/pydantic/PydanticCompletionContributor.kt
@@ -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)) {
@@ -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 ->
diff --git a/src/com/koxudaxi/pydantic/PydanticTypeProvider.kt b/src/com/koxudaxi/pydantic/PydanticTypeProvider.kt
index 80b8ef72..c78318f7 100644
--- a/src/com/koxudaxi/pydantic/PydanticTypeProvider.kt
+++ b/src/com/koxudaxi/pydantic/PydanticTypeProvider.kt
@@ -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)
@@ -203,7 +203,7 @@ class PydanticTypeProvider : PyTypeProviderBase() {
.any {
when {
versionZero -> isPydanticSchemaByPsiElement(it, context)
- else -> isPydanticFieldByPsiElement(it, context)
+ else -> isPydanticFieldByPsiElement(it)
}
}
diff --git a/testData/completion/fieldIgnore.py b/testData/completion/fieldIgnore.py
new file mode 100644
index 00000000..b14dfd55
--- /dev/null
+++ b/testData/completion/fieldIgnore.py
@@ -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().
\ No newline at end of file
diff --git a/testData/completion/keywordArgumentIgnore.py b/testData/completion/keywordArgumentIgnore.py
new file mode 100644
index 00000000..0ca23e73
--- /dev/null
+++ b/testData/completion/keywordArgumentIgnore.py
@@ -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()
\ No newline at end of file
diff --git a/testSrc/com/koxudaxi/pydantic/PydanticCompletionTest.kt b/testSrc/com/koxudaxi/pydantic/PydanticCompletionTest.kt
index bae29fc3..da989f4d 100644
--- a/testSrc/com/koxudaxi/pydantic/PydanticCompletionTest.kt
+++ b/testSrc/com/koxudaxi/pydantic/PydanticCompletionTest.kt
@@ -29,6 +29,13 @@ open class PydanticCompletionTest : PydanticTestCase() {
)
}
+ fun testKeywordArgumentIgnore() {
+ doFieldTest(
+ listOf(
+ )
+ )
+ }
+
fun testKeywordArgumentParent() {
doFieldTest(
listOf(
@@ -344,6 +351,14 @@ open class PydanticCompletionTest : PydanticTestCase() {
)
}
+ fun testFieldIgnore() {
+ doFieldTest(
+ listOf(
+ Pair("___slots__", "BaseModel")
+ )
+ )
+ }
+
fun testFieldOptional() {
doFieldTest(
listOf(