Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: set Pattern encoder in ENCODERS_BY_TYPE #2444

Merged
merged 2 commits into from Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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