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

Do not auto-determine generic args if already supplied #4148

Merged
merged 3 commits into from
Oct 12, 2024

Conversation

benedikt-bartscher
Copy link
Contributor

@benedikt-bartscher benedikt-bartscher commented Oct 10, 2024

Custom types which inherit from dict result in wrong _var_type

@benedikt-bartscher benedikt-bartscher changed the title add failing test for figure_out_type Do not auto-determine generic args if already supplied Oct 10, 2024
@benedikt-bartscher benedikt-bartscher marked this pull request as ready for review October 10, 2024 18:14
@@ -1257,6 +1257,27 @@ def unionize(*args: Type) -> Type:
return Union[unionize(*first_half), unionize(*second_half)]


def has_args(cls) -> bool:
Copy link
Contributor

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?

def is_generic_alias(cls: GenericType) -> bool:

Copy link
Contributor Author

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

@masenf masenf merged commit 0889276 into reflex-dev:main Oct 12, 2024
31 checks passed
Kastier1 pushed a commit that referenced this pull request Oct 23, 2024
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants