mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
c81ec9aeec
* add support for annotation only fields, fix #34 * adding tests with mypy * adding docs for mypy usage * adding mypy failure test * adding alias tests * tweak mypy tests
27 lines
588 B
Python
27 lines
588 B
Python
from enum import Enum, IntEnum
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
class FruitEnum(str, Enum):
|
|
pear = 'pear'
|
|
banana = 'banana'
|
|
|
|
|
|
class ToolEnum(IntEnum):
|
|
spanner = 1
|
|
wrench = 2
|
|
|
|
|
|
class CookingModel(BaseModel):
|
|
fruit: FruitEnum = FruitEnum.pear
|
|
tool: ToolEnum = ToolEnum.spanner
|
|
|
|
|
|
print(CookingModel())
|
|
# > CookingModel fruit=<FruitEnum.pear: 'pear'> tool=<ToolEnum.spanner: 1>
|
|
print(CookingModel(tool=2, fruit='banana'))
|
|
# > CookingModel fruit=<FruitEnum.banana: 'banana'> tool=<ToolEnum.wrench: 2>
|
|
print(CookingModel(fruit='other'))
|
|
# will raise a validation error
|