-
Notifications
You must be signed in to change notification settings - Fork 891
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refine type check for is_custom_strong_type (#676)
# Description Refine the type check for is_custom_strong_type static method as a follow-up to this pull request: #649 For generic type annotations like list[str], tuple[str, int], dict[str, typing.Any] would be types.GenericAlias so return False. For other types that would be passed to issubclass to check if it is the subclass of CustomStrongTypeConnection. The try-except block is still kept since it cannot be confidently removed due to the uncertainty of TypeError that may occur. TypeError is not expected to happen, but if it does, we will log it for debugging and return False. Apart from ut, e2e flow test using below script can pass: ``` def test(self): from promptflow._sdk._pf_client import PFClient client = PFClient() client.flows.test(flow=r"D:\proj\github\ms\promptflow\examples\flows\standard\gen-docstring") ``` ![image](https://github.com/microsoft/promptflow/assets/46446115/dd47eb2a-f935-4793-b1a8-6159ccf07dae) --------- Co-authored-by: yalu4 <[email protected]>
- Loading branch information
Showing
2 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
54 changes: 54 additions & 0 deletions
54
src/promptflow/tests/executor/unittests/contracts/test_tool.py
This file contains 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,54 @@ | ||
from typing import Any, Callable, NewType, Optional, Tuple, TypeVar, Union | ||
|
||
import pytest | ||
|
||
from promptflow._sdk.entities import CustomStrongTypeConnection | ||
from promptflow.contracts.tool import ConnectionType | ||
|
||
|
||
class MyConnection(CustomStrongTypeConnection): | ||
pass | ||
|
||
|
||
my_connection = MyConnection(name="my_connection", secrets={"key": "value"}) | ||
|
||
|
||
def some_function(): | ||
pass | ||
|
||
|
||
@pytest.mark.unittest | ||
class TestToolContract: | ||
@pytest.mark.parametrize( | ||
"val, expected_res", | ||
[ | ||
(my_connection, True), | ||
(MyConnection, True), | ||
(list, False), | ||
(list[str], False), | ||
(list[int], False), | ||
([1, 2, 3], False), | ||
(float, False), | ||
(int, False), | ||
(5, False), | ||
(str, False), | ||
(some_function, False), | ||
(Union[str, int], False), | ||
# ((int | str), False), # Python 3.10 | ||
(tuple, False), | ||
(tuple[str, int], False), | ||
(Tuple[int, ...], False), | ||
(dict[str, Any], False), | ||
({"test1": [1, 2, 3], "test2": [4, 5, 6], "test3": [7, 8, 9]}, False), | ||
(Any, False), | ||
(None, False), | ||
(Optional[str], False), | ||
(TypeVar("T"), False), | ||
(TypeVar, False), | ||
(Callable, False), | ||
(Callable[..., Any], False), | ||
(NewType("MyType", int), False), | ||
], | ||
) | ||
def test_is_custom_strong_type(self, val, expected_res): | ||
assert ConnectionType.is_custom_strong_type(val) == expected_res |