-
-
Notifications
You must be signed in to change notification settings - Fork 64
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
BaseSettings doesn't respect population by field name if env is present #23
Comments
Hi @RobertCraigie from pydantic import BaseModel, Extra, Field
class M(BaseModel, allow_population_by_field_name=True, extra=Extra.forbid):
x: str = Field(alias='y')
data = {'x': 'pikaX'}
assert M(**data).x == 'pikaX' # works
data = {'y': 'pikaY'}
assert M(**data).x == 'pikaY' # works
data = {'x': 'pikaX', 'y': 'pikaY'} # raises an error
|
Ah I understand the issue, I would still like to see this use case supported through a config option or similar if that would be possible? There is an also an issue with setting from pydantic import BaseModel, Extra, Field
class M(BaseModel, allow_population_by_field_name=True, extra=Extra.allow):
x: int = Field(alias='y')
data = {'x': 1}
assert M(**data).x == 1 # works
data = {'y': 2}
assert M(**data).x == 2 # works
data = {'x': 'foo'} # raises an error as it should
data = {'x': 'foo', 'y': 2} # works but sets x to 'foo'
assert M(**data).x == 'foo' |
This issue persists in pydantic v2. It seems setting import pytest
from pydantic_settings import BaseSettings, SettingsConfigDict
import pydantic as v2
class V2(BaseSettings):
log_level: str = v2.Field("INFO", validation_alias="log")
model_config = SettingsConfigDict(
populate_by_name=True,
)
@pytest.mark.parametrize(
"arg",
[
"log_level",
"log",
],
)
def test_v2(arg):
data = {arg: "ERROR"}
assert V2(**data).log_level == "ERROR" |
Closing this because we have more detailed discussion in #180 |
Checks
Bug
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:When the following are true
Config.allow_population_by_field_name
is Truean
extra fields not permitted
error will be raised, however the same error is not raised if the field alias is passed to init instead of the field nameThe text was updated successfully, but these errors were encountered: