Skip to content

Commit

Permalink
Fix config detection for TypedDict from grandparent classes (#7272)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmontagu committed Aug 28, 2023
1 parent ce51f5e commit dad6665
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
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'}

0 comments on commit dad6665

Please sign in to comment.