Browse Source

⚒️ format script

pull/13982/head
Ben Brady 4 weeks ago
parent
commit
1f86213ffc
  1. 3
      fastapi/encoders.py
  2. 9
      tests/test_jsonable_encoder.py

3
fastapi/encoders.py

@ -14,7 +14,7 @@ from ipaddress import (
from pathlib import Path, PurePath
from re import Pattern
from types import GeneratorType
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union
from typing import Any, Callable, Dict, Optional, Tuple, Type, Union
from uuid import UUID
from fastapi.types import IncEx
@ -102,6 +102,7 @@ encoders_by_class_tuples = generate_encoders_by_class_tuples(ENCODERS_BY_TYPE)
NoneType = type(None)
def jsonable_encoder(
obj: Annotated[
Any,

9
tests/test_jsonable_encoder.py

@ -102,21 +102,20 @@ def test_encode_dict_with_nonprimative_keys():
return hash(self.value)
assert jsonable_encoder(
{CustomString("foo"): "bar"},
custom_encoder={CustomString: lambda v: v.value}
{CustomString("foo"): "bar"}, custom_encoder={CustomString: lambda v: v.value}
) == {"foo": "bar"}
def test_encode_dict_with_custom_encoder_keys():
assert jsonable_encoder(
{"foo": "bar"},
custom_encoder={str: lambda v: "_" + v}
{"foo": "bar"}, custom_encoder={str: lambda v: "_" + v}
) == {"_foo": "_bar"}
def test_encode_dict_with_sqlalchemy_safe():
obj = {"_sa_foo": "foo", "bar": "bar"}
assert jsonable_encoder(obj, sqlalchemy_safe=True) == {"bar": "bar"}
assert jsonable_encoder(obj, sqlalchemy_safe=False ) == obj
assert jsonable_encoder(obj, sqlalchemy_safe=False) == obj
def test_encode_dict_with_exclude_none():

Loading…
Cancel
Save