refactor: set Pattern encoder in ENCODERS_BY_TYPE (#2444)

* refactor: set `Pattern` encoder in `ENCODERS_BY_TYPE`

* docs: add change file
This commit is contained in:
Eric Jolibois
2021-03-02 13:05:57 +01:00
committed by GitHub
parent 9bd2da7805
commit b2d3f333f0
2 changed files with 11 additions and 4 deletions
+1
View File
@@ -0,0 +1 @@
expose `Pattern` encoder to `fastapi`
+10 -4
View File
@@ -1,13 +1,21 @@
import datetime
import re
import sys
from collections import deque
from decimal import Decimal
from enum import Enum
from ipaddress import IPv4Address, IPv4Interface, IPv4Network, IPv6Address, IPv6Interface, IPv6Network
from pathlib import Path
from types import GeneratorType
from typing import Any, Callable, Dict, Pattern, Type, Union
from typing import Any, Callable, Dict, Type, Union
from uuid import UUID
if sys.version_info >= (3, 7):
Pattern = re.Pattern
else:
# python 3.6
Pattern = re.compile('a').__class__
from .color import Color
from .types import SecretBytes, SecretStr
@@ -58,6 +66,7 @@ ENCODERS_BY_TYPE: Dict[Type[Any], Callable[[Any], Any]] = {
IPv6Interface: str,
IPv6Network: str,
Path: str,
Pattern: lambda o: o.pattern,
SecretBytes: str,
SecretStr: str,
set: list,
@@ -75,9 +84,6 @@ def pydantic_encoder(obj: Any) -> Any:
elif is_dataclass(obj):
return asdict(obj)
if isinstance(obj, Pattern):
return obj.pattern
# Check the class type and its superclasses for a matching encoder
for base in obj.__class__.__mro__[:-1]:
try: