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

Ban dataset attributes as tensor names #1103

Merged
merged 4 commits into from
Aug 6, 2021
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
10 changes: 10 additions & 0 deletions hub/api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
TensorInvalidSampleShapeError,
DatasetHandlerError,
UnsupportedCompressionError,
InvalidTensorNameError,
)
from hub.constants import MB

Expand Down Expand Up @@ -647,3 +648,12 @@ def test_dataset_delete():
assert not os.path.isfile("test/dataset_meta.json")

hub.constants.DELETE_SAFETY_SIZE = old_size


def test_invalid_tesnor_name(memory_ds):
with pytest.raises(InvalidTensorNameError):
memory_ds.create_tensor("meta")
with pytest.raises(InvalidTensorNameError):
memory_ds.create_tensor("tensors")
with pytest.raises(InvalidTensorNameError):
memory_ds.create_tensor("info")
4 changes: 4 additions & 0 deletions hub/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
ReadOnlyModeError,
TensorAlreadyExistsError,
TensorDoesNotExistError,
InvalidTensorNameError,
)
from hub.client.client import HubBackendClient
from hub.client.log import logger
Expand Down Expand Up @@ -171,11 +172,14 @@ def create_tensor(

Raises:
TensorAlreadyExistsError: Duplicate tensors are not allowed.
InvalidTensorNameError: If `name` is in dataset attributes.
NotImplementedError: If trying to override `chunk_compression`.
"""

if tensor_exists(name, self.storage):
raise TensorAlreadyExistsError(name)
if name in vars(self):
raise InvalidTensorNameError(name)
kristinagrig06 marked this conversation as resolved.
Show resolved Hide resolved

# Seperate meta and info

Expand Down
7 changes: 7 additions & 0 deletions hub/util/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ def __init__(self, key: str):
super().__init__(f"Tensor '{key}' already exists.")


class InvalidTensorNameError(Exception):
def __init__(self, name: str):
super().__init__(
f"The use of a reserved attribute '{name}' as a tensor name is invalid."
)


class DynamicTensorNumpyError(Exception):
def __init__(self, key: str, index, property_key: str):
super().__init__(
Expand Down