Skip to content
Open
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: 2 additions & 0 deletions dltype/_lib/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import inspect
import itertools
import warnings
from copy import copy
from functools import lru_cache, wraps
from types import EllipsisType
from typing import (
Expand Down Expand Up @@ -124,6 +125,7 @@ def from_hint( # noqa: PLR0911
msg = f"Invalid base type=<{tensor_type}> in DLType hint, expected a subtype of {_dtypes.SUPPORTED_TENSOR_TYPES}"
raise TypeError(msg)

dltype_hint = copy(dltype_hint) if optional else dltype_hint
dltype_hint.optional = optional
return (cls(tensor_type_hint=tensor_type, dltype_annotation=dltype_hint),)

Expand Down
21 changes: 21 additions & 0 deletions dltype/tests/dltype_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1711,3 +1711,24 @@ def tuple_function(
) -> Self:
"""A function that takes a tensor and returns a tensor."""
return self


SomeTensorT: TypeAlias = Annotated[torch.Tensor, dltype.FloatTensor["1 2 3"]]


def test_aliased_optional() -> None:

@dltype.dltyped()
def func(non_optional_tensor: SomeTensorT, optional_tensor: SomeTensorT | None = None) -> None:
pass

func(torch.zeros((1, 2, 3)))

func(torch.zeros((1, 2, 3)), None)

func(torch.zeros((1, 2, 3)), torch.zeros((1, 2, 3)))

func(torch.zeros((1, 2, 3)))

with pytest.raises(dltype.DLTypeUnsupportedTensorTypeError):
func(None, torch.zeros((1, 2, 3))) # pyright: ignore[reportArgumentType]
Loading