Skip to content

Commit

Permalink
Fix populate_by_name (#797)
Browse files Browse the repository at this point in the history
* Fix populate_by_name

* Fix unittest
  • Loading branch information
koxudaxi authored Sep 8, 2023
1 parent c0bf7c4 commit 2a9bc63
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/com/koxudaxi/pydantic/Pydantic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,10 @@ fun getFieldName(
config["allow_population_by_alias"] == true -> field.name
else -> getAliasedFieldName(field, context, pydanticVersion)
}
2 -> when {
config["populate_by_name"] == true -> field.name
else -> getAliasedFieldName(field, context, pydanticVersion)
}
else -> when {
config["allow_population_by_field_name"] == true -> field.name
else -> getAliasedFieldName(field, context, pydanticVersion)
Expand Down
13 changes: 13 additions & 0 deletions testData/completionv2/keywordArgumentPopulateByName.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@


from pydantic import BaseModel, Field, ConfigDict

class A(BaseModel):
model_config = ConfigDict(
populate_by_name=True,
)
abc: str = Field(..., alias='ABC')
cde: str = Field(..., alias='CDE')


A(<caret>)
36 changes: 36 additions & 0 deletions testData/mock/pydanticv2/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

def Field( # noqa: C901
default: Any = PydanticUndefined,
*,
default_factory: typing.Callable[[], Any] | None = _Unset,
alias: str | None = _Unset,
alias_priority: int | None = _Unset,
validation_alias: str | AliasPath | AliasChoices | None = _Unset,
serialization_alias: str | None = _Unset,
title: str | None = _Unset,
description: str | None = _Unset,
examples: list[Any] | None = _Unset,
exclude: bool | None = _Unset,
discriminator: str | None = _Unset,
json_schema_extra: dict[str, Any] | typing.Callable[[dict[str, Any]], None] | None = _Unset,
frozen: bool | None = _Unset,
validate_default: bool | None = _Unset,
repr: bool = _Unset,
init_var: bool | None = _Unset,
kw_only: bool | None = _Unset,
pattern: str | None = _Unset,
strict: bool | None = _Unset,
gt: float | None = _Unset,
ge: float | None = _Unset,
lt: float | None = _Unset,
le: float | None = _Unset,
multiple_of: float | None = _Unset,
allow_inf_nan: bool | None = _Unset,
max_digits: int | None = _Unset,
decimal_places: int | None = _Unset,
min_length: int | None = _Unset,
max_length: int | None = _Unset,
union_mode: Literal['smart', 'left_to_right'] = _Unset,
**extra: Unpack[_EmptyKwargs],
) -> Any:
pass
16 changes: 15 additions & 1 deletion testSrc/com/koxudaxi/pydantic/PydanticCompletionV2Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ open class PydanticCompletionV2Test : PydanticTestCase(version = "v2") {
"__pydantic_model_complete__ = ",
"__pydantic_core_schema__ = ",
"__pydantic_generic_parameters__ = ",
"__pydantic_parent_namespace__ = "
"__pydantic_parent_namespace__ = ",
"None",
"not",
"ConfigDict",
"async",
"False",
"True"
)
val actual = myFixture!!.completeBasic().filter {
it!!.psiElement is PyTargetExpression || it.psiElement == null
Expand Down Expand Up @@ -77,4 +83,12 @@ open class PydanticCompletionV2Test : PydanticTestCase(version = "v2") {
)
)
}
fun testKeywordArgumentPopulateByName() {
doFieldTest(
listOf(
Pair("abc=", "str A"),
Pair("cde=", "str A")
)
)
}
}

0 comments on commit 2a9bc63

Please sign in to comment.