Skip to content

Commit

Permalink
api: inline PRIMITIVES and NUMERIC_TYPES
Browse files Browse the repository at this point in the history
Make ITERATOR_TYPES a private internal symbol.
Inline the only external usage of PRIMITIVES.
Inline and remove NUMERIC_TYPES.
  • Loading branch information
davvid committed Nov 21, 2024
1 parent 30704f5 commit 126f598
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
4 changes: 2 additions & 2 deletions jsonpickle/ext/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np

from ..handlers import BaseHandler, register, unregister
from ..util import NUMERIC_TYPES, b64decode, b64encode
from ..util import b64decode, b64encode

__all__ = ['register_handlers', 'unregister_handlers']

Expand Down Expand Up @@ -189,7 +189,7 @@ def restore(self, data):
if isinstance(values, list):
# decode text representation
arr = super().restore(data)
elif isinstance(values, NUMERIC_TYPES):
elif isinstance(values, (int, float)):
# single-value array
arr = np.array([values], dtype=self.restore_dtype(data))
else:
Expand Down
7 changes: 3 additions & 4 deletions jsonpickle/pickler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from . import handlers, tags, util
from .backend import json
from .util import NUMERIC_TYPES


def encode(
Expand Down Expand Up @@ -377,7 +376,7 @@ def _flatten_impl(self, obj):
return self._flatten_bytestring(obj)

# Decimal is a primitive when use_decimal is True
if type(obj) in util.PRIMITIVES or (
if type(obj) in (str, bool, int, float, type(None)) or (
self._use_decimal and isinstance(obj, decimal.Decimal)
):
return obj
Expand Down Expand Up @@ -456,7 +455,7 @@ def _flatten_key_value_pair(self, k, v, data):
if k is None:
k = 'null' # for compatibility with common json encoders

if self.numeric_keys and isinstance(k, NUMERIC_TYPES):
if self.numeric_keys and isinstance(k, (int, float)):
pass
elif not isinstance(k, str):
try:
Expand Down Expand Up @@ -761,7 +760,7 @@ def _flatten_string_key_value_pair(self, k, v, data):
if k is None:
k = 'null' # for compatibility with common json encoders

if self.numeric_keys and isinstance(k, NUMERIC_TYPES):
if self.numeric_keys and isinstance(k, (int, float)):
pass
elif not isinstance(k, str):
try:
Expand Down
7 changes: 3 additions & 4 deletions jsonpickle/unpickler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from . import errors, handlers, tags, util
from .backend import json
from .util import NUMERIC_TYPES


def decode(
Expand Down Expand Up @@ -653,7 +652,7 @@ def _restore_from_dict(
# ignore the reserved attribute
if ignorereserved and k in tags.RESERVED:
continue
if isinstance(k, NUMERIC_TYPES):
if isinstance(k, (int, float)):
str_k = k.__str__()
else:
str_k = k
Expand Down Expand Up @@ -868,7 +867,7 @@ def _restore_dict(self, obj):
for k, v in util.items(obj):
if _is_json_key(k):
continue
if isinstance(k, NUMERIC_TYPES):
if isinstance(k, (int, float)):
str_k = k.__str__()
else:
str_k = k
Expand All @@ -893,7 +892,7 @@ def _restore_dict(self, obj):
else:
# No special keys, thus we don't need to restore the keys either.
for k, v in util.items(obj):
if isinstance(k, NUMERIC_TYPES):
if isinstance(k, (int, float)):
str_k = k.__str__()
else:
str_k = k
Expand Down
5 changes: 2 additions & 3 deletions jsonpickle/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

from . import tags

NUMERIC_TYPES = (int, float)
ITERATOR_TYPE = type(iter(''))
_ITERATOR_TYPE = type(iter(''))
SEQUENCES = (list, set, tuple)
SEQUENCES_SET = {list, set, tuple}
PRIMITIVES = {str, bool, int, float, type(None)}
Expand Down Expand Up @@ -382,7 +381,7 @@ def is_reducible(obj):
# Condensing it into one line seems to save the parser a lot of time.
# fmt: off
# pylint: disable=line-too-long
if type(obj) in NON_REDUCIBLE_TYPES or obj is object or is_dictionary_subclass(obj) or isinstance(obj, types.ModuleType) or is_reducible_sequence_subclass(obj) or is_list_like(obj) or isinstance(getattr(obj, '__slots__', None), ITERATOR_TYPE) or (is_type(obj) and obj.__module__ == 'datetime'): # noqa: E501
if type(obj) in NON_REDUCIBLE_TYPES or obj is object or is_dictionary_subclass(obj) or isinstance(obj, types.ModuleType) or is_reducible_sequence_subclass(obj) or is_list_like(obj) or isinstance(getattr(obj, '__slots__', None), _ITERATOR_TYPE) or (is_type(obj) and obj.__module__ == 'datetime'): # noqa: E501
return False
# fmt: on
return True
Expand Down

0 comments on commit 126f598

Please sign in to comment.