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

Added tests for the case JsonValue contains subclassed primitive values #8286

Merged
merged 1 commit into from Dec 4, 2023
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
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