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: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ classifiers = [
"Intended Audience :: Developers",
]
name = "type-lens"
version = "0.2.5"
version = "0.2.6"
description = "type-lens is a Python template project designed to simplify the setup of a new project."
readme = "README.md"
license = { text = "MIT" }
Expand Down
9 changes: 9 additions & 0 deletions tests/test_type_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ def test_strip_optional() -> None:
assert TypeView(Optional[Union[str, int]]).strip_optional() == TypeView(Union[str, int])
assert TypeView(Union[str, int, None]).strip_optional() == TypeView(Union[str, int])

# Retain metadata
assert TypeView(Annotated[Union[int, None], 4]).strip_optional().metadata == TypeView(int, metadata=[4]).metadata == (4,)


def test_repr() -> None:
assert repr(TypeView(int)) == "TypeView(int)"
Expand Down Expand Up @@ -453,10 +456,16 @@ def test_strip_type_alias() -> None:
assert TypeView(Foo).strip_type_alias() == TypeView(int) # pyright: ignore
assert TypeView(int).strip_type_alias() == TypeView(int)

# Retain metadata
assert TypeView(Annotated[Foo, 4]).strip_type_alias().metadata == TypeView(int, metadata=[4]).metadata == (4,)


def test_strip_type_alias_extensions() -> None:
from typing_extensions import TypeAliasType

Foo = TypeAliasType("Foo", int) # pyright: ignore
assert TypeView(Foo).strip_type_alias() == TypeView(int)
assert TypeView(int).strip_type_alias() == TypeView(int)

# Retain metadata
assert TypeView(Annotated[Foo, 4]).strip_type_alias().metadata == TypeView(int, metadata=[4]).metadata == (4,)
13 changes: 7 additions & 6 deletions type_lens/type_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Literal,
TypeVar,
Union,
Sequence,
_SpecialForm, # pyright: ignore[reportPrivateUsage]
)

Expand Down Expand Up @@ -43,7 +44,7 @@ class TypeView(Generic[T]):
"_wrappers": "A set of wrapper types that were removed from the annotation.",
}

def __init__(self, annotation: T) -> None:
def __init__(self, annotation: T, *, metadata: Sequence[Any] = ()) -> None:
"""Initialize ParsedType.

Args:
Expand All @@ -54,7 +55,7 @@ def __init__(self, annotation: T) -> None:
Returns:
ParsedType
"""
unwrapped, metadata, wrappers = unwrap_annotation(annotation)
unwrapped, annotation_metadata, wrappers = unwrap_annotation(annotation)
origin = get_origin(unwrapped)

args: tuple[Any, ...] = () if origin is abc.Callable else get_args(unwrapped) # pyright: ignore
Expand All @@ -64,9 +65,9 @@ def __init__(self, annotation: T) -> None:
self.origin: Final[Any] = origin
self.fallback_origin: Final[Any] = origin or unwrapped
self.args: Final[tuple[Any, ...]] = args
self.metadata: Final = metadata
self.metadata: Final = (*annotation_metadata, *metadata)
self._wrappers: Final = wrappers
self.inner_types: Final = tuple(TypeView(arg) for arg in args)
self.inner_types: Final = tuple(TypeView(arg, metadata=self.metadata) for arg in args)

def __eq__(self, other: object) -> bool:
if not isinstance(other, TypeView):
Expand Down Expand Up @@ -285,7 +286,7 @@ def strip_optional(self) -> TypeView[Any]:

args = tuple(a for a in self.args if a is not NoneType)
non_optional = Union[args] # type: ignore[valid-type]
return TypeView(non_optional)
return TypeView(non_optional, metadata=self.metadata)

def strip_type_alias(self) -> TypeView[Any]:
"""Remove the type alias from a `type Type = T` type alias.
Expand All @@ -297,7 +298,7 @@ def strip_type_alias(self) -> TypeView[Any]:
"""
if not self.is_type_alias:
return self
return TypeView(self.annotation.__value__)
return TypeView(self.annotation.__value__, metadata=self.metadata)


def _is_typing_extensins_type_alias(type_view: TypeView[Any]) -> bool:
Expand Down
Loading