-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Do not auto-determine generic args if already supplied #4148
Do not auto-determine generic args if already supplied #4148
Conversation
reflex/vars/base.py
Outdated
@@ -1257,6 +1257,27 @@ def unionize(*args: Type) -> Type: | |||
return Union[unionize(*first_half), unionize(*second_half)] | |||
|
|||
|
|||
def has_args(cls) -> bool: |
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.
Can we reuse this function?
Line 173 in 87648af
def is_generic_alias(cls: GenericType) -> bool: |
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.
I don't think so, is_generic_alias does not return True for this dict:
class CustomDict(dict[str, str]):
"""A custom dict with generic arguments."""
pass
tests/units/utils/test_types.py ........................FF.. [100%]
================================================================================== FAILURES ==================================================================================
_______________________________________________________________________ test_has_args[CustomDict-True] _______________________________________________________________________
cls = <class 'tests.units.utils.test_types.CustomDict'>, expected = True
@pytest.mark.parametrize(
"cls,expected",
[
(int, False),
(str, False),
(float, False),
(Tuple[int], True),
(List[int], True),
(Union[int, str], True),
(Union[str, int], True),
(Dict[str, int], True),
(CustomDict, True),
(ChildCustomDict, True),
(GenericDict, False),
(ChildGenericDict, False),
],
)
def test_has_args(cls, expected: bool) -> None:
> assert types.is_generic_alias(cls) == expected
E AssertionError: assert False == True
E + where False = <functools._lru_cache_wrapper object at 0x7477f8add7a0>(<class 'tests.units.utils.test_types.CustomDict'>)
E + where <functools._lru_cache_wrapper object at 0x7477f8add7a0> = types.is_generic_alias
tests/units/utils/test_types.py:92: AssertionError
____________________________________________________________________ test_has_args[ChildCustomDict-True] _____________________________________________________________________
cls = <class 'tests.units.utils.test_types.ChildCustomDict'>, expected = True
@pytest.mark.parametrize(
"cls,expected",
[
(int, False),
(str, False),
(float, False),
(Tuple[int], True),
(List[int], True),
(Union[int, str], True),
(Union[str, int], True),
(Dict[str, int], True),
(CustomDict, True),
(ChildCustomDict, True),
(GenericDict, False),
(ChildGenericDict, False),
],
)
def test_has_args(cls, expected: bool) -> None:
> assert types.is_generic_alias(cls) == expected
E AssertionError: assert False == True
E + where False = <functools._lru_cache_wrapper object at 0x7477f8add7a0>(<class 'tests.units.utils.test_types.ChildCustomDict'>)
E + where <functools._lru_cache_wrapper object at 0x7477f8add7a0> = types.is_generic_alias
tests/units/utils/test_types.py:92: AssertionError
========================================================================== short test summary info ===========================================================================
FAILED tests/units/utils/test_types.py::test_has_args[CustomDict-True] - AssertionError: assert False == True
FAILED tests/units/utils/test_types.py::test_has_args[ChildCustomDict-True] - AssertionError: assert False == True
======================================================================== 2 failed, 26 passed in 0.10s ========================================================================
but I could try to use is_generic_alias in has_args like this:
diff --git a/reflex/utils/types.py b/reflex/utils/types.py
index 3c5182e0..a5fc30e4 100644
--- a/reflex/utils/types.py
+++ b/reflex/utils/types.py
@@ -229,13 +229,13 @@ def has_args(cls) -> bool:
Returns:
Whether the class has generic
"""
- if get_args(cls):
+ if is_generic_alias(cls):
return True
# Check if the class inherits from a generic class (using __orig_bases__)
if hasattr(cls, "__orig_bases__"):
for base in cls.__orig_bases__:
- if get_args(base):
+ if is_generic_alias(base):
return True
return False
* add failing test for figure_out_type * do not auto-determine generic args if already supplied * move has_args to utils.types, add tests for it
Custom types which inherit from dict result in wrong
_var_type