From 411f46c73ead7756deb90bd4004b17f52fc43723 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Tue, 18 Feb 2025 16:09:22 -0800 Subject: [PATCH] remove a bit more unions --- reflex/app.py | 7 +- reflex/components/component.py | 6 +- reflex/components/core/breakpoints.py | 4 +- reflex/components/datadisplay/logo.py | 4 +- .../datadisplay/shiki_code_block.py | 8 +- reflex/components/el/elements/inline.py | 4 +- reflex/components/el/elements/media.py | 4 +- reflex/components/moment/moment.py | 3 +- reflex/components/plotly/plotly.py | 33 ++----- reflex/components/plotly/plotly.pyi | 10 +-- .../components/radix/primitives/accordion.py | 6 +- reflex/components/radix/themes/color_mode.py | 4 +- .../radix/themes/components/callout.py | 4 +- .../radix/themes/components/checkbox_cards.py | 10 +-- .../radix/themes/components/context_menu.py | 6 +- .../radix/themes/components/dropdown_menu.py | 6 +- .../radix/themes/components/hover_card.py | 4 +- .../radix/themes/components/popover.py | 4 +- .../radix/themes/components/radio_cards.py | 10 +-- .../themes/components/segmented_control.py | 10 +-- .../themes/components/segmented_control.pyi | 8 +- .../radix/themes/components/select.py | 4 +- reflex/components/recharts/cartesian.py | 2 +- reflex/components/suneditor/editor.py | 2 +- reflex/components/suneditor/editor.pyi | 4 +- reflex/event.py | 15 ++-- reflex/model.py | 6 +- reflex/state.py | 3 +- reflex/vars/base.py | 2 +- reflex/vars/function.py | 85 +++++++++---------- tests/units/utils/test_utils.py | 2 +- 31 files changed, 116 insertions(+), 164 deletions(-) diff --git a/reflex/app.py b/reflex/app.py index 3492f6ec7..c9a1ed69c 100644 --- a/reflex/app.py +++ b/reflex/app.py @@ -27,7 +27,6 @@ from typing import ( Dict, MutableMapping, Type, - Union, get_args, get_type_hints, ) @@ -289,7 +288,7 @@ class UnevaluatedPage: title: Var | str | None description: Var | str | None image: str - on_load: Union[EventType[()], None] + on_load: EventType[()] | None meta: list[dict[str, str]] @@ -399,7 +398,7 @@ class App(MiddlewareMixin, LifespanMixin): # Backend Error Handler Function backend_exception_handler: Callable[ - [Exception], Union[EventSpec, list[EventSpec], None] + [Exception], EventSpec | list[EventSpec] | None ] = default_backend_exception_handler # Put the toast provider in the app wrap. @@ -1491,7 +1490,7 @@ class App(MiddlewareMixin, LifespanMixin): if not valid: raise ValueError( f"Provided custom {handler_domain} exception handler `{_fn_name}` has the wrong return type." - f"Expected `Union[EventSpec, list[EventSpec], None]` but got `{return_type}`" + f"Expected `EventSpec | list[EventSpec] | None` but got `{return_type}`" ) diff --git a/reflex/components/component.py b/reflex/components/component.py index 5f87ca839..16884dc1d 100644 --- a/reflex/components/component.py +++ b/reflex/components/component.py @@ -9,7 +9,7 @@ from abc import ABC, abstractmethod from functools import lru_cache, wraps from hashlib import md5 from types import SimpleNamespace -from typing import Any, Callable, ClassVar, Iterator, List, Sequence, Type, Union +from typing import Any, Callable, ClassVar, Iterator, List, Sequence, Type from typing_extensions import Self @@ -159,9 +159,7 @@ def evaluate_style_namespaces(style: ComponentStyle) -> dict: # Map from component to styling. -ComponentStyle = dict[ - Union[str, Type[BaseComponent], Callable, ComponentNamespace], Any -] +ComponentStyle = dict[str | Type[BaseComponent] | Callable | ComponentNamespace, Any] ComponentChild = types.PrimitiveType | Var | BaseComponent ComponentChildTypes = (*types.PrimitiveTypes, Var, BaseComponent) diff --git a/reflex/components/core/breakpoints.py b/reflex/components/core/breakpoints.py index 9b0a607c0..2f113db5a 100644 --- a/reflex/components/core/breakpoints.py +++ b/reflex/components/core/breakpoints.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import TypeVar, Union +from typing import TypeVar breakpoints_values = ["30em", "48em", "62em", "80em", "96em"] breakpoint_names = ["xs", "sm", "md", "lg", "xl"] @@ -94,4 +94,4 @@ breakpoints = Breakpoints.create T = TypeVar("T") -Responsive = Union[T, Breakpoints[str, T]] +Responsive = T | Breakpoints[str, T] diff --git a/reflex/components/datadisplay/logo.py b/reflex/components/datadisplay/logo.py index dab6d2468..5c7401981 100644 --- a/reflex/components/datadisplay/logo.py +++ b/reflex/components/datadisplay/logo.py @@ -1,12 +1,10 @@ """A Reflex logo component.""" -from typing import Union - import reflex as rx def svg_logo( - color: Union[str, rx.Var[str]] = rx.color_mode_cond("#110F1F", "white"), + color: str | rx.Var[str] = rx.color_mode_cond("#110F1F", "white"), **props, ): """A Reflex logo SVG. diff --git a/reflex/components/datadisplay/shiki_code_block.py b/reflex/components/datadisplay/shiki_code_block.py index 14ba0b715..de82f0e47 100644 --- a/reflex/components/datadisplay/shiki_code_block.py +++ b/reflex/components/datadisplay/shiki_code_block.py @@ -4,7 +4,7 @@ from __future__ import annotations import re from collections import defaultdict -from typing import Any, Literal, Union +from typing import Any, Literal from reflex.base import Base from reflex.components.component import Component, ComponentNamespace @@ -547,15 +547,13 @@ class ShikiCodeBlock(Component, MarkdownComponentMap): theme: Var[LiteralCodeTheme] = Var.create("one-light") # The set of themes to use for different modes. - themes: Var[Union[list[dict[str, Any]], dict[str, str]]] + themes: Var[list[dict[str, Any]] | dict[str, str]] # The code to display. code: Var[str] # The transformers to use for the syntax highlighter. - transformers: Var[list[Union[ShikiBaseTransformers, dict[str, Any]]]] = Var.create( - [] - ) + transformers: Var[list[ShikiBaseTransformers | dict[str, Any]]] = Var.create([]) # The decorations to use for the syntax highlighter. decorations: Var[list[ShikiDecorations]] = Var.create([]) diff --git a/reflex/components/el/elements/inline.py b/reflex/components/el/elements/inline.py index bf7e24339..7b048200d 100644 --- a/reflex/components/el/elements/inline.py +++ b/reflex/components/el/elements/inline.py @@ -1,6 +1,6 @@ """Inline classes.""" -from typing import Literal, Union +from typing import Literal from reflex.vars.base import Var @@ -46,7 +46,7 @@ class A(BaseHTML): # Inherits common attributes from BaseMeta rel: Var[str] # Specifies where to open the linked document - target: Var[Union[str, Literal["_self", "_blank", "_parent", "_top"]]] + target: Var[str | Literal["_self", "_blank", "_parent", "_top"]] class Abbr(BaseHTML): diff --git a/reflex/components/el/elements/media.py b/reflex/components/el/elements/media.py index 39ee81135..3bbd40ba0 100644 --- a/reflex/components/el/elements/media.py +++ b/reflex/components/el/elements/media.py @@ -1,6 +1,6 @@ """Media classes.""" -from typing import Any, Literal, Union +from typing import Any, Literal from reflex import Component, ComponentNamespace from reflex.components.el.elements.inline import ReferrerPolicy @@ -472,7 +472,7 @@ class Stop(BaseHTML): stop_color: Var[str | Color | bool] # Opacity of the gradient stop. - stop_opacity: Var[Union[str, float, int, bool]] + stop_opacity: Var[str | float | int | bool] class Path(BaseHTML): diff --git a/reflex/components/moment/moment.py b/reflex/components/moment/moment.py index 523119485..79f0f73dd 100644 --- a/reflex/components/moment/moment.py +++ b/reflex/components/moment/moment.py @@ -2,7 +2,6 @@ import dataclasses from datetime import date, datetime, time, timedelta -from typing import Union from reflex.components.component import NoSSRComponent from reflex.event import EventHandler, passthrough_event_spec @@ -79,7 +78,7 @@ class Moment(NoSSRComponent): duration: Var[str] # The date to display (also work if passed as children). - date: Var[Union[str, datetime, date, time, timedelta]] + date: Var[str | datetime | date | time | timedelta] # Shows the duration (elapsed time) between now and the provided datetime. duration_from_now: Var[bool] diff --git a/reflex/components/plotly/plotly.py b/reflex/components/plotly/plotly.py index e2e746f0c..66a159a22 100644 --- a/reflex/components/plotly/plotly.py +++ b/reflex/components/plotly/plotly.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import Any, Dict, TypedDict, TypeVar, Union +from typing import Any, Dict, TypedDict, TypeVar from reflex.components.component import Component, NoSSRComponent from reflex.components.core.cond import color_mode_cond @@ -35,7 +35,7 @@ def _event_points_data_signature(e0: Var) -> tuple[Var[list[Point]]]: T = TypeVar("T") -ItemOrList = Union[T, list[T]] +ItemOrList = T | list[T] class BBox(TypedDict): @@ -59,33 +59,10 @@ class Point(TypedDict): lon: float | int | None curveNumber: int | None pointNumber: int | None - pointNumbers: Union[list[int], None] + pointNumbers: list[int] | None pointIndex: int | None - markerColor: Union[ - ItemOrList[ - ItemOrList[ - Union[ - float, - int, - str, - None, - ] - ] - ], - None, - ] - markerSize: Union[ - ItemOrList[ - ItemOrList[ - Union[ - float, - int, - None, - ] - ] - ], - None, - ] + markerColor: ItemOrList[ItemOrList[float | int | str | None]] | None + markerSize: ItemOrList[ItemOrList[float | int | None,]] | None bbox: BBox | None diff --git a/reflex/components/plotly/plotly.pyi b/reflex/components/plotly/plotly.pyi index 8bd470b73..0a6b0563e 100644 --- a/reflex/components/plotly/plotly.pyi +++ b/reflex/components/plotly/plotly.pyi @@ -3,7 +3,7 @@ # ------------------- DO NOT EDIT ---------------------- # This file was generated by `reflex/utils/pyi_generator.py`! # ------------------------------------------------------ -from typing import Any, Dict, Optional, TypedDict, TypeVar, Union, overload +from typing import Any, Dict, Optional, TypedDict, TypeVar, overload from reflex.components.component import NoSSRComponent from reflex.event import EventType @@ -21,7 +21,7 @@ except ImportError: Figure = Any Template = Any T = TypeVar("T") -ItemOrList = Union[T, list[T]] +ItemOrList = T | list[T] class BBox(TypedDict): x0: float | int | None @@ -39,10 +39,10 @@ class Point(TypedDict): lon: float | int | None curveNumber: int | None pointNumber: int | None - pointNumbers: Union[list[int], None] + pointNumbers: list[int] | None pointIndex: int | None - markerColor: Union[ItemOrList[ItemOrList[Union[float, int, str, None]]], None] - markerSize: Union[ItemOrList[ItemOrList[Union[float, int, None]]], None] + markerColor: ItemOrList[ItemOrList[float | int | str | None]] | None + markerSize: ItemOrList[ItemOrList[float | int | None,]] | None bbox: BBox | None class Plotly(NoSSRComponent): diff --git a/reflex/components/radix/primitives/accordion.py b/reflex/components/radix/primitives/accordion.py index 68d1655cf..ee6134ca6 100644 --- a/reflex/components/radix/primitives/accordion.py +++ b/reflex/components/radix/primitives/accordion.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import Any, Literal, Union +from typing import Any, Literal from reflex.components.component import Component, ComponentNamespace from reflex.components.core.colors import color @@ -95,10 +95,10 @@ class AccordionRoot(AccordionComponent): type: Var[LiteralAccordionType] # The value of the item to expand. - value: Var[Union[str, list[str]]] + value: Var[str | list[str]] # The default value of the item to expand. - default_value: Var[Union[str, list[str]]] + default_value: Var[str | list[str]] # Whether or not the accordion is collapsible. collapsible: Var[bool] diff --git a/reflex/components/radix/themes/color_mode.py b/reflex/components/radix/themes/color_mode.py index 6259e73a7..4de739854 100644 --- a/reflex/components/radix/themes/color_mode.py +++ b/reflex/components/radix/themes/color_mode.py @@ -17,7 +17,7 @@ rx.text( from __future__ import annotations -from typing import Any, Literal, Union, get_args +from typing import Any, Literal, get_args from reflex.components.component import BaseComponent from reflex.components.core.cond import Cond, color_mode_cond, cond @@ -99,7 +99,7 @@ class ColorModeIconButton(IconButton): """Icon Button for toggling light / dark mode via toggle_color_mode.""" # The position of the icon button. Follow document flow if None. - position: Union[LiteralPosition, Var[LiteralPosition]] | None = None + position: LiteralPosition | Var[LiteralPosition] | None = None # Allow picking the "system" value for the color mode. allow_system: bool = False diff --git a/reflex/components/radix/themes/components/callout.py b/reflex/components/radix/themes/components/callout.py index 6b0a1d399..a75b421b6 100644 --- a/reflex/components/radix/themes/components/callout.py +++ b/reflex/components/radix/themes/components/callout.py @@ -1,6 +1,6 @@ """Interactive components provided by @radix-ui/themes.""" -from typing import Literal, Union +from typing import Literal import reflex as rx from reflex.components.component import Component, ComponentNamespace @@ -57,7 +57,7 @@ class Callout(CalloutRoot): icon: Var[str] @classmethod - def create(cls, text: Union[str, Var[str]], **props) -> Component: + def create(cls, text: str | Var[str], **props) -> Component: """Create a callout component. Args: diff --git a/reflex/components/radix/themes/components/checkbox_cards.py b/reflex/components/radix/themes/components/checkbox_cards.py index 5f5fc3ae3..6fd8a7f30 100644 --- a/reflex/components/radix/themes/components/checkbox_cards.py +++ b/reflex/components/radix/themes/components/checkbox_cards.py @@ -1,7 +1,7 @@ """Components for the Radix CheckboxCards component.""" from types import SimpleNamespace -from typing import Literal, Union +from typing import Literal from reflex.components.core.breakpoints import Responsive from reflex.vars.base import Var @@ -27,14 +27,10 @@ class CheckboxCardsRoot(RadixThemesComponent): high_contrast: Var[bool] # The number of columns: - columns: Var[ - Responsive[Union[str, Literal["1", "2", "3", "4", "5", "6", "7", "8", "9"]]] - ] + columns: Var[Responsive[str | Literal["1", "2", "3", "4", "5", "6", "7", "8", "9"]]] # The gap between the checkbox cards: - gap: Var[ - Responsive[Union[str, Literal["1", "2", "3", "4", "5", "6", "7", "8", "9"]]] - ] + gap: Var[Responsive[str | Literal["1", "2", "3", "4", "5", "6", "7", "8", "9"]]] class CheckboxCardsItem(RadixThemesComponent): diff --git a/reflex/components/radix/themes/components/context_menu.py b/reflex/components/radix/themes/components/context_menu.py index ad0570be2..07000a66f 100644 --- a/reflex/components/radix/themes/components/context_menu.py +++ b/reflex/components/radix/themes/components/context_menu.py @@ -1,6 +1,6 @@ """Interactive components provided by @radix-ui/themes.""" -from typing import Literal, Union +from typing import Literal from reflex.components.component import ComponentNamespace from reflex.components.core.breakpoints import Responsive @@ -101,7 +101,7 @@ class ContextMenuContent(RadixThemesComponent): avoid_collisions: Var[bool] # The distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: { "top": 20, "left": 20 }. Defaults to 0. - collision_padding: Var[Union[float, int, dict[str, float | int]]] + collision_padding: Var[float | int | dict[str, float | int]] # The sticky behavior on the align axis. "partial" will keep the content in the boundary as long as the trigger is at least partially in the boundary whilst "always" will keep the content in the boundary regardless. Defaults to "partial". sticky: Var[LiteralStickyType] @@ -183,7 +183,7 @@ class ContextMenuSubContent(RadixThemesComponent): avoid_collisions: Var[bool] # The distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: { "top": 20, "left": 20 }. Defaults to 0. - collision_padding: Var[Union[float, int, dict[str, float | int]]] + collision_padding: Var[float | int | dict[str, float | int]] # The sticky behavior on the align axis. "partial" will keep the content in the boundary as long as the trigger is at least partially in the boundary whilst "always" will keep the content in the boundary regardless. Defaults to "partial". sticky: Var[LiteralStickyType] diff --git a/reflex/components/radix/themes/components/dropdown_menu.py b/reflex/components/radix/themes/components/dropdown_menu.py index 5ba6260a6..c75e16385 100644 --- a/reflex/components/radix/themes/components/dropdown_menu.py +++ b/reflex/components/radix/themes/components/dropdown_menu.py @@ -1,6 +1,6 @@ """Interactive components provided by @radix-ui/themes.""" -from typing import Literal, Union +from typing import Literal from reflex.components.component import ComponentNamespace from reflex.components.core.breakpoints import Responsive @@ -106,7 +106,7 @@ class DropdownMenuContent(RadixThemesComponent): avoid_collisions: Var[bool] # The distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: { "top": 20, "left": 20 }. Defaults to 0. - collision_padding: Var[Union[float, int, dict[str, float | int]]] + collision_padding: Var[float | int | dict[str, float | int]] # The sticky behavior on the align axis. "partial" will keep the content in the boundary as long as the trigger is at least partially in the boundary whilst "always" will keep the content in the boundary regardless. Defaults to "partial". sticky: Var[LiteralStickyType] @@ -188,7 +188,7 @@ class DropdownMenuSubContent(RadixThemesComponent): avoid_collisions: Var[bool] # The distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: { "top": 20, "left": 20 }. Defaults to 0. - collision_padding: Var[Union[float, int, dict[str, float | int]]] + collision_padding: Var[float | int | dict[str, float | int]] # The sticky behavior on the align axis. "partial" will keep the content in the boundary as long as the trigger is at least partially in the boundary whilst "always" will keep the content in the boundary regardless. Defaults to "partial". sticky: Var[LiteralStickyType] diff --git a/reflex/components/radix/themes/components/hover_card.py b/reflex/components/radix/themes/components/hover_card.py index a86cddc65..03559083e 100644 --- a/reflex/components/radix/themes/components/hover_card.py +++ b/reflex/components/radix/themes/components/hover_card.py @@ -1,6 +1,6 @@ """Interactive components provided by @radix-ui/themes.""" -from typing import Literal, Union +from typing import Literal from reflex.components.component import ComponentNamespace from reflex.components.core.breakpoints import Responsive @@ -62,7 +62,7 @@ class HoverCardContent(elements.Div, RadixThemesComponent): avoid_collisions: Var[bool] # The distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: { top: 20, left: 20 }. - collision_padding: Var[Union[float, int, dict[str, float | int]]] + collision_padding: Var[float | int | dict[str, float | int]] # The sticky behavior on the align axis. "partial" will keep the content in the boundary as long as the trigger is at least partially in the boundary whilst "always" will keep the content in the boundary regardless sticky: Var[Literal["partial", "always"]] diff --git a/reflex/components/radix/themes/components/popover.py b/reflex/components/radix/themes/components/popover.py index fc7ea822a..f783acf9e 100644 --- a/reflex/components/radix/themes/components/popover.py +++ b/reflex/components/radix/themes/components/popover.py @@ -1,6 +1,6 @@ """Interactive components provided by @radix-ui/themes.""" -from typing import Literal, Union +from typing import Literal from reflex.components.component import ComponentNamespace from reflex.components.core.breakpoints import Responsive @@ -62,7 +62,7 @@ class PopoverContent(elements.Div, RadixThemesComponent): avoid_collisions: Var[bool] # The distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: { "top": 20, "left": 20 }. Defaults to 0. - collision_padding: Var[Union[float, int, dict[str, float | int]]] + collision_padding: Var[float | int | dict[str, float | int]] # The sticky behavior on the align axis. "partial" will keep the content in the boundary as long as the trigger is at least partially in the boundary whilst "always" will keep the content in the boundary regardless. Defaults to "partial". sticky: Var[Literal["partial", "always"]] diff --git a/reflex/components/radix/themes/components/radio_cards.py b/reflex/components/radix/themes/components/radio_cards.py index 4a9aefc99..48bd10137 100644 --- a/reflex/components/radix/themes/components/radio_cards.py +++ b/reflex/components/radix/themes/components/radio_cards.py @@ -1,7 +1,7 @@ """Radio component from Radix Themes.""" from types import SimpleNamespace -from typing import Literal, Union +from typing import Literal from reflex.components.core.breakpoints import Responsive from reflex.event import EventHandler, passthrough_event_spec @@ -31,14 +31,10 @@ class RadioCardsRoot(RadixThemesComponent): high_contrast: Var[bool] # The number of columns: - columns: Var[ - Responsive[Union[str, Literal["1", "2", "3", "4", "5", "6", "7", "8", "9"]]] - ] + columns: Var[Responsive[str | Literal["1", "2", "3", "4", "5", "6", "7", "8", "9"]]] # The gap between the checkbox cards: - gap: Var[ - Responsive[Union[str, Literal["1", "2", "3", "4", "5", "6", "7", "8", "9"]]] - ] + gap: Var[Responsive[str | Literal["1", "2", "3", "4", "5", "6", "7", "8", "9"]]] default_value: Var[str] diff --git a/reflex/components/radix/themes/components/segmented_control.py b/reflex/components/radix/themes/components/segmented_control.py index c2a47ab97..48a412347 100644 --- a/reflex/components/radix/themes/components/segmented_control.py +++ b/reflex/components/radix/themes/components/segmented_control.py @@ -3,7 +3,7 @@ from __future__ import annotations from types import SimpleNamespace -from typing import Literal, Union +from typing import Literal from reflex.components.core.breakpoints import Responsive from reflex.event import EventHandler @@ -13,8 +13,8 @@ from ..base import LiteralAccentColor, RadixThemesComponent def on_value_change( - value: Var[Union[str, list[str]]], -) -> tuple[Var[Union[str, list[str]]]]: + value: Var[str | list[str]], +) -> tuple[Var[str | list[str]]]: """Handle the on_value_change event. Args: @@ -47,10 +47,10 @@ class SegmentedControlRoot(RadixThemesComponent): radius: Var[Literal["none", "small", "medium", "large", "full"]] # The default value of the segmented control. - default_value: Var[Union[str, list[str]]] + default_value: Var[str | list[str]] # The current value of the segmented control. - value: Var[Union[str, list[str]]] + value: Var[str | list[str]] # Handles the `onChange` event for the SegmentedControl component. on_change: EventHandler[on_value_change] diff --git a/reflex/components/radix/themes/components/segmented_control.pyi b/reflex/components/radix/themes/components/segmented_control.pyi index 657f56301..fc50a4df0 100644 --- a/reflex/components/radix/themes/components/segmented_control.pyi +++ b/reflex/components/radix/themes/components/segmented_control.pyi @@ -4,7 +4,7 @@ # This file was generated by `reflex/utils/pyi_generator.py`! # ------------------------------------------------------ from types import SimpleNamespace -from typing import Any, Literal, Optional, Union, overload +from typing import Any, Literal, Optional, overload from reflex.components.core.breakpoints import Breakpoints from reflex.event import EventType @@ -13,9 +13,7 @@ from reflex.vars.base import Var from ..base import RadixThemesComponent -def on_value_change( - value: Var[Union[str, list[str]]], -) -> tuple[Var[Union[str, list[str]]]]: ... +def on_value_change(value: Var[str | list[str]]) -> tuple[Var[str | list[str]]]: ... class SegmentedControlRoot(RadixThemesComponent): @overload @@ -104,7 +102,7 @@ class SegmentedControlRoot(RadixThemesComponent): autofocus: bool | None = None, custom_attrs: dict[str, Var | Any] | None = None, on_blur: Optional[EventType[()]] = None, - on_change: Optional[EventType[()] | EventType[Union[str, list[str]]]] = None, + on_change: Optional[EventType[()] | EventType[str | list[str]]] = None, on_click: Optional[EventType[()]] = None, on_context_menu: Optional[EventType[()]] = None, on_double_click: Optional[EventType[()]] = None, diff --git a/reflex/components/radix/themes/components/select.py b/reflex/components/radix/themes/components/select.py index b4819fb97..f21c82305 100644 --- a/reflex/components/radix/themes/components/select.py +++ b/reflex/components/radix/themes/components/select.py @@ -1,6 +1,6 @@ """Interactive components provided by @radix-ui/themes.""" -from typing import Literal, Union +from typing import Literal import reflex as rx from reflex.components.component import Component, ComponentNamespace @@ -179,7 +179,7 @@ class HighLevelSelect(SelectRoot): position: Var[Literal["item-aligned", "popper"]] @classmethod - def create(cls, items: Union[list[str], Var[list[str]]], **props) -> Component: + def create(cls, items: list[str] | Var[list[str]], **props) -> Component: """Create a select component. Args: diff --git a/reflex/components/recharts/cartesian.py b/reflex/components/recharts/cartesian.py index fd148ae22..4a8e3b79c 100644 --- a/reflex/components/recharts/cartesian.py +++ b/reflex/components/recharts/cartesian.py @@ -413,7 +413,7 @@ class Bar(Cartesian): max_bar_size: Var[int] # If set a value, the option is the radius of all the rounded corners. If set a array, the option are in turn the radiuses of top-left corner, top-right corner, bottom-right corner, bottom-left corner. Default: 0 - radius: Var[Union[int, list[int]]] + radius: Var[int | list[int]] # The active bar is shown when a user enters a bar chart and this chart has tooltip. If set to false, no active bar will be drawn. If set to true, active bar will be drawn with the props calculated internally. If passed an object, active bar will be drawn, and the internally calculated props will be merged with the key value pairs of the passed object. # active_bar: Var[Union[bool, dict[str, Any]]] #noqa: ERA001 diff --git a/reflex/components/suneditor/editor.py b/reflex/components/suneditor/editor.py index 55972cd0d..75d9d717c 100644 --- a/reflex/components/suneditor/editor.py +++ b/reflex/components/suneditor/editor.py @@ -65,7 +65,7 @@ class EditorOptions(Base): rtl: bool | None = None # List of buttons to use in the toolbar. - button_list: list[Union[list[str], str]] | None + button_list: list[list[str] | str] | None def on_blur_spec(e: Var, content: Var[str]) -> tuple[Var[str]]: diff --git a/reflex/components/suneditor/editor.pyi b/reflex/components/suneditor/editor.pyi index 5cffa68f7..02db4ce07 100644 --- a/reflex/components/suneditor/editor.pyi +++ b/reflex/components/suneditor/editor.pyi @@ -4,7 +4,7 @@ # This file was generated by `reflex/utils/pyi_generator.py`! # ------------------------------------------------------ import enum -from typing import Any, Dict, Literal, Optional, Union, overload +from typing import Any, Dict, Literal, Optional, overload from reflex.base import Base from reflex.components.component import NoSSRComponent @@ -42,7 +42,7 @@ class EditorOptions(Base): default_tag: str | None mode: str | None rtl: bool | None - button_list: list[Union[list[str], str]] | None + button_list: list[list[str] | str] | None def on_blur_spec(e: Var, content: Var[str]) -> tuple[Var[str]]: ... def on_paste_spec( diff --git a/reflex/event.py b/reflex/event.py index 55314b3f8..147d2e565 100644 --- a/reflex/event.py +++ b/reflex/event.py @@ -20,7 +20,6 @@ from typing import ( Type, TypedDict, TypeVar, - Union, get_args, get_origin, get_type_hints, @@ -393,9 +392,7 @@ class EventChain(EventActionsMixin): default_factory=list ) - args_spec: Union[Callable, Sequence[Callable]] | None = dataclasses.field( - default=None - ) + args_spec: Callable | Sequence[Callable] | None = dataclasses.field(default=None) invocation: Var | None = dataclasses.field(default=None) @@ -979,7 +976,7 @@ def remove_session_storage(key: str) -> EventSpec: ) -def set_clipboard(content: Union[str, Var[str]]) -> EventSpec: +def set_clipboard(content: str | Var[str]) -> EventSpec: """Set the text in content in the clipboard. Args: @@ -1943,10 +1940,10 @@ class EventNamespace(types.SimpleNamespace): func: Callable[[BASE_STATE, Unpack[P]], Any] | None = None, *, background: bool | None = None, - ) -> Union[ - EventCallback[Unpack[P]], - Callable[[Callable[[BASE_STATE, Unpack[P]], Any]], EventCallback[Unpack[P]]], - ]: + ) -> ( + EventCallback[Unpack[P]] + | Callable[[Callable[[BASE_STATE, Unpack[P]], Any]], EventCallback[Unpack[P]]] + ): """Wrap a function to be used as an event. Args: diff --git a/reflex/model.py b/reflex/model.py index 377c9d75a..8550da1fd 100644 --- a/reflex/model.py +++ b/reflex/model.py @@ -5,7 +5,7 @@ from __future__ import annotations import re from collections import defaultdict from contextlib import suppress -from typing import Any, ClassVar, Type, Union +from typing import Any, ClassVar, Type import alembic.autogenerate import alembic.command @@ -161,9 +161,7 @@ async def get_db_status() -> dict[str, bool]: return {"db": status} -SQLModelOrSqlAlchemy = Union[ - Type[sqlmodel.SQLModel], Type[sqlalchemy.orm.DeclarativeBase] -] +SQLModelOrSqlAlchemy = Type[sqlmodel.SQLModel] | Type[sqlalchemy.orm.DeclarativeBase] class ModelRegistry: diff --git a/reflex/state.py b/reflex/state.py index 107f110e0..56a449cdd 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -33,7 +33,6 @@ from typing import ( Tuple, Type, TypeVar, - Union, cast, get_args, get_type_hints, @@ -1694,7 +1693,7 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow): return StateUpdate() event_specs_correct_type = cast( - Union[list[EventSpec | EventHandler], None], + list[EventSpec | EventHandler] | None, [event_specs] if isinstance(event_specs, EventSpec) else event_specs, ) fixed_events = fix_events( diff --git a/reflex/vars/base.py b/reflex/vars/base.py index b7a6cb3ac..0c8aafc8c 100644 --- a/reflex/vars/base.py +++ b/reflex/vars/base.py @@ -2405,7 +2405,7 @@ class ComputedVar(Var[RETURN_TYPE]): return self._fget -class DynamicRouteVar(ComputedVar[Union[str, list[str]]]): +class DynamicRouteVar(ComputedVar[str | list[str]]): """A ComputedVar that represents a dynamic route.""" pass diff --git a/reflex/vars/function.py b/reflex/vars/function.py index bd10ced48..e1adc311a 100644 --- a/reflex/vars/function.py +++ b/reflex/vars/function.py @@ -14,7 +14,6 @@ from typing import ( Sequence, Type, TypeVar, - Union, overload, ) @@ -54,52 +53,52 @@ class FunctionVar(Var[CALLABLE_TYPE], default_type=ReflexCallable[Any, Any]): @overload def partial( self: FunctionVar[ReflexCallable[Concatenate[V1, P], R]], - arg1: Union[V1, Var[V1]], + arg1: V1 | Var[V1], ) -> FunctionVar[ReflexCallable[P, R]]: ... @overload def partial( self: FunctionVar[ReflexCallable[Concatenate[V1, V2, P], R]], - arg1: Union[V1, Var[V1]], - arg2: Union[V2, Var[V2]], + arg1: V1 | Var[V1], + arg2: V2 | Var[V2], ) -> FunctionVar[ReflexCallable[P, R]]: ... @overload def partial( self: FunctionVar[ReflexCallable[Concatenate[V1, V2, V3, P], R]], - arg1: Union[V1, Var[V1]], - arg2: Union[V2, Var[V2]], - arg3: Union[V3, Var[V3]], + arg1: V1 | Var[V1], + arg2: V2 | Var[V2], + arg3: V3 | Var[V3], ) -> FunctionVar[ReflexCallable[P, R]]: ... @overload def partial( self: FunctionVar[ReflexCallable[Concatenate[V1, V2, V3, V4, P], R]], - arg1: Union[V1, Var[V1]], - arg2: Union[V2, Var[V2]], - arg3: Union[V3, Var[V3]], - arg4: Union[V4, Var[V4]], + arg1: V1 | Var[V1], + arg2: V2 | Var[V2], + arg3: V3 | Var[V3], + arg4: V4 | Var[V4], ) -> FunctionVar[ReflexCallable[P, R]]: ... @overload def partial( self: FunctionVar[ReflexCallable[Concatenate[V1, V2, V3, V4, V5, P], R]], - arg1: Union[V1, Var[V1]], - arg2: Union[V2, Var[V2]], - arg3: Union[V3, Var[V3]], - arg4: Union[V4, Var[V4]], - arg5: Union[V5, Var[V5]], + arg1: V1 | Var[V1], + arg2: V2 | Var[V2], + arg3: V3 | Var[V3], + arg4: V4 | Var[V4], + arg5: V5 | Var[V5], ) -> FunctionVar[ReflexCallable[P, R]]: ... @overload def partial( self: FunctionVar[ReflexCallable[Concatenate[V1, V2, V3, V4, V5, V6, P], R]], - arg1: Union[V1, Var[V1]], - arg2: Union[V2, Var[V2]], - arg3: Union[V3, Var[V3]], - arg4: Union[V4, Var[V4]], - arg5: Union[V5, Var[V5]], - arg6: Union[V6, Var[V6]], + arg1: V1 | Var[V1], + arg2: V2 | Var[V2], + arg3: V3 | Var[V3], + arg4: V4 | Var[V4], + arg5: V5 | Var[V5], + arg6: V6 | Var[V6], ) -> FunctionVar[ReflexCallable[P, R]]: ... @overload @@ -128,52 +127,52 @@ class FunctionVar(Var[CALLABLE_TYPE], default_type=ReflexCallable[Any, Any]): @overload def call( - self: FunctionVar[ReflexCallable[[V1], R]], arg1: Union[V1, Var[V1]] + self: FunctionVar[ReflexCallable[[V1], R]], arg1: V1 | Var[V1] ) -> VarOperationCall[[V1], R]: ... @overload def call( self: FunctionVar[ReflexCallable[[V1, V2], R]], - arg1: Union[V1, Var[V1]], - arg2: Union[V2, Var[V2]], + arg1: V1 | Var[V1], + arg2: V2 | Var[V2], ) -> VarOperationCall[[V1, V2], R]: ... @overload def call( self: FunctionVar[ReflexCallable[[V1, V2, V3], R]], - arg1: Union[V1, Var[V1]], - arg2: Union[V2, Var[V2]], - arg3: Union[V3, Var[V3]], + arg1: V1 | Var[V1], + arg2: V2 | Var[V2], + arg3: V3 | Var[V3], ) -> VarOperationCall[[V1, V2, V3], R]: ... @overload def call( self: FunctionVar[ReflexCallable[[V1, V2, V3, V4], R]], - arg1: Union[V1, Var[V1]], - arg2: Union[V2, Var[V2]], - arg3: Union[V3, Var[V3]], - arg4: Union[V4, Var[V4]], + arg1: V1 | Var[V1], + arg2: V2 | Var[V2], + arg3: V3 | Var[V3], + arg4: V4 | Var[V4], ) -> VarOperationCall[[V1, V2, V3, V4], R]: ... @overload def call( self: FunctionVar[ReflexCallable[[V1, V2, V3, V4, V5], R]], - arg1: Union[V1, Var[V1]], - arg2: Union[V2, Var[V2]], - arg3: Union[V3, Var[V3]], - arg4: Union[V4, Var[V4]], - arg5: Union[V5, Var[V5]], + arg1: V1 | Var[V1], + arg2: V2 | Var[V2], + arg3: V3 | Var[V3], + arg4: V4 | Var[V4], + arg5: V5 | Var[V5], ) -> VarOperationCall[[V1, V2, V3, V4, V5], R]: ... @overload def call( self: FunctionVar[ReflexCallable[[V1, V2, V3, V4, V5, V6], R]], - arg1: Union[V1, Var[V1]], - arg2: Union[V2, Var[V2]], - arg3: Union[V3, Var[V3]], - arg4: Union[V4, Var[V4]], - arg5: Union[V5, Var[V5]], - arg6: Union[V6, Var[V6]], + arg1: V1 | Var[V1], + arg2: V2 | Var[V2], + arg3: V3 | Var[V3], + arg4: V4 | Var[V4], + arg5: V5 | Var[V5], + arg6: V6 | Var[V6], ) -> VarOperationCall[[V1, V2, V3, V4, V5, V6], R]: ... @overload diff --git a/tests/units/utils/test_utils.py b/tests/units/utils/test_utils.py index 96f273e14..754963cd5 100644 --- a/tests/units/utils/test_utils.py +++ b/tests/units/utils/test_utils.py @@ -255,7 +255,7 @@ def test_is_backend_base_variable( (list[int], list[int], True), (list[int], list[float], True), (int | float, int | float, False), - (Union[int, Var[int]], Var[int], False), + (int | Var[int], Var[int], False), (int, Any, True), (Any, Any, True), (int | float, Any, True),