Skip to content

Commit

Permalink
Fix Python to JavaScript int conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Dec 22, 2023
1 parent 622d918 commit e8671ba
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ jobs:
run: mypy python

- name: Run tests
run: pytest -v tests
run: pytest --color=yes -v tests
7 changes: 6 additions & 1 deletion src/type_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,13 @@ pub fn py_to_any(value: &PyAny) -> Any {
let v: bool = value.extract().unwrap();
Any::Bool(v)
} else if value.is_instance_of::<PyLong>() {
const MAX_JS_NUMBER: i64 = 2_i64.pow(53) - 1;
let v: i64 = value.extract().unwrap();
Any::BigInt(v)
if v > MAX_JS_NUMBER {
Any::BigInt(v)
} else {
Any::Number(v as f64)
}
} else if value.is_instance_of::<PyFloat>() {
let v: f64 = value.extract().unwrap();
Any::Number(v)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_str():
map1 = Map({"foo": array1})
array0 = Array([0, 1, None, map1])
doc["array"] = array0
assert str(array0) == '[0,1,null,{"foo":[2,3,{"key":"val"}]}]'
assert str(array0) == '[0.0,1.0,null,{"foo":[2.0,3.0,{"key":"val"}]}]'


def test_nested():
Expand Down Expand Up @@ -120,18 +120,18 @@ def test_api():
assert v == 3
v = array.pop(0)
assert v == 1
assert str(array) == "[2]"
assert str(array) == "[2.0]"

# insert
doc = Doc()
array = Array([1, 2, 3])
doc["array"] = array
array.insert(1, 4)
assert str(array) == "[1,4,2,3]"
assert str(array) == "[1.0,4.0,2.0,3.0]"


def test_move():
doc = Doc()
doc["array"] = array = Array([1, 2, 3, 4])
array.move(1, 3)
assert str(array) == "[1,3,2,4]"
assert str(array) == "[1.0,3.0,2.0,4.0]"
4 changes: 2 additions & 2 deletions tests/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_str():
array1 = Array([0, 1, map2])
map0 = Map({"key1": array1})
doc["map"] = map0
assert str(map0) == '{"key1":[0,1,{"key2":"val2"}]}'
assert str(map0) == '{"key1":[0.0,1.0,{"key2":"val2"}]}'


def test_nested():
Expand Down Expand Up @@ -52,7 +52,7 @@ def test_api():
doc["map0"] = map0
v = map0.pop("foo")
assert v == 1
assert str(map0) == '{"bar":2}'
assert str(map0) == '{"bar":2.0}'
v = map0.pop("bar")
assert v == 2
assert str(map0) == "{}"
2 changes: 1 addition & 1 deletion tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ def callback(event):
text += "hello"
array.append(1)
map_["foo"] = "bar"
assert events == ["hello", "[1]", '{"foo":"bar"}']
assert events == ["hello", "[1.0]", '{"foo":"bar"}']

0 comments on commit e8671ba

Please sign in to comment.