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

Fix Python to JavaScript int conversion #52

Merged
merged 1 commit into from
Dec 22, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"}']