test_base.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from typing import Dict, List, Union
  2. import pytest
  3. from reflex.vars.base import figure_out_type
  4. class CustomDict(dict[str, str]):
  5. """A custom dict with generic arguments."""
  6. pass
  7. class ChildCustomDict(CustomDict):
  8. """A child of CustomDict."""
  9. pass
  10. class GenericDict(dict):
  11. """A generic dict with no generic arguments."""
  12. pass
  13. class ChildGenericDict(GenericDict):
  14. """A child of GenericDict."""
  15. pass
  16. @pytest.mark.parametrize(
  17. ("value", "expected"),
  18. [
  19. (1, int),
  20. (1.0, float),
  21. ("a", str),
  22. ([1, 2, 3], List[int]),
  23. ([1, 2.0, "a"], List[Union[int, float, str]]),
  24. ({"a": 1, "b": 2}, Dict[str, int]),
  25. ({"a": 1, 2: "b"}, Dict[Union[int, str], Union[str, int]]),
  26. (CustomDict(), CustomDict),
  27. (ChildCustomDict(), ChildCustomDict),
  28. (GenericDict({1: 1}), Dict[int, int]),
  29. (ChildGenericDict({1: 1}), Dict[int, int]),
  30. ],
  31. )
  32. def test_figure_out_type(value, expected):
  33. assert figure_out_type(value) == expected