Skip to content

Commit

Permalink
Make it work when using a type annotation on the model_config (#6136)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmontagu committed Jun 14, 2023
1 parent 175c7ca commit 216f123
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pydantic/_internal/_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ def collect_model_fields( # noqa: C901

class_vars: set[str] = set()
for ann_name, ann_type in type_hints.items():
if ann_name == 'model_config':
# We never want to treat `model_config` as a field
# Note: we may need to change this logic if/when we introduce a `BareModel` class with no
# protected namespaces (where `model_config` might be allowed as a field name)
continue
for protected_namespace in config_wrapper.protected_namespaces:
if ann_name.startswith(protected_namespace):
raise NameError(f'Field "{ann_name}" has conflict with protected namespace "{protected_namespace}"')
Expand Down
10 changes: 10 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,3 +622,13 @@ def test_config_wrapper_get_item():
assert config_wrapper.title == 'test'
with pytest.raises(AttributeError, match="Config has no attribute 'test'"):
config_wrapper.test


def test_config_inheritance_with_annotations():
class Parent(BaseModel):
model_config: ConfigDict = {'extra': 'allow'}

class Child(Parent):
model_config: ConfigDict = {'str_to_lower': True}

assert Child.model_config == {'extra': 'allow', 'str_to_lower': True}

0 comments on commit 216f123

Please sign in to comment.