Skip to content

Commit

Permalink
feat: add global config
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood committed Dec 12, 2020
1 parent 4da511e commit 012c828
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
4 changes: 3 additions & 1 deletion pydantic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# flake8: noqa
from . import dataclasses
from . import dataclasses, global_config
from .class_validators import root_validator, validator
from .decorator import validate_arguments
from .env_settings import BaseSettings
Expand All @@ -16,6 +16,8 @@
# WARNING __all__ from .errors is not included here, it will be removed as an export here in v2
# please use "from pydantic.errors import ..." instead
__all__ = [
# global config
'global_config',
# dataclasses
'dataclasses',
# class_validators
Expand Down
1 change: 1 addition & 0 deletions pydantic/global_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COPY_ON_MODEL_VALIDATION = True
4 changes: 3 additions & 1 deletion pydantic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,9 @@ def validate(cls: Type['Model'], value: Any) -> 'Model':
if isinstance(value, dict):
return cls(**value)
elif isinstance(value, cls):
return value
from .global_config import COPY_ON_MODEL_VALIDATION

return value.copy() if COPY_ON_MODEL_VALIDATION else value
elif cls.__config__.orm_mode:
return cls.from_orm(value)
elif cls.__custom_root_type__:
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,13 @@ def run(source_code_or_function, rewrite_assertions=True):
return module

return run


@pytest.fixture
def mock_global_config(mocker):
def inner(global_config_flag: str, desired_flag_value: bool):
import pydantic.global_config as GLOBAL_CONFIG

mocker.patch.object(GLOBAL_CONFIG, global_config_flag, new=desired_flag_value)

return inner
32 changes: 24 additions & 8 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1427,21 +1427,37 @@ class M(BaseModel):
get_type_hints(M.__config__)


def test_inherited_model_field():
def test_inherited_model_field_default():
"""By default submodels are copied on validation"""

class Image(BaseModel):
path: str

def __hash__(self):
return id(self)

class Item(BaseModel):
images: List[Image]
image: Image

image = Image(path='my_image1.png')

item = Item(image=image)
assert item.image is not image


def test_inherited_model_field_no_copy(mock_global_config):
mock_global_config('COPY_ON_MODEL_VALIDATION', False)

class Image(BaseModel):
path: str

def __hash__(self):
return id(self)

image_1 = Image(path='my_image1.png')
image_2 = Image(path='my_image2.png')
class Item(BaseModel):
image: Image

item = Item(images={image_1, image_2})
print(image_1 in item.images)
image = Image(path='my_image1.png')

assert id(image_1) == id(item.images[0])
assert id(image_2) == id(item.images[1])
item = Item(image=image)
assert item.image is image

0 comments on commit 012c828

Please sign in to comment.