diff --git a/changes/2532-uriyyo.md b/changes/2532-uriyyo.md new file mode 100644 index 0000000000..8ec81ae4a4 --- /dev/null +++ b/changes/2532-uriyyo.md @@ -0,0 +1,2 @@ +Fix bug with configurations declarations that are passed as +keyword arguments during class creation. \ No newline at end of file diff --git a/pydantic/main.py b/pydantic/main.py index f6aca41048..0df761bbd7 100644 --- a/pydantic/main.py +++ b/pydantic/main.py @@ -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_config_kwargs: SetStr = { + key + for key in dir(config) + if not (key.startswith('__') and key.endswith('__')) # skip dunder methods and attributes + } + config_kwargs = {key: kwargs.pop(key) for key in kwargs.keys() & allowed_config_kwargs} 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') diff --git a/tests/test_main.py b/tests/test_main.py index 328e89a2b3..0c868404e9 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -7,6 +7,7 @@ import pytest from pydantic import ( + BaseConfig, BaseModel, ConfigError, Extra, @@ -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'