-
-
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
Nested dataclass settings class gives AttributeError: __pydantic_fields__
#356
Comments
@kschwab Could you please take a look? |
@bschoenmaeckers I'm unable to reproduce the issue. Can you paste the versions for Also, the |
Tested it on the latest commit of main:
|
My SubSettings has to ingerit from BaseSettings because I want to set custom config values to that sub model only, like |
If you run my example with pytest on the current main then it should crash. Run ###test.py import dataclasses
import os
import pytest
from pydantic_settings import BaseSettings, SettingsConfigDict
class SetEnv:
def __init__(self):
self.envars = set()
def set(self, name, value):
self.envars.add(name)
os.environ[name] = value
def pop(self, name):
self.envars.remove(name)
os.environ.pop(name)
def clear(self):
for n in self.envars:
os.environ.pop(n)
@pytest.fixture
def env():
setenv = SetEnv()
yield setenv
setenv.clear()
def test_nested_dataclass_setting(env):
@dataclasses.dataclass
class DataClass:
value: str
class SubSettings(BaseSettings, DataClass):
model_config = SettingsConfigDict(alias_generator=str.lower)
class Cfg(BaseSettings):
model_config = SettingsConfigDict(env_nested_delimiter='__')
sub: SubSettings
env.set('SUB__VALUE', 'something')
cfg = Cfg() |
Thanks @bschoenmaeckers. It was the dataclasses. I was using pydantic dataclasses instead of generic dataclasses. I was able to reproduce prior to However, it does raise a potential issue with either CLI settings or pydantic import dataclasses
from pydantic import BaseModel
from pydantic.dataclasses import is_pydantic_dataclass
@dataclasses.dataclass
class DataClass:
value: str
class DataModel(BaseModel, DataClass):
...
print(is_pydantic_dataclass(DataModel))
#> True |
Yes my bad, it is the vanilla dataclass.
My test passes for version 2.3.2 & 2.3.3 but it fails again for 2.3.4 & 2.4.0 |
Got it. Yes, the pydantic_settings/main.py:145: in __init__
**__pydantic_self__._settings_build_values(
pydantic_settings/main.py:330: in _settings_build_values
source_state = source()
pydantic_settings/sources.py:430: in __call__
field_value = self.prepare_field_value(field_name, field, field_value, value_is_complex)
pydantic_settings/sources.py:623: in prepare_field_value
env_val_built = self.explode_env_vars(field_name, field, self.env_vars)
pydantic_settings/sources.py:749: in explode_env_vars
target_field = self.next_field(target_field, last_key, self.case_sensitive)
pydantic_settings/sources.py:700: in next_field
annotation.__pydantic_fields__
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <class 'test_bed.test_nested_dataclass_setting.<locals>.SubSettings'>, item = '__pydantic_fields__'
def __getattr__(self, item: str) -> Any:
"""This is necessary to keep attribute access working for class attribute access."""
private_attributes = self.__dict__.get('__private_attributes__')
if private_attributes and item in private_attributes:
return private_attributes[item]
if item == '__pydantic_core_schema__':
# This means the class didn't get a schema generated for it, likely because there was an undefined reference
maybe_mock_validator = getattr(self, '__pydantic_validator__', None)
if isinstance(maybe_mock_validator, MockValSer):
rebuilt_validator = maybe_mock_validator.rebuild()
if rebuilt_validator is not None:
# In this case, a validator was built, and so `__pydantic_core_schema__` should now be set
return getattr(self, '__pydantic_core_schema__')
> raise AttributeError(item)
E AttributeError: __pydantic_fields__. Did you mean: '__pydantic_fields_set__'?
../.local/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py:242: AttributeError @hramezani this is in the |
@kschwab Yes,
I've created #357 to fix the problem. Take a look if you have time. |
I have a dataclass that I don't control as a nested object (wrapped by a BaseSettings class). This used to work great but after v2.3.0 (#214) it throws a AttributeError. See the following testcase that passes on
<=v2.2.1
but fails on>=v2.3.0
.The exception
The text was updated successfully, but these errors were encountered: