Skip to content

Commit

Permalink
Fix issue with config decl at class level (#2532)
Browse files Browse the repository at this point in the history
* Fix issue with config decl at class level

* Add changes

* Update pydantic/main.py

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>

* Update changes

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
  • Loading branch information
2 people authored and samuelcolvin committed May 11, 2021
1 parent f1fd5c2 commit 00c55be
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changes/2532-uriyyo.md
@@ -0,0 +1,2 @@
Fix bug with configurations declarations that are passed as
keyword arguments during class creation.
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_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')
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'

0 comments on commit 00c55be

Please sign in to comment.