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

Resolve Annotated types (PEP 593) on dataflow visualizations #1276

Merged
merged 4 commits into from
Feb 11, 2025
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
4 changes: 3 additions & 1 deletion hamilton/htypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ def get_type_as_string(type_: Type) -> Optional[str]:
:return: string representation of the type. An empty string if everything fails.
"""

if getattr(type_, "__name__", None):
if _is_annotated_type(type_):
type_string = get_type_as_string(typing.get_args(type_)[0])
elif getattr(type_, "__name__", None):
type_string = type_.__name__
elif typing_inspect.get_origin(type_):
base_type = typing_inspect.get_origin(type_)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_type_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
from hamilton import htypes
from hamilton.htypes import check_instance

if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated


class X:
pass
Expand Down Expand Up @@ -178,6 +183,7 @@ def test__safe_subclass(candidate, type_, expected):
(pd.Series),
(htypes.column[pd.Series, int]),
(pd.DataFrame),
(Annotated[int, "metadata"]),
],
)
def test_get_type_as_string(type_):
Expand All @@ -188,6 +194,12 @@ def test_get_type_as_string(type_):
pytest.fail(f"test get_type_as_string raised: {e}")


def test_type_as_string_with_annotated_type():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you feel like doing more testing work we should really test the string output of these, not just that they fail. But that's out of scope.

"""Tests the custom_subclass_check"""
type_string = htypes.get_type_as_string(Annotated[int, "metadata"]) # type: ignore
assert type_string == "int"


@pytest.mark.parametrize(
"node_type,input_value",
[
Expand Down