Skip to content

Commit

Permalink
refactor: set Pattern encoder in ENCODERS_BY_TYPE (#2444)
Browse files Browse the repository at this point in the history
* refactor: set `Pattern` encoder in `ENCODERS_BY_TYPE`

* docs: add change file
  • Loading branch information
PrettyWood committed Mar 2, 2021
1 parent 9bd2da7 commit b2d3f33
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions changes/2444-PrettyWood.md
@@ -0,0 +1 @@
expose `Pattern` encoder to `fastapi`
14 changes: 10 additions & 4 deletions pydantic/json.py
@@ -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

Expand Down Expand Up @@ -58,6 +66,7 @@ def decimal_encoder(dec_value: Decimal) -> Union[int, float]:
IPv6Interface: str,
IPv6Network: str,
Path: str,
Pattern: lambda o: o.pattern,
SecretBytes: str,
SecretStr: str,
set: list,
Expand All @@ -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:
Expand Down

0 comments on commit b2d3f33

Please sign in to comment.