From edf1608cbb116ebcea73240f85be2818aa3dc143 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Wed, 5 Feb 2025 11:03:21 -0800 Subject: [PATCH] fix weird cases of using _isinstance instead of isinstance --- reflex/components/component.py | 4 +--- reflex/components/core/match.py | 8 ++++---- reflex/components/markdown/markdown.py | 5 ++--- reflex/components/tags/tag.py | 4 ++-- reflex/utils/types.py | 10 ++++++---- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/reflex/components/component.py b/reflex/components/component.py index bfd8d8e3b..e1a398786 100644 --- a/reflex/components/component.py +++ b/reflex/components/component.py @@ -1770,9 +1770,7 @@ class CustomComponent(Component): return [ Var( _js_expr=name, - _var_type=( - prop._var_type if types._isinstance(prop, Var) else type(prop) - ), + _var_type=(prop._var_type if isinstance(prop, Var) else type(prop)), ).guess_type() for name, prop in self.props.items() ] diff --git a/reflex/components/core/match.py b/reflex/components/core/match.py index 5c31669a1..2d936544a 100644 --- a/reflex/components/core/match.py +++ b/reflex/components/core/match.py @@ -178,9 +178,9 @@ class Match(MemoizationLeaf): first_case_return = match_cases[0][-1] return_type = type(first_case_return) - if types._isinstance(first_case_return, BaseComponent): + if isinstance(first_case_return, BaseComponent): return_type = BaseComponent - elif types._isinstance(first_case_return, Var): + elif isinstance(first_case_return, Var): return_type = Var for index, case in enumerate(match_cases): @@ -228,8 +228,8 @@ class Match(MemoizationLeaf): # Validate the match cases (as well as the default case) to have Var return types. if any( - case for case in match_cases if not types._isinstance(case[-1], Var) - ) or not types._isinstance(default, Var): + case for case in match_cases if not isinstance(case[-1], Var) + ) or not isinstance(default, Var): raise ValueError("Return types of match cases should be Vars.") return Var( diff --git a/reflex/components/markdown/markdown.py b/reflex/components/markdown/markdown.py index 27bd5bd62..510819f4d 100644 --- a/reflex/components/markdown/markdown.py +++ b/reflex/components/markdown/markdown.py @@ -6,11 +6,10 @@ import dataclasses import textwrap from functools import lru_cache from hashlib import md5 -from typing import Any, Callable, Dict, Sequence, Union +from typing import Any, Callable, Dict, Sequence from reflex.components.component import BaseComponent, Component, CustomComponent from reflex.components.tags.tag import Tag -from reflex.utils import types from reflex.utils.imports import ImportDict, ImportVar from reflex.vars.base import LiteralVar, Var from reflex.vars.function import ARRAY_ISARRAY, ArgsFunctionOperation, DestructuredArg @@ -166,7 +165,7 @@ class Markdown(Component): Returns: The markdown component. """ - if len(children) != 1 or not types._isinstance(children[0], Union[str, Var]): + if len(children) != 1 or not isinstance(children[0], (str, Var)): raise ValueError( "Markdown component must have exactly one child containing the markdown source." ) diff --git a/reflex/components/tags/tag.py b/reflex/components/tags/tag.py index 983726e56..515d9e05f 100644 --- a/reflex/components/tags/tag.py +++ b/reflex/components/tags/tag.py @@ -3,7 +3,7 @@ from __future__ import annotations import dataclasses -from typing import Any, Dict, List, Optional, Sequence, Union +from typing import Any, Dict, List, Mapping, Optional, Sequence from reflex.event import EventChain from reflex.utils import format, types @@ -103,7 +103,7 @@ class Tag: { format.to_camel_case(name, allow_hyphens=True): ( prop - if types._isinstance(prop, Union[EventChain, dict]) + if types._isinstance(prop, (EventChain, Mapping)) else LiteralVar.create(prop) ) # rx.color is always a string for name, prop in kwargs.items() diff --git a/reflex/utils/types.py b/reflex/utils/types.py index 8cf9f7335..1dd43f500 100644 --- a/reflex/utils/types.py +++ b/reflex/utils/types.py @@ -567,6 +567,11 @@ def _isinstance(obj: Any, cls: GenericType, nested: int = 0) -> bool: from reflex.vars import LiteralVar, Var + if cls and is_union(cls): + return any(_isinstance(obj, arg, nested=nested) for arg in get_args(cls)) + + if cls is Var: + return isinstance(obj, Var) if isinstance(obj, LiteralVar): return _isinstance(obj._var_value, cls, nested=nested) if isinstance(obj, Var): @@ -578,9 +583,6 @@ def _isinstance(obj: Any, cls: GenericType, nested: int = 0) -> bool: if is_literal(cls): return obj in get_args(cls) - if is_union(cls): - return any(_isinstance(obj, arg, nested=nested) for arg in get_args(cls)) - origin = get_origin(cls) if origin is None: @@ -758,7 +760,7 @@ def check_prop_in_allowed_types(prop: Any, allowed_types: Iterable) -> bool: """ from reflex.vars import Var - type_ = prop._var_type if _isinstance(prop, Var) else type(prop) + type_ = prop._var_type if isinstance(prop, Var) else type(prop) return type_ in allowed_types