Skip to content

Commit 5dda057

Browse files
author
Diego Ardila
committed
Nan fixes, back to python 3.6
1 parent a5447b7 commit 5dda057

File tree

7 files changed

+67
-38
lines changed

7 files changed

+67
-38
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version: 2.1
44
jobs:
55
build_test:
66
docker:
7-
- image: python:3.7-slim-buster
7+
- image: python:3.6-slim-buster
88
resource_class: small
99
steps:
1010
- checkout # checkout source code to working directory
@@ -18,7 +18,7 @@ jobs:
1818
- run:
1919
name: Black Formatting Check # Only validation, without re-formatting
2020
command: |
21-
poetry run black --check -t py37 .
21+
poetry run black --check -t py36 .
2222
- run:
2323
name: Flake8 Lint Check # Uses setup.cfg for configuration
2424
command: |

nucleus/__init__.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,14 @@ def _process_append_requests_local(
402402
):
403403
def get_files(batch):
404404
request_payload = [
405-
(ITEMS_KEY, (None, json.dumps(batch), "application/json"))
405+
(
406+
ITEMS_KEY,
407+
(
408+
None,
409+
json.dumps(batch, allow_nan=False),
410+
"application/json",
411+
),
412+
)
406413
]
407414
for item in batch:
408415
image = open( # pylint: disable=R1732
@@ -426,7 +433,8 @@ def get_files(batch):
426433
files_per_request.append(get_files(batch))
427434
payload_items.append(batch)
428435

429-
responses = asyncio.run(
436+
loop = asyncio.get_event_loop()
437+
responses = loop.run_until_complete(
430438
self.make_many_files_requests_asynchronously(
431439
files_per_request,
432440
f"dataset/{dataset_id}/append",
@@ -476,7 +484,7 @@ async def _make_files_request(
476484
"""
477485
Makes an async post request with files to a Nucleus endpoint.
478486
479-
:param files: A list of tuples (filename, file_pointer, file_type)
487+
:param files: A list of tuples (name, (filename, file_pointer, file_type))
480488
:param route: route for the request
481489
:param session: Session to use for post.
482490
:return: response
@@ -502,12 +510,21 @@ async def _make_files_request(
502510
timeout=DEFAULT_NETWORK_TIMEOUT_SEC,
503511
) as response:
504512
logger.info("API request has response code %s", response.status)
513+
514+
try:
515+
data = await response.json()
516+
except aiohttp.client_exceptions.ContentTypeError:
517+
# In case of 404, the server returns text
518+
data = await response.text()
519+
505520
if not response.ok:
506521
self.handle_bad_response(
507-
endpoint, session.post, aiohttp_response=response
522+
endpoint,
523+
session.post,
524+
aiohttp_response=(response.status, response.reason, data),
508525
)
509526

510-
return await response.json()
527+
return data
511528

512529
def _process_append_requests(
513530
self,

nucleus/annotation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def to_payload(self):
5353
)
5454

5555
def to_json(self) -> str:
56-
return json.dumps(self.to_payload())
56+
return json.dumps(self.to_payload(), allow_nan=False)
5757

5858

5959
@dataclass

nucleus/dataset_item.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def to_payload(self) -> dict:
5252
return payload
5353

5454
def to_json(self) -> str:
55-
return json.dumps(self.to_payload())
55+
return json.dumps(self.to_payload(), allow_nan=False)
5656

5757

5858
def is_local_path(path: str) -> bool:

nucleus/errors.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ def __init__(
3838
)
3939

4040
if aiohttp_response is not None:
41-
message = f"Tried to {command.__name__} {endpoint}, but received {aiohttp_response.status}: {aiohttp_response.reason}."
42-
if hasattr(requests_response, "text"):
43-
text = requests_response.text()
44-
if text:
45-
message += f"\nThe detailed error is:\n{text}"
41+
status, reason, data = aiohttp_response
42+
message = f"Tried to {command.__name__} {endpoint}, but received {status}: {reason}."
43+
if data:
44+
message += f"\nThe detailed error is:\n{data}"
4645

4746
super().__init__(message)

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ documentation = "https://dashboard.scale.com/nucleus/docs/api"
3232
packages = [{include="nucleus"}]
3333

3434
[tool.poetry.dependencies]
35-
python = "^3.7.0"
36-
requests = "^2.25.1"
35+
python = "^3.6.2"
36+
requests = "^2.23.0"
3737
tqdm = "^4.41.0"
3838
dataclasses = { version = "^0.7", python = "^3.6.1, <3.7" }
3939
aiohttp = "^3.7.4"

tests/test_dataset.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
1+
import math
2+
import os
3+
4+
import pytest
5+
from nucleus import (
6+
Dataset,
7+
DatasetItem,
8+
NucleusAPIError,
9+
NucleusClient,
10+
UploadResponse,
11+
)
112
from nucleus.annotation import (
213
BoxAnnotation,
314
PolygonAnnotation,
415
SegmentationAnnotation,
516
)
17+
from nucleus.constants import (
18+
DATASET_ID_KEY,
19+
ERROR_ITEMS,
20+
ERROR_PAYLOAD,
21+
IGNORED_ITEMS,
22+
NEW_ITEMS,
23+
UPDATED_ITEMS,
24+
)
625
from nucleus.job import AsyncJob, JobError
7-
import pytest
8-
import os
926

1027
from .helpers import (
28+
LOCAL_FILENAME,
1129
TEST_BOX_ANNOTATIONS,
30+
TEST_DATASET_NAME,
31+
TEST_IMG_URLS,
1232
TEST_POLYGON_ANNOTATIONS,
1333
TEST_SEGMENTATION_ANNOTATIONS,
1434
TEST_SLICE_NAME,
15-
TEST_DATASET_NAME,
16-
TEST_IMG_URLS,
17-
LOCAL_FILENAME,
1835
reference_id_from_url,
1936
)
2037

21-
from nucleus import (
22-
Dataset,
23-
DatasetItem,
24-
UploadResponse,
25-
NucleusClient,
26-
NucleusAPIError,
27-
)
28-
from nucleus.constants import (
29-
NEW_ITEMS,
30-
UPDATED_ITEMS,
31-
IGNORED_ITEMS,
32-
ERROR_ITEMS,
33-
ERROR_PAYLOAD,
34-
DATASET_ID_KEY,
35-
)
36-
3738
TEST_AUTOTAG_DATASET = "ds_bz43jm2jwm70060b3890"
3839

3940

@@ -132,8 +133,20 @@ def check_is_expected_response(response):
132133

133134

134135
def test_dataset_append_local(CLIENT, dataset):
135-
ds_items_local = [DatasetItem(image_location=LOCAL_FILENAME)]
136+
ds_items_local_error = [
137+
DatasetItem(image_location=LOCAL_FILENAME, metadata={"test": math.nan})
138+
]
139+
with pytest.raises(ValueError) as e:
140+
dataset.append(ds_items_local_error)
141+
assert "Out of range float values are not JSON compliant" in str(
142+
e.value
143+
)
144+
ds_items_local = [
145+
DatasetItem(image_location=LOCAL_FILENAME, metadata={"test": 0})
146+
]
147+
136148
response = dataset.append(ds_items_local)
149+
137150
assert isinstance(response, UploadResponse)
138151
resp_json = response.json()
139152
assert resp_json[DATASET_ID_KEY] == dataset.id

0 commit comments

Comments
 (0)