-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
feat: Improve type validation for bare types #8997
feat: Improve type validation for bare types #8997
Conversation
Pull Request Test Coverage Report for Build 13782703256Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
@mdrazak2001 thanks for your work on this! Could we also expand the test suite a bit to include Nested checks: @pytest.mark.parametrize(
"sender_type,receiver_type",
[
# Nested Lists
pytest.param(List[List[int]], List[List], id="nested-list-to-partially-bare-list"),
pytest.param(List[List[int]], List, id="nested-list-to-bare-list"),
# Nested Dicts
pytest.param(Dict[str, Dict[str, int]], Dict[str, Dict], id="nested-dict-to-partially-bare-dict"),
pytest.param(Dict[str, Dict[str, int]], Dict, id="nested-dict-to-bare-dict"),
# Nested Sets
pytest.param(Set[Set[int]], Set[Set], id="nested-set-to-partially-bare-set"),
pytest.param(Set[Set[int]], Set, id="nested-set-to-bare-set"),
# Nested Tuples
pytest.param(Tuple[Tuple[int, str], bool], Tuple[Tuple, bool], id="nested-tuple-to-partially-bare-tuple"),
pytest.param(Tuple[Tuple[int, str], bool], Tuple, id="nested-tuple-to-bare-tuple"),
# Mixed nesting
pytest.param(List[Dict[str, int]], List[Dict], id="list-of-dict-to-list-of-bare-dict"),
pytest.param(Dict[str, List[int]], Dict[str, List], id="dict-with-list-to-dict-with-bare-list"),
pytest.param(Set[Tuple[int, str]], Set[Tuple], id="set-of-tuple-to-set-of-bare-tuple"),
pytest.param(Tuple[List[int], Dict[str, bool]], Tuple[List, Dict], id="tuple-of-containers-to-tuple-of-bare-containers"),
# Triple nesting
pytest.param(List[List[List[int]]], List[List[List]], id="triple-nested-list-to-partially-bare"),
pytest.param(List[List[List[int]]], List[List], id="triple-nested-list-to-double-bare"),
pytest.param(List[List[List[int]]], List, id="triple-nested-list-to-completely-bare"),
],
)
def test_nested_container_compatibility(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) |
@sjrl: I merged the following if conditional block into the final return statement because the function had too many returns, which was causing it to fail to commit.
|
haystack/core/type_utils.py
Outdated
|
||
return all(_strict_types_are_compatible(*args) for args in zip(sender_args, receiver_args)) | ||
|
||
def _check_callable_compatibility(sender_args, receiver_args): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your work on this, but for now let's not worry about the callable compatibility and do this in a separate PR so we can focus on just the other bare types. Could you open a new issue/PR with these changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Certainly, @sjrl , I will include callable compatibility in a separate PR. However, I noticed a minor limitation in the current implementation for bare types: even Callable bare types require special handling via _check_callable_compatibility
due to their unique structure. For example, the test case pytest.param(Callable[[Any], Any], Callable)
fails with the current implementation (without _check_callable_compatibility
).
Would it be acceptable to exclude Callable bare types from this PR and instead address all cases involving them in the separate PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Would it be acceptable to exclude Callable bare types from this PR and instead address all cases involving them in the separate PR?
Yes that sounds good to me.
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((List[int]), list, id="list-of-primitive-to-list-object"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one needs to be put into a separate test for now we expect it to not work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the work on this!
Related Issues
Proposed Changes:
How did you test it?
Notes for the reviewer
Checklist
fix:
,feat:
,build:
,chore:
,ci:
,docs:
,style:
,refactor:
,perf:
,test:
and added!
in case the PR includes breaking changes.