More detailed type annotations in graph visualizations

Open Beginner friendly
#1,383 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
70/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
python

Research direction

Start in htypes.py at get_type_as_string, which currently reduces parameterized types such as dict[str, MySpecialClass] to their base name. Check how the function handles typing.get_args() and verify that the resulting graph type annotation preserves nested type attributes without using fully qualified names. Done means parameterized annotations display their component types while existing simple types remain unchanged.

Written by the indexing model from the issue text.

Description

Is your feature request related to a problem? Please describe.
With the current graph visualization, a node with a return type of dict[str,MySpecialClass] will just have "dict" listed as type.
The conversion of the type to string seems to happen in get_type_as_string, which is currently implemented as:

def get_type_as_string(type_: Type) -> Optional[str]:
    """Get a string representation of a type.

    The logic supports the evolution of the type system between 3.8 and 3.10.
    :param type_: Any Type object. Typically the node type found at Node.type.
    :return: string representation of the type. An empty string if everything fails.
    """

    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_)
        type_string = get_type_as_string(base_type)
    elif getattr(type_, "__repr__", None):
        type_string = type_.__repr__()
    else:
        type_string = None

    return type_string

In the above mentioned example this seems to fall into the second case (using__name__), which will discard the type attributes

Describe the solution you'd like
Adding

    if typing.get_args(type_):
        type_string += f"[{', '.join(get_type_as_string(arg) for arg in typing.get_args(type_))}]"

before the return seems to fix this issue for my cases.

Describe alternatives you've considered
The str() option will return fully qualified names, which might not be desired since it would make text too long. A custom version could be implemented (for example by regex replacing the module names)

Dominant language
Jupyter Notebook
Stars
2.6k
Forks
214
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from apache/hamilton

All issues in apache/hamilton

Similar issues

More Data Visualization issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.