Skip to content

Commit

Permalink
adds model_name option in ConfigDict
Browse files Browse the repository at this point in the history
  • Loading branch information
Rômulo Collopy committed Jul 23, 2023
1 parent 5320764 commit 1ef95b6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pydantic/_internal/_config.py
Expand Up @@ -28,6 +28,7 @@ class ConfigWrapper:
# all annotations are copied directly from ConfigDict, and should be kept up to date, a test will fail if they
# stop matching
title: str | None
model_name: str | None
str_to_lower: bool
str_to_upper: bool
str_strip_whitespace: bool
Expand Down Expand Up @@ -165,6 +166,7 @@ def __repr__(self):

config_defaults = ConfigDict(
title=None,
model_name=None,
str_to_lower=False,
str_to_upper=False,
str_strip_whitespace=False,
Expand Down
1 change: 1 addition & 0 deletions pydantic/_internal/_model_construction.py
Expand Up @@ -93,6 +93,7 @@ def __new__(
base_field_names, class_vars, base_private_attributes = mcs._collect_bases_data(bases)

config_wrapper = ConfigWrapper.for_model(bases, namespace, kwargs)
cls_name = config_wrapper.config_dict.get("model_name") or cls_name
namespace['model_config'] = config_wrapper.config_dict
private_attributes = inspect_namespace(
namespace, config_wrapper.ignored_types, class_vars, base_field_names
Expand Down
1 change: 1 addition & 0 deletions pydantic/config.py
Expand Up @@ -128,6 +128,7 @@ class without an annotation and has a type that is not in this tuple (or otherwi
"""

title: str | None
model_name: str | None
str_to_lower: bool
str_to_upper: bool
str_strip_whitespace: bool
Expand Down
28 changes: 28 additions & 0 deletions tests/test_config.py
Expand Up @@ -23,6 +23,7 @@
from pydantic.config import ConfigDict
from pydantic.dataclasses import dataclass as pydantic_dataclass
from pydantic.errors import PydanticUserError
from pydantic.v1.schema import get_model_name_map

if sys.version_info < (3, 9):
from typing_extensions import Annotated
Expand Down Expand Up @@ -484,6 +485,33 @@ def test_invalid_config_keys():
def my_function():
pass

def test_config_model_name() -> None:
CLIENT_USER_MODEL_NAME = "ClientUser"
BUSINESS_USER_MODEL_NAME = "BusinessUser"


def _get_business_user_class():
class User(BaseModel):
model_config = ConfigDict(model_name=BUSINESS_USER_MODEL_NAME)

return User

def _get_client_user_class():
class User(BaseModel):
model_config = ConfigDict(model_name=CLIENT_USER_MODEL_NAME)

return User

BusinessUser = _get_business_user_class()
ClientUser = _get_client_user_class()

name_map = get_model_name_map({BusinessUser, ClientUser})
assert name_map[BusinessUser] == BUSINESS_USER_MODEL_NAME
assert name_map[ClientUser] == CLIENT_USER_MODEL_NAME

assert BusinessUser().model_json_schema()["title"] == BUSINESS_USER_MODEL_NAME
assert ClientUser().model_json_schema()["title"] == CLIENT_USER_MODEL_NAME


def test_multiple_inheritance_config():
class Parent(BaseModel):
Expand Down

0 comments on commit 1ef95b6

Please sign in to comment.