-
-
Notifications
You must be signed in to change notification settings - Fork 69
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
pydantic-settings fails to handle unions with defaults; raises Attribute error #471
Comments
The following works: ...
class Car(BaseSettings):
driver: Cat | Dog
CliApp.run(Car, ["--driver.meow=meow"]) So this has to do with default arguments of union types (it makes sense that it's not trivial to provide a list of available arguments in that case, but it still shouldn't error...) |
A workaround seems to be to use a class Car(BaseSettings):
driver: Cat | Dog = Field(default_factory=Cat) This feels pretty normal coming from dataclasses, but pydantic states that mutable defaults are ok, or did I misunderstand this? |
@hramezani no, it's slightly more complicated. We have to ignore the model default that was propagated down the tree. i.e., at the point of failure it's too late for correction, it has to be resolved further up. e.g., "purr" has to be cascaded down the tree to in order to override the from pydantic_settings import BaseSettings, CliApp
class Cat(BaseSettings):
meow: str = "meow"
class Dog(BaseSettings):
bark: str = "bark"
class Car(BaseSettings):
driver: Cat | Dog = Cat(meow='purr')
CliApp.run(Car, cli_args=['--help'])
"""
usage: example.py [-h] [--driver JSON] [--driver.meow str] [--driver.bark str]
options:
-h, --help show this help message and exit
driver options:
--driver JSON set driver from JSON string
--driver.meow str (default: purr)
--driver.bark str (default: bark)
""" The issue occurs when applying defaults from I opened PR #472 for resolution. |
Won't have time to check it out this week, but the tests in @kschwab 's PR are great, so if they pass, this should be resolved. Thank you for the lightning fast fix! @kschwab @hramezani ❤️ |
Thank you for this wonderful addition to
pydantic
.However, the following snippet
raises
The text was updated successfully, but these errors were encountered: