Skip to content
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

Fix config detection for TypedDict from grandparent classes #7272

Merged
merged 1 commit into from Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions pydantic/_internal/_generate_schema.py
Expand Up @@ -67,6 +67,7 @@
ModelValidatorDecoratorInfo,
RootValidatorDecoratorInfo,
ValidatorDecoratorInfo,
get_attribute_from_bases,
inspect_field_serializer,
inspect_model_serializer,
inspect_validator,
Expand Down Expand Up @@ -1057,11 +1058,10 @@ def _typed_dict_schema(self, typed_dict_cls: Any, origin: Any) -> core_schema.Co
code='typed-dict-version',
)

config: ConfigDict | None = None
for base in (typed_dict_cls, *typed_dict_cls.__orig_bases__):
config = getattr(base, '__pydantic_config__', None)
if config is not None:
break
try:
config: ConfigDict | None = get_attribute_from_bases(typed_dict_cls, '__pydantic_config__')
except AttributeError:
config = None

with self._config_wrapper_stack.push(config):
core_config = self._config_wrapper.core_config(typed_dict_cls)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_types_typeddict.py
Expand Up @@ -890,3 +890,18 @@ class Model(TypedDict):
ta = TypeAdapter(Model)

assert ta.validate_python(dict(x=1))['x'] == '1'


def test_grandparent_config():
class MyTypedDict(TypedDict):
__pydantic_config__ = ConfigDict(str_to_lower=True)
x: str

class MyMiddleTypedDict(MyTypedDict):
y: str

class MySubTypedDict(MyMiddleTypedDict):
z: str

validated_data = TypeAdapter(MySubTypedDict).validate_python({'x': 'ABC', 'y': 'DEF', 'z': 'GHI'})
assert validated_data == {'x': 'abc', 'y': 'def', 'z': 'ghi'}