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

Improve type validation to handle bare typing objects #8874

Closed
sjrl opened this issue Feb 19, 2025 · 1 comment · Fixed by #8997
Closed

Improve type validation to handle bare typing objects #8874

sjrl opened this issue Feb 19, 2025 · 1 comment · Fixed by #8997
Assignees
Labels
P3 Low priority, leave it in the backlog

Comments

@sjrl
Copy link
Contributor

sjrl commented Feb 19, 2025

Is your feature request related to a problem? Please describe.
Our current type validation used when validating pipeline connections doesn't support comparison of bare types. For example, List[Any] does not get matched to List even though it should.

Describe the solution you'd like
Update the function such that we can handle bare types. For example, this code

    # If either is a bare type (no args), treat it as if it had Any
    if not sender_args:
        sender_args = (Any,)
    if not receiver_args:
        receiver_args = (Any,) * len(sender_args)

could be used such that any bare types are given Any as arguments.

Alternatively we can look for existing type comparison solutions that probably exist in other libraries that handles this already.

Describe alternatives you've considered
Leave as is and probably document that we don't properly support bare types.

@sjrl
Copy link
Contributor Author

sjrl commented Feb 19, 2025

Here are some tests (not exhaustive) we can use while developing this

@pytest.mark.parametrize(
    "sender_type,receiver_type",
    [
        pytest.param(List[int], List, id="list-of-primitive-to-bare-list"),
        pytest.param(Dict[str, int], Dict, id="dict-of-primitive-to-bare-dict"),
        pytest.param(Set[float], Set, id="set-of-primitive-to-bare-set"),
        pytest.param(Tuple[int, str], Tuple, id="tuple-of-primitive-to-bare-tuple"),
        pytest.param(Callable[[int, str], bool], Callable, id="callable-to-bare-callable"),
    ],
)
def test_container_of_primitive_to_bare_container_strict(sender_type, receiver_type):
    assert _types_are_compatible(sender_type, receiver_type)
    # Bare container types should not be compatible with their typed counterparts
    assert not _types_are_compatible(receiver_type, sender_type)


@pytest.mark.parametrize(
    "sender_type,receiver_type",
    [
        pytest.param(List[Any], List, id="list-of-any-to-bare-list"),
        pytest.param(Dict[Any, Any], Dict, id="dict-of-any-to-bare-dict"),
        pytest.param(Set[Any], Set, id="set-of-any-to-bare-set"),
        pytest.param(Tuple[Any], Tuple, id="tuple-of-any-to-bare-tuple"),
    ],
)
def test_container_of_any_to_bare_container_strict(sender_type, receiver_type):
    # Both are compatible
    assert _types_are_compatible(sender_type, receiver_type)
    assert _types_are_compatible(receiver_type, sender_type)


@pytest.mark.parametrize(
    "sender_type,receiver_type",
    [
        pytest.param(Literal["test"], Literal, id="literal-to-bare-literal"),
        pytest.param(Union[str, int], Union, id="union-to-bare-union"),
        pytest.param(Optional[str], Optional, id="union-to-bare-union"),
    ],
)
def test_always_incompatible_bare_types(sender_type, receiver_type):
    # Neither are compatible
    assert not _types_are_compatible(sender_type, receiver_type)
    assert not _types_are_compatible(receiver_type, sender_type)

@julian-risch julian-risch added the P3 Low priority, leave it in the backlog label Feb 21, 2025
@sjrl sjrl self-assigned this Mar 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 Low priority, leave it in the backlog
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants