Skip to content

Commit

Permalink
Remove JsonDict
Browse files Browse the repository at this point in the history
  • Loading branch information
dmontagu committed Nov 2, 2023
1 parent aaefb19 commit 30de9c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
1 change: 0 additions & 1 deletion pydantic/__init__.py
Expand Up @@ -325,7 +325,6 @@
'Tag': (__package__, '.types'),
'CallableDiscriminator': (__package__, '.types'),
'JsonValue': (__package__, '.types'),
'JsonDict': (__package__, '.types'),
# type_adapter
'TypeAdapter': (__package__, '.type_adapter'),
# warnings
Expand Down
28 changes: 24 additions & 4 deletions pydantic/types.py
Expand Up @@ -30,7 +30,7 @@
import annotated_types
from annotated_types import BaseMetadata, MaxLen, MinLen
from pydantic_core import CoreSchema, PydanticCustomError, core_schema
from typing_extensions import Annotated, Literal, Protocol, TypeAlias, TypeAliasType, deprecated
from typing_extensions import Annotated, Literal, Protocol, TypeAliasType, deprecated

from ._internal import (
_core_utils,
Expand Down Expand Up @@ -103,7 +103,6 @@
'StringConstraints',
'Tag',
'CallableDiscriminator',
'JsonDict',
'JsonValue',
)

Expand Down Expand Up @@ -2665,8 +2664,30 @@ def _convert_schema(self, original_schema: core_schema.CoreSchema) -> core_schem
)


_JSON_TYPES = {int, float, str, bool, list, dict, type(None)}


def _get_type_name(x: Any) -> str:
return getattr(type(x), '__name__', '<no name>')
type_ = type(x)
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'
if isinstance(x, int):
return 'int'
if isinstance(x, float):
return 'float'
if isinstance(x, str):
return 'str'
if isinstance(x, list):
return 'list'
if isinstance(x, dict):
return 'dict'

# Fail by returning the type's actual name
return getattr(type_, '__name__', '<no type name>')


class _AllowAnyJson:
Expand All @@ -2692,4 +2713,3 @@ def __get_pydantic_core_schema__(cls, source_type: Any, handler: GetCoreSchemaHa
_AllowAnyJson,
],
)
JsonDict: TypeAlias = Dict[str, JsonValue]

0 comments on commit 30de9c9

Please sign in to comment.