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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions reflex/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,27 @@ def is_literal(cls: GenericType) -> bool:
return get_origin(cls) is Literal


def has_args(cls) -> bool:
"""Check if the class has generic parameters.

Args:
cls: The class to check.

Returns:
Whether the class has generic
"""
if get_args(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):
return True

return False


def is_optional(cls: GenericType) -> bool:
"""Check if a class is an Optional.

Expand Down
9 changes: 6 additions & 3 deletions reflex/vars/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
ParsedImportDict,
parse_imports,
)
from reflex.utils.types import GenericType, Self, get_origin
from reflex.utils.types import GenericType, Self, get_origin, has_args

if TYPE_CHECKING:
from reflex.state import BaseState
Expand Down Expand Up @@ -1266,6 +1266,11 @@ def figure_out_type(value: Any) -> types.GenericType:
Returns:
The type of the value.
"""
if isinstance(value, Var):
return value._var_type
type_ = type(value)
if has_args(type_):
return type_
if isinstance(value, list):
return List[unionize(*(figure_out_type(v) for v in value))]
if isinstance(value, set):
Expand All @@ -1277,8 +1282,6 @@ def figure_out_type(value: Any) -> types.GenericType:
unionize(*(figure_out_type(k) for k in value)),
unionize(*(figure_out_type(v) for v in value.values())),
]
if isinstance(value, Var):
return value._var_type
return type(value)


Expand Down
47 changes: 46 additions & 1 deletion tests/units/utils/test_types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, List, Literal, Tuple, Union
from typing import Any, Dict, List, Literal, Tuple, Union

import pytest

Expand Down Expand Up @@ -45,3 +45,48 @@ def test_issubclass(
cls: types.GenericType, cls_check: types.GenericType, expected: bool
) -> None:
assert types._issubclass(cls, cls_check) == expected


class CustomDict(dict[str, str]):
"""A custom dict with generic arguments."""

pass


class ChildCustomDict(CustomDict):
"""A child of CustomDict."""

pass


class GenericDict(dict):
"""A generic dict with no generic arguments."""

pass


class ChildGenericDict(GenericDict):
"""A child of GenericDict."""

pass


@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.has_args(cls) == expected
28 changes: 28 additions & 0 deletions tests/units/vars/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@
from reflex.vars.base import figure_out_type


class CustomDict(dict[str, str]):
"""A custom dict with generic arguments."""

pass


class ChildCustomDict(CustomDict):
"""A child of CustomDict."""

pass


class GenericDict(dict):
"""A generic dict with no generic arguments."""

pass


class ChildGenericDict(GenericDict):
"""A child of GenericDict."""

pass


@pytest.mark.parametrize(
("value", "expected"),
[
Expand All @@ -15,6 +39,10 @@
([1, 2.0, "a"], List[Union[int, float, str]]),
({"a": 1, "b": 2}, Dict[str, int]),
({"a": 1, 2: "b"}, Dict[Union[int, str], Union[str, int]]),
(CustomDict(), CustomDict),
(ChildCustomDict(), ChildCustomDict),
(GenericDict({1: 1}), Dict[int, int]),
(ChildGenericDict({1: 1}), Dict[int, int]),
],
)
def test_figure_out_type(value, expected):
Expand Down
Loading