reflex/tests/units/vars/test_base.py
Khaleel Al-Adhami bea266b8ed
make object var handle all mapping instead of just dict (#4602)
* make object var handle all mapping instead of just dict

* unbreak ci

* get it right pyright

* create generic variable for field

* add support for typeddict (to some degree)

* import from extensions
2025-01-21 13:14:02 -08:00

50 lines
1.0 KiB
Python

from typing import List, Mapping, Union
import pytest
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"),
[
(1, int),
(1.0, float),
("a", str),
([1, 2, 3], List[int]),
([1, 2.0, "a"], List[Union[int, float, str]]),
({"a": 1, "b": 2}, Mapping[str, int]),
({"a": 1, 2: "b"}, Mapping[Union[int, str], Union[str, int]]),
(CustomDict(), CustomDict),
(ChildCustomDict(), ChildCustomDict),
(GenericDict({1: 1}), Mapping[int, int]),
(ChildGenericDict({1: 1}), Mapping[int, int]),
],
)
def test_figure_out_type(value, expected):
assert figure_out_type(value) == expected