فهرست منبع

fix unionize recursion (#3948)

* fix unionize recursion

* merging

---------

Co-authored-by: Khaleel Al-Adhami <khaleel.aladhami@gmail.com>
Thomas Brandého 8 ماه پیش
والد
کامیت
44a89b2e87
2فایلهای تغییر یافته به همراه28 افزوده شده و 4 حذف شده
  1. 7 4
      reflex/vars/base.py
  2. 21 0
      tests/vars/test_base.py

+ 7 - 4
reflex/vars/base.py

@@ -1199,10 +1199,13 @@ def unionize(*args: Type) -> Type:
     """
     """
     if not args:
     if not args:
         return Any
         return Any
-    first, *rest = args
-    if not rest:
-        return first
-    return Union[first, unionize(*rest)]
+    if len(args) == 1:
+        return args[0]
+    # We are bisecting the args list here to avoid hitting the recursion limit
+    # In Python versions >= 3.11, we can simply do `return Union[*args]`
+    midpoint = len(args) // 2
+    first_half, second_half = args[:midpoint], args[midpoint:]
+    return Union[unionize(*first_half), unionize(*second_half)]
 
 
 
 
 def figure_out_type(value: Any) -> types.GenericType:
 def figure_out_type(value: Any) -> types.GenericType:

+ 21 - 0
tests/vars/test_base.py

@@ -0,0 +1,21 @@
+from typing import Dict, List, Union
+
+import pytest
+
+from reflex.vars.base import figure_out_type
+
+
+@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}, Dict[str, int]),
+        ({"a": 1, 2: "b"}, Dict[Union[int, str], Union[str, int]]),
+    ],
+)
+def test_figure_out_type(value, expected):
+    assert figure_out_type(value) == expected