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 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
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'