-
Notifications
You must be signed in to change notification settings - Fork 11.5k
Reland union type #5900
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
Merged
comfyanonymous
merged 5 commits into
Comfy-Org:master
from
webfiltered:reland-union-type
Dec 4, 2024
Merged
Reland union type #5900
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c3588f0
Reapply "Add union link connection type support (#5806)" (#5889)
webfiltered 3dca9a0
Fix union type breaks existing type workarounds
webfiltered 41c2dfb
Add non-string test
webfiltered ad43d4c
Add tests for hacks and non-string types
webfiltered 9af17fd
Support python versions lower than 3.11
webfiltered File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| from __future__ import annotations | ||
|
|
||
|
|
||
| def validate_node_input( | ||
| received_type: str, input_type: str, strict: bool = False | ||
| ) -> bool: | ||
| """ | ||
| received_type and input_type are both strings of the form "T1,T2,...". | ||
|
|
||
| If strict is True, the input_type must contain the received_type. | ||
| For example, if received_type is "STRING" and input_type is "STRING,INT", | ||
| this will return True. But if received_type is "STRING,INT" and input_type is | ||
| "INT", this will return False. | ||
|
|
||
| If strict is False, the input_type must have overlap with the received_type. | ||
| For example, if received_type is "STRING,BOOLEAN" and input_type is "STRING,INT", | ||
| this will return True. | ||
|
|
||
| Supports pre-union type extension behaviour of ``__ne__`` overrides. | ||
| """ | ||
| # If the types are exactly the same, we can return immediately | ||
| # Use pre-union behaviour: inverse of `__ne__` | ||
| if not received_type != input_type: | ||
| return True | ||
|
|
||
| # Not equal, and not strings | ||
| if not isinstance(received_type, str) or not isinstance(input_type, str): | ||
| return False | ||
|
|
||
| # Split the type strings into sets for comparison | ||
| received_types = set(t.strip() for t in received_type.split(",")) | ||
| input_types = set(t.strip() for t in input_type.split(",")) | ||
|
|
||
| if strict: | ||
| # In strict mode, all received types must be in the input types | ||
| return received_types.issubset(input_types) | ||
| else: | ||
| # In non-strict mode, there must be at least one type in common | ||
| return len(received_types.intersection(input_types)) > 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import pytest | ||
| from comfy_execution.validation import validate_node_input | ||
| from enum import StrEnum | ||
|
|
||
|
|
||
| def test_exact_match(): | ||
| """Test cases where types match exactly""" | ||
| assert validate_node_input("STRING", "STRING") | ||
| assert validate_node_input("STRING,INT", "STRING,INT") | ||
| assert validate_node_input("INT,STRING", "STRING,INT") # Order shouldn't matter | ||
|
|
||
|
|
||
| def test_strict_mode(): | ||
| """Test strict mode validation""" | ||
| # Should pass - received type is subset of input type | ||
| assert validate_node_input("STRING", "STRING,INT", strict=True) | ||
| assert validate_node_input("INT", "STRING,INT", strict=True) | ||
| assert validate_node_input("STRING,INT", "STRING,INT,BOOLEAN", strict=True) | ||
|
|
||
| # Should fail - received type is not subset of input type | ||
| assert not validate_node_input("STRING,INT", "STRING", strict=True) | ||
| assert not validate_node_input("STRING,BOOLEAN", "STRING", strict=True) | ||
| assert not validate_node_input("INT,BOOLEAN", "STRING,INT", strict=True) | ||
|
|
||
|
|
||
| def test_non_strict_mode(): | ||
| """Test non-strict mode validation (default behavior)""" | ||
| # Should pass - types have overlap | ||
| assert validate_node_input("STRING,BOOLEAN", "STRING,INT") | ||
| assert validate_node_input("STRING,INT", "INT,BOOLEAN") | ||
| assert validate_node_input("STRING", "STRING,INT") | ||
|
|
||
| # Should fail - no overlap in types | ||
| assert not validate_node_input("BOOLEAN", "STRING,INT") | ||
| assert not validate_node_input("FLOAT", "STRING,INT") | ||
| assert not validate_node_input("FLOAT,BOOLEAN", "STRING,INT") | ||
|
|
||
|
|
||
| def test_whitespace_handling(): | ||
| """Test that whitespace is handled correctly""" | ||
| assert validate_node_input("STRING, INT", "STRING,INT") | ||
| assert validate_node_input("STRING,INT", "STRING, INT") | ||
| assert validate_node_input(" STRING , INT ", "STRING,INT") | ||
| assert validate_node_input("STRING,INT", " STRING , INT ") | ||
|
|
||
|
|
||
| def test_empty_strings(): | ||
| """Test behavior with empty strings""" | ||
| assert validate_node_input("", "") | ||
| assert not validate_node_input("STRING", "") | ||
| assert not validate_node_input("", "STRING") | ||
|
|
||
|
|
||
| def test_single_vs_multiple(): | ||
| """Test single type against multiple types""" | ||
| assert validate_node_input("STRING", "STRING,INT,BOOLEAN") | ||
| assert validate_node_input("STRING,INT,BOOLEAN", "STRING", strict=False) | ||
| assert not validate_node_input("STRING,INT,BOOLEAN", "STRING", strict=True) | ||
|
|
||
|
|
||
| def test_non_string(): | ||
| """Test non-string types""" | ||
| obj1 = object() | ||
| obj2 = object() | ||
| assert validate_node_input(obj1, obj1) | ||
| assert not validate_node_input(obj1, obj2) | ||
|
|
||
|
|
||
| class NotEqualsOverrideTest(StrEnum): | ||
| """Test class for ``__ne__`` override.""" | ||
|
|
||
| ANY = "*" | ||
| LONGER_THAN_2 = "LONGER_THAN_2" | ||
|
|
||
| def __ne__(self, value: object) -> bool: | ||
| if self == "*" or value == "*": | ||
| return False | ||
| if self == "LONGER_THAN_2": | ||
| return not len(value) > 2 | ||
| raise TypeError("This is a class for unit tests only.") | ||
|
|
||
|
|
||
| def test_ne_override(): | ||
| """Test ``__ne__`` any override""" | ||
| any = NotEqualsOverrideTest.ANY | ||
| invalid_type = "INVALID_TYPE" | ||
| obj = object() | ||
| assert validate_node_input(any, any) | ||
| assert validate_node_input(any, invalid_type) | ||
| assert validate_node_input(any, obj) | ||
| assert validate_node_input(any, {}) | ||
| assert validate_node_input(any, []) | ||
| assert validate_node_input(any, [1, 2, 3]) | ||
|
|
||
|
|
||
| def test_ne_custom_override(): | ||
| """Test ``__ne__`` custom override""" | ||
| special = NotEqualsOverrideTest.LONGER_THAN_2 | ||
|
|
||
| assert validate_node_input(special, special) | ||
| assert validate_node_input(special, "*") | ||
| assert validate_node_input(special, "INVALID_TYPE") | ||
| assert validate_node_input(special, [1, 2, 3]) | ||
|
|
||
| # Should fail | ||
| assert not validate_node_input(special, [1, 2]) | ||
| assert not validate_node_input(special, "TY") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "received,input_type,strict,expected", | ||
| [ | ||
| ("STRING", "STRING", False, True), | ||
| ("STRING,INT", "STRING,INT", False, True), | ||
| ("STRING", "STRING,INT", True, True), | ||
| ("STRING,INT", "STRING", True, False), | ||
| ("BOOLEAN", "STRING,INT", False, False), | ||
| ("STRING,BOOLEAN", "STRING,INT", False, True), | ||
| ], | ||
| ) | ||
| def test_parametrized_cases(received, input_type, strict, expected): | ||
| """Parametrized test cases for various scenarios""" | ||
| assert validate_node_input(received, input_type, strict) == expected | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.