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 inherited typeddict attributes / config #6981

Merged
merged 1 commit into from Aug 1, 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
7 changes: 6 additions & 1 deletion pydantic/_internal/_generate_schema.py
Expand Up @@ -1043,7 +1043,12 @@ def _typed_dict_schema(self, typed_dict_cls: Any, origin: Any) -> core_schema.Co
code='typed-dict-version',
)

config = getattr(typed_dict_cls, '__pydantic_config__', None)
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

with self._config_wrapper_stack.push(config):
core_config = self._config_wrapper.core_config(typed_dict_cls)

Expand Down
12 changes: 12 additions & 0 deletions tests/test_types_typeddict.py
Expand Up @@ -866,6 +866,18 @@ class Model(TypedDict):
assert ta.validate_python({'x': 'ABC'}) == {'x': 'abc'}


def test_model_config_inherited() -> None:
class Base(TypedDict):
__pydantic_config__ = ConfigDict(str_to_lower=True) # type: ignore

class Model(Base):
x: str

ta = TypeAdapter(Model)

assert ta.validate_python({'x': 'ABC'}) == {'x': 'abc'}


def test_schema_generator() -> None:
class LaxStrGenerator(GenerateSchema):
def str_schema(self) -> CoreSchema:
Expand Down