Skip to content

Commit

Permalink
Don't check for subclass of bool for JsonValue (#8286)
Browse files Browse the repository at this point in the history
  • Loading branch information
jusexton committed Dec 4, 2023
1 parent 8bbb3c1 commit 8d2e6f2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 1 addition & 3 deletions pydantic/types.py
Expand Up @@ -2704,9 +2704,7 @@ def _get_type_name(x: Any) -> str:
if type_ in _JSON_TYPES:
return type_.__name__

# Handle proper subclasses; note we don't need to handle None here
if isinstance(x, bool):
return 'bool'
# Handle proper subclasses; note we don't need to handle None or bool here
if isinstance(x, int):
return 'int'
if isinstance(x, float):
Expand Down
21 changes: 21 additions & 0 deletions tests/test_types.py
Expand Up @@ -6126,6 +6126,27 @@ def test_json_value():
]


def test_json_value_with_subclassed_types():
class IntType(int):
pass

class FloatType(float):
pass

class StrType(str):
pass

class ListType(list):
pass

class DictType(dict):
pass

adapter = TypeAdapter(JsonValue)
valid_json_data = {'int': IntType(), 'float': FloatType(), 'str': StrType(), 'list': ListType(), 'dict': DictType()}
assert adapter.validate_python(valid_json_data) == valid_json_data


def test_json_value_roundtrip() -> None:
# see https://github.com/pydantic/pydantic/issues/8175
class MyModel(BaseModel):
Expand Down

0 comments on commit 8d2e6f2

Please sign in to comment.