mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
e58dad6dec
* Added support for Rich Repr protocol * restore line * Words * quotes * Update changes/3099-willmcgugan.md Adds code to markdown Co-authored-by: Eric Jolibois <em.jolibois@gmail.com> * added usage with Rich Co-authored-by: Eric Jolibois <em.jolibois@gmail.com> Co-authored-by: Samuel Colvin <s@muelcolvin.com>
33 lines
684 B
Python
33 lines
684 B
Python
from datetime import datetime
|
|
from typing import List, Optional
|
|
|
|
from pydantic import BaseModel
|
|
from pydantic.color import Color
|
|
|
|
|
|
class User(BaseModel):
|
|
id: int
|
|
name: str = 'John Doe'
|
|
signup_ts: Optional[datetime] = None
|
|
friends: List[int] = []
|
|
|
|
|
|
def test_rich_repr() -> None:
|
|
user = User(id=22)
|
|
rich_repr = list(user.__rich_repr__())
|
|
|
|
assert rich_repr == [
|
|
('id', 22),
|
|
('name', 'John Doe'),
|
|
('signup_ts', None),
|
|
('friends', []),
|
|
]
|
|
|
|
|
|
def test_rich_repr_color() -> None:
|
|
|
|
color = Color((10, 20, 30, 0.1))
|
|
rich_repr = list(color.__rich_repr__())
|
|
|
|
assert rich_repr == ['#0a141e1a', ('rgb', (10, 20, 30, 0.1))]
|