|
10 | 10 | BeforeValidator,
|
11 | 11 | ConfigDict,
|
12 | 12 | Field,
|
| 13 | + PlainSerializer, |
13 | 14 | UrlConstraints,
|
14 | 15 | )
|
15 | 16 | from pydantic_core import Url
|
@@ -77,7 +78,7 @@ def none_to_empty_dict(value: T | None) -> T | dict[Never]:
|
77 | 78 | return value
|
78 | 79 |
|
79 | 80 |
|
80 |
| -NoneIsEmptyDict = Annotated[dict[T], BeforeValidator(none_to_empty_dict)] |
| 81 | +NoneIsEmptyDict = Annotated[dict[K, V], BeforeValidator(none_to_empty_dict)] |
81 | 82 | """
|
82 | 83 | A generic dict type that converts `None` to an empty dict.
|
83 | 84 | This should not be needed if this proper data model is used in production.
|
@@ -151,22 +152,15 @@ def parse_rfc_2822_date(value: str) -> datetime:
|
151 | 152 | return email.utils.parsedate_to_datetime(value)
|
152 | 153 |
|
153 | 154 |
|
154 |
| -RFC2822Date = Annotated[datetime, BeforeValidator(parse_rfc_2822_date)] |
155 |
| - |
156 |
| - |
157 |
| -def none_to_empty_dict(value: T | None) -> T | dict[Never, Never]: |
158 |
| - """ |
159 |
| - Convert `None` to an empty dictionary f, otherwise keep the value as is. |
160 |
| - """ |
161 |
| - if value is None: |
162 |
| - return {} |
163 |
| - return value |
| 155 | +def serialize_rfc_2822_date(value: datetime) -> str: |
| 156 | + return email.utils.format_datetime(value) |
164 | 157 |
|
165 | 158 |
|
166 |
| -NoneIsEmptyDict = Annotated[dict[K, V], BeforeValidator(none_to_empty_dict)] |
167 |
| -""" |
168 |
| -A generic dict type that converts `None` to an empty dict. |
169 |
| -""" |
| 159 | +RFC2822Date = Annotated[ |
| 160 | + datetime, |
| 161 | + BeforeValidator(parse_rfc_2822_date), |
| 162 | + PlainSerializer(serialize_rfc_2822_date), |
| 163 | +] |
170 | 164 |
|
171 | 165 |
|
172 | 166 | GitUrl = Annotated[Url, UrlConstraints(allowed_schemes=["git"])]
|
|
0 commit comments