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
25 changes: 12 additions & 13 deletions grassmann_tensor/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,18 @@ def to(self, whatever: torch.device | torch.dtype | str | None = None, *, device
"""
Copy the tensor to a specified device or copy it to a specified data type.
"""
if whatever is None:
pass
elif isinstance(whatever, torch.device):
assert device is None, "Duplicate device specification."
device = whatever
elif isinstance(whatever, torch.dtype):
assert dtype is None, "Duplicate dtype specification."
dtype = whatever
elif isinstance(whatever, str):
assert device is None, "Duplicate device specification."
device = torch.device(whatever)
else:
raise TypeError(f"Unsupported type for 'to': {type(whatever)}. Expected torch.device, torch.dtype, or str.")
match whatever:
case torch.device():
assert device is None, "Duplicate device specification."
device = whatever
case torch.dtype():
assert dtype is None, "Duplicate dtype specification."
dtype = whatever
case str():
assert device is None, "Duplicate device specification."
device = torch.device(whatever)
case None:
pass
match (device, dtype):
case (None, None):
return self
Expand Down
5 changes: 0 additions & 5 deletions tests/conversion_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ def test_conversion(
y = x.to(*args, **kwargs)


def test_conversion_invalid_type(x: GrassmannTensor) -> None:
with pytest.raises(TypeError):
x.to(2333) # type: ignore[arg-type]


def test_conversion_duplicated_value(x: GrassmannTensor) -> None:
with pytest.raises(AssertionError):
x.to(torch.device("cpu"), device=torch.device("cpu"))
Expand Down