diff --git a/tests/test_type_view.py b/tests/test_type_view.py index 862ab0e..406a09b 100644 --- a/tests/test_type_view.py +++ b/tests/test_type_view.py @@ -310,10 +310,10 @@ def test_strip_optional() -> None: def test_repr() -> None: assert repr(TypeView(int)) == "TypeView(int)" if sys.version_info < (3, 9): - assert repr(TypeView(Optional[str])) == "TypeView(typing.Union[str, NoneType])" + assert repr(TypeView(Optional[str])) == "TypeView(Union[str, NoneType])" else: - assert repr(TypeView(Optional[str])) == "TypeView(typing.Optional[str])" - assert repr(TypeView(Literal["1", 2, True])) == "TypeView(typing.Literal['1', 2, True])" + assert repr(TypeView(Optional[str])) == "TypeView(Optional[str])" + assert repr(TypeView(Literal["1", 2, True])) == "TypeView(Literal['1', 2, True])" def test_is_none_type() -> None: @@ -354,3 +354,15 @@ def test_safe_generic_origin(annotation: Any, expected: Any) -> None: if isinstance(annotation, str): annotation = eval(annotation) assert TypeView(annotation).safe_generic_origin is expected + + +def test_repr_type() -> None: + assert TypeView(int).repr_type == "int" + assert TypeView(str).repr_type == "str" + assert TypeView("asdf").repr_type == "asdf" + assert TypeView(Optional[int]).repr_type in ("Optional[int]", "Union[int, NoneType]", "Union[int, None]") + assert TypeView(Literal[1, "two"]).repr_type == "Literal[1, 'two']" + assert TypeView(Union[Literal[1, "two"], bool]).repr_type == "Union[Literal[1, 'two'], bool]" + + if sys.version_info >= (3, 9): + assert TypeView(set[bool]).repr_type == "set[bool]" diff --git a/type_lens/type_view.py b/type_lens/type_view.py index 690222a..234f3c7 100644 --- a/type_lens/type_view.py +++ b/type_lens/type_view.py @@ -63,11 +63,15 @@ def __eq__(self, other: object) -> bool: def __repr__(self) -> str: cls_name = self.__class__.__name__ + return f"{cls_name}({self.repr_type})" - raw = self.raw - if isinstance(self.raw, type): - raw = raw.__name__ # type: ignore[attr-defined] - return f"{cls_name}({raw})" + @property + def repr_type(self) -> str: + ann = self.annotation + if isinstance(ann, type) and not self.origin: + ann = ann.__name__ # type: ignore[attr-defined] + + return str(ann).replace("typing.", "") @property def allows_none(self) -> bool: