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 issue with config decl at class level #2532

Merged
merged 4 commits into from May 9, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions changes/2532-uriyyo.md
@@ -0,0 +1 @@
Fix issue with config decl at class level.
uriyyo marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 6 additions & 1 deletion pydantic/main.py
Expand Up @@ -248,7 +248,12 @@ def __new__(mcs, name, bases, namespace, **kwargs): # noqa C901
class_vars.update(base.__class_vars__)
hash_func = base.__hash__

config_kwargs = {key: kwargs.pop(key) for key in kwargs.keys() & BaseConfig.__dict__.keys()}
allowed_configs: SetStr = {
key
for key in dir(config)
if not (key.startswith('__') and key.endswith('__')) # skip dander methods and attributes
}
config_kwargs = {key: kwargs.pop(key) for key in kwargs.keys() & allowed_configs}
uriyyo marked this conversation as resolved.
Show resolved Hide resolved
config_from_namespace = namespace.get('Config')
if config_kwargs and config_from_namespace:
raise TypeError('Specifying config in two places is ambiguous, use either Config attribute or class kwargs')
Expand Down
12 changes: 12 additions & 0 deletions tests/test_main.py
Expand Up @@ -7,6 +7,7 @@
import pytest

from pydantic import (
BaseConfig,
BaseModel,
ConfigError,
Extra,
Expand Down Expand Up @@ -1734,3 +1735,14 @@ class Model(BaseModel, extra='allow'):

class Config:
extra = 'forbid'


def test_class_kwargs_custom_config():
class Base(BaseModel):
class Config(BaseConfig):
some_config = 'value'

class Model(Base, some_config='new_value'):
a: int

assert Model.__config__.some_config == 'new_value'