Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@

Release 1.9.4
=========================================

* **BUGFIX:** Fixed ``TypeError`` which occurred on certain ``np.isnan()`` operations when serializing to JS
Literal (courtesy of @ThomasGL) (closes #200).

----

Release 1.9.3
=========================================

Expand Down
3 changes: 2 additions & 1 deletion docs/_contributors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
* Byron Cook (`@ByronCook <https://github.com/ByronCook>`__)
* karlacio (`@karlacio <https://github.com/karlacio>`__)
* Max Dugan Knight (`@maxduganknight <https://github.com/maxduganknight>`__)
* Julien Bacquart (`@JulienBacquart <https://github.com/JulienBacquart>`__)
* Julien Bacquart (`@JulienBacquart <https://github.com/JulienBacquart>`__)
* Thomas Glezer (`@ThomasGL <https://github.com/ThomasGl>`__)
2 changes: 1 addition & 1 deletion highcharts_core/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.9.3'
__version__ = '1.9.4'
4 changes: 3 additions & 1 deletion highcharts_core/js_literal_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,9 @@ def get_js_literal(item, careful_validation = False) -> str:
as_str += f"""'{item}'"""
else:
as_str += f"""{item}"""
elif item == constants.EnforcedNull:
elif item == constants.EnforcedNull or item is None:
as_str += """null"""
elif HAS_NUMPY and not isinstance(item, (dict, UserDict, Decimal)) and np.isnan(item):
as_str += """null"""
elif item is True:
as_str += """true"""
Expand Down
75 changes: 74 additions & 1 deletion tests/test_js_literal_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
from decimal import Decimal

import esprima
try:
import numpy as np
HAS_NUMPY = True
except ImportError:
HAS_NUMPY = False

from validator_collection import checkers


Expand Down Expand Up @@ -208,4 +214,71 @@ def test_is_js_object(original_str, expected, error):
assert result is expected
else:
with pytest.raises(error):
result = js.is_js_object(original_str)
result = js.is_js_object(original_str)


@pytest.mark.parametrize('original_value, error', [
({'test', 123}, TypeError),
([{'test': 123}], TypeError),
({'test': {'subtest': 123}}, TypeError),
({'test': 'string'}, TypeError),
('string', TypeError),
(True, None),
(False, None),
(123, None),
(123.45, None),
(Decimal(123.45), TypeError),
([1, 2, 3], None),
([1.2, 3.4, 5.6], None),
(['string', 1, 2, 3], TypeError),
(['string', [1, 2, 3]], ValueError),
(['test', None], TypeError),
(None, TypeError),
])
def test_np_isnan(original_value, error):
print(f'ORIGINAL VALUE: {original_value}')
print(f'{type(original_value)}')
if HAS_NUMPY and not error:
result = np.isnan(original_value)
print(f'RESULT: {result}')
if not isinstance(result, np.ndarray):
assert result is False or not result
else:
assert result.all() is False or not result.all()
elif HAS_NUMPY:
with pytest.raises(error):
result = np.isnan(original_value)
else:
pass


@pytest.mark.parametrize('original_value, expected, error', [
({'test': 123}, """{'test': 123}""", None),
([{'test': 123}], """[{'test': 123}]""", None),
({'test': {'subtest': 123}}, """{'test': {'subtest': 123}}""", None),
({'test': 'string'}, """{'test': 'string'}""", None),
('string', """'string'""", None),
(True, """true""", None),
(False, """false""", None),
(123, """123""", None),
(123.45, """123.45""", None),
(Decimal(123.45), """123.45""", None),
([1, 2, 3], """[1,\n2,\n3]""", None),
([1.2, 3.4, 5.6], """[1.2,\n3.4,\n5.6]""", None),
(['string', 1, 2, 3], """['string',\n1,\n2,\n3]""", None),
(['string', [1, 2, 3]], """['string',\n[1,\n2,\n3]]""", None),
(['test', None], """['test',\nnull]""", None),
(None, """null""", None),
])
def test_get_js_literal(original_value, expected, error):
print(f'ORIGINAL VALUE: {original_value}')
if not error:
result = js.get_js_literal(original_value)
if not isinstance(original_value, Decimal):
assert result == expected
else:
assert result == str(original_value)
else:
with pytest.raises(error):
result = js.get_js_literal(original_value)

Loading