소스 검색

handle literal subclass (#5081)

Khaleel Al-Adhami 1 개월 전
부모
커밋
216552df62
2개의 변경된 파일16개의 추가작업 그리고 0개의 파일을 삭제
  1. 12 0
      reflex/utils/types.py
  2. 4 0
      tests/units/utils/test_utils.py

+ 12 - 0
reflex/utils/types.py

@@ -996,6 +996,18 @@ def typehint_issubclass(
             for arg in args
             for arg in args
         )
         )
 
 
+    if is_literal(possible_subclass):
+        args = get_args(possible_subclass)
+        return all(
+            _isinstance(
+                arg,
+                possible_superclass,
+                treat_mutable_obj_as_immutable=treat_mutable_superclasss_as_immutable,
+                nested=2,
+            )
+            for arg in args
+        )
+
     # Remove this check when Python 3.10 is the minimum supported version
     # Remove this check when Python 3.10 is the minimum supported version
     if hasattr(types, "UnionType"):
     if hasattr(types, "UnionType"):
         provided_type_origin = (
         provided_type_origin = (

+ 4 - 0
tests/units/utils/test_utils.py

@@ -103,6 +103,10 @@ def test_is_generic_alias(cls: type, expected: bool):
         (str, Literal["test", "value", 2, 3], True),
         (str, Literal["test", "value", 2, 3], True),
         (int, Literal["test", "value"], False),
         (int, Literal["test", "value"], False),
         (int, Literal["test", "value", 2, 3], True),
         (int, Literal["test", "value", 2, 3], True),
+        (Literal["test", "value"], str, True),
+        (Literal["test", "value", 2, 3], str, False),
+        (Literal["test", "value"], int, False),
+        (Literal["test", "value", 2, 3], int, False),
         *[
         *[
             (NoReturn, super_class, True)
             (NoReturn, super_class, True)
             for super_class in [int, float, str, bool, list, dict, object, Any]
             for super_class in [int, float, str, bool, list, dict, object, Any]