Skip to content

Commit

Permalink
add use_enum_values to config (#127)
Browse files Browse the repository at this point in the history
* add `use_enum_values` to config

* docs and history
  • Loading branch information
samuelcolvin authored Feb 6, 2018
1 parent eeb5698 commit 7a77d06
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
3 changes: 2 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ v0.7.0 (2018-02-06)
* add ``create_model`` method #113 #125
* **breaking change**: rename ``.config`` to ``.__config__`` on a model
* **breaking change**: remove deprecated ``.values()`` on a model, use ``.dict()`` instead
* remove use of `OrderedDict` and use simple dict #126
* remove use of ``OrderedDict`` and use simple dict #126
* add ``Config.use_enum_values`` #127

v0.6.4 (2018-02-01)
...................
Expand Down
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ Options:
:ignore_extra: whether to ignore any extra values in input data (default: ``True``)
:allow_extra: whether or not too allow (and include on the model) any extra values in input data (default: ``False``)
:allow_mutation: whether or not models are faux-immutable, e.g. __setattr__ fails (default: ``True``)
:use_enum_values: whether to populate models with the ``value`` property of enums,
rather than the raw enum - useful if you want to serialise ``model.dict()`` later (default: ``False``)
:fields: extra information on each field, currently just "alias" is allowed (default: ``None``)
:validate_assignment: whether to perform validation on assignment to attributes or not (default: ``False``)

Expand Down
1 change: 1 addition & 0 deletions pydantic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BaseConfig:
ignore_extra = True
allow_extra = False
allow_mutation = True
use_enum_values = False
fields = {}
validate_assignment = False

Expand Down
5 changes: 3 additions & 2 deletions pydantic/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ def set_validator(v) -> set:
return set(v)


def enum_validator(v, field, **kwargs) -> Enum:
return field.type_(v)
def enum_validator(v, field, config, **kwargs) -> Enum:
enum_v = field.type_(v)
return enum_v.value if config.use_enum_values else enum_v


def uuid_validator(v) -> UUID:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from enum import Enum
from typing import Any

import pytest
Expand Down Expand Up @@ -369,3 +370,29 @@ def test_validating_assignment_fail():
assert """error validating input
b:
length less than minimum allowed: 1 (error_type=ValueError track=ConstrainedStrValue)""" == str(exc_info.value)


def test_enum_values():
FooEnum = Enum('FooEnum', {'foo': 'foo', 'bar': 'bar'})

class Model(BaseModel):
foo: FooEnum = None

class Config:
use_enum_values = True

m = Model(foo='foo')
# this is the actual value, so has not "values" field
assert not isinstance(m.foo, FooEnum)
assert m.foo == 'foo'


def test_enum_raw():
FooEnum = Enum('FooEnum', {'foo': 'foo', 'bar': 'bar'})

class Model(BaseModel):
foo: FooEnum = None
m = Model(foo='foo')
assert isinstance(m.foo, FooEnum)
assert m.foo != 'foo'
assert m.foo.value == 'foo'

0 comments on commit 7a77d06

Please sign in to comment.