From a6b9f4f1c8116e80ea355a542ded1c9b25a82c56 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Tue, 18 Feb 2025 15:27:16 -0800 Subject: [PATCH] removed even more optionals --- reflex/app.py | 9 +++------ reflex/compiler/compiler.py | 10 +++++----- reflex/compiler/utils.py | 4 ++-- reflex/components/component.py | 14 ++------------ reflex/components/core/upload.py | 4 ++-- reflex/components/datadisplay/dataeditor.py | 4 ++-- reflex/components/datadisplay/dataeditor.pyi | 2 +- reflex/components/radix/primitives/drawer.py | 4 ++-- reflex/components/radix/themes/color_mode.py | 4 ++-- .../radix/themes/components/radio_group.py | 4 ++-- reflex/components/suneditor/editor.py | 4 ++-- reflex/components/suneditor/editor.pyi | 2 +- reflex/config.py | 3 +-- reflex/event.py | 5 ++--- reflex/utils/prerequisites.py | 6 +++--- reflex/vars/base.py | 9 ++++----- reflex/vars/function.py | 3 +-- 17 files changed, 37 insertions(+), 54 deletions(-) diff --git a/reflex/app.py b/reflex/app.py index 4a1ecdcca..3492f6ec7 100644 --- a/reflex/app.py +++ b/reflex/app.py @@ -26,7 +26,6 @@ from typing import ( Coroutine, Dict, MutableMapping, - Optional, Type, Union, get_args, @@ -360,7 +359,7 @@ class App(MiddlewareMixin, LifespanMixin): html_lang: str | None = None # Attributes to add to the html root tag of every page. - html_custom_attrs: Optional[dict[str, str]] = None + html_custom_attrs: dict[str, str] | None = None # A map from a route to an unevaluated page. _unevaluated_pages: dict[str, UnevaluatedPage] = dataclasses.field( @@ -374,7 +373,7 @@ class App(MiddlewareMixin, LifespanMixin): _api: FastAPI | None = None # The state class to use for the app. - _state: Optional[Type[BaseState]] = None + _state: Type[BaseState] | None = None # Class to manage many client states. _state_manager: StateManager | None = None @@ -985,9 +984,7 @@ class App(MiddlewareMixin, LifespanMixin): for render, kwargs in DECORATED_PAGES[get_config().app_name]: self.add_page(render, **kwargs) - def _validate_var_dependencies( - self, state: Optional[Type[BaseState]] = None - ) -> None: + def _validate_var_dependencies(self, state: Type[BaseState] | None = None) -> None: """Validate the dependencies of the vars in the app. Args: diff --git a/reflex/compiler/compiler.py b/reflex/compiler/compiler.py index dea0940ee..599dc02cc 100644 --- a/reflex/compiler/compiler.py +++ b/reflex/compiler/compiler.py @@ -4,7 +4,7 @@ from __future__ import annotations from datetime import datetime from pathlib import Path -from typing import TYPE_CHECKING, Iterable, Optional, Sequence, Type +from typing import TYPE_CHECKING, Iterable, Sequence, Type from reflex import constants from reflex.compiler import templates, utils @@ -94,7 +94,7 @@ def _compile_theme(theme: str) -> str: return templates.THEME.render(theme=theme) -def _compile_contexts(state: Optional[Type[BaseState]], theme: Component | None) -> str: +def _compile_contexts(state: Type[BaseState] | None, theme: Component | None) -> str: """Compile the initial state and contexts. Args: @@ -353,7 +353,7 @@ def _compile_tailwind( def compile_document_root( head_components: list[Component], html_lang: str | None = None, - html_custom_attrs: Optional[dict[str, Var | str]] = None, + html_custom_attrs: dict[str, Var | str] | None = None, ) -> tuple[str, str]: """Compile the document root. @@ -415,7 +415,7 @@ def compile_theme(style: ComponentStyle) -> tuple[str, str]: def compile_contexts( - state: Optional[Type[BaseState]], + state: Type[BaseState] | None, theme: Component | None, ) -> tuple[str, str]: """Compile the initial state / context. @@ -681,7 +681,7 @@ class ExecutorSafeFunctions: COMPONENTS: dict[str, BaseComponent] = {} UNCOMPILED_PAGES: dict[str, UnevaluatedPage] = {} - STATE: Optional[Type[BaseState]] = None + STATE: Type[BaseState] | None = None @classmethod def compile_page(cls, route: str) -> tuple[str, str]: diff --git a/reflex/compiler/utils.py b/reflex/compiler/utils.py index 6d41d8992..856bb908a 100644 --- a/reflex/compiler/utils.py +++ b/reflex/compiler/utils.py @@ -7,7 +7,7 @@ import concurrent.futures import traceback from datetime import datetime from pathlib import Path -from typing import Any, Callable, Optional, Type +from typing import Any, Callable, Type from urllib.parse import urlparse from pydantic.v1.fields import ModelField @@ -346,7 +346,7 @@ def compile_custom_component( def create_document_root( head_components: list[Component] | None = None, html_lang: str | None = None, - html_custom_attrs: Optional[dict[str, Var | str]] = None, + html_custom_attrs: dict[str, Var | str] | None = None, ) -> Component: """Create the document root. diff --git a/reflex/components/component.py b/reflex/components/component.py index 3a81dd5aa..5f87ca839 100644 --- a/reflex/components/component.py +++ b/reflex/components/component.py @@ -9,17 +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, - Optional, - Sequence, - Type, - Union, -) +from typing import Any, Callable, ClassVar, Iterator, List, Sequence, Type, Union from typing_extensions import Self @@ -238,7 +228,7 @@ class Component(BaseComponent, ABC): _memoization_mode: MemoizationMode = MemoizationMode() # State class associated with this component instance - State: Optional[Type[reflex.state.State]] = None + State: Type[reflex.state.State] | None = None def add_imports(self) -> ImportDict | list[ImportDict]: """Add imports for the component. diff --git a/reflex/components/core/upload.py b/reflex/components/core/upload.py index 0a10ae336..39b61eef1 100644 --- a/reflex/components/core/upload.py +++ b/reflex/components/core/upload.py @@ -3,7 +3,7 @@ from __future__ import annotations from pathlib import Path -from typing import Any, Callable, ClassVar, List, Optional +from typing import Any, Callable, ClassVar, List from reflex.components.base.fragment import Fragment from reflex.components.component import ( @@ -197,7 +197,7 @@ class Upload(MemoizationLeaf): # The list of accepted file types. This should be a dictionary of MIME types as keys and array of file formats as # values. # supported MIME types: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types - accept: Var[Optional[dict[str, List]]] + accept: Var[dict[str, List] | None] # Whether the dropzone is disabled. disabled: Var[bool] diff --git a/reflex/components/datadisplay/dataeditor.py b/reflex/components/datadisplay/dataeditor.py index 165bc421e..f2e4e3e16 100644 --- a/reflex/components/datadisplay/dataeditor.py +++ b/reflex/components/datadisplay/dataeditor.py @@ -3,7 +3,7 @@ from __future__ import annotations from enum import Enum -from typing import Any, Dict, Literal, Optional, TypedDict +from typing import Any, Dict, Literal, TypedDict from reflex.base import Base from reflex.components.component import Component, NoSSRComponent @@ -148,7 +148,7 @@ class GroupHeaderClickedEventArgs(TypedDict): class GridCell(TypedDict): """The grid cell.""" - span: Optional[list[int]] + span: list[int] | None class GridColumn(TypedDict): diff --git a/reflex/components/datadisplay/dataeditor.pyi b/reflex/components/datadisplay/dataeditor.pyi index cebb63516..34c3233e0 100644 --- a/reflex/components/datadisplay/dataeditor.pyi +++ b/reflex/components/datadisplay/dataeditor.pyi @@ -118,7 +118,7 @@ class GroupHeaderClickedEventArgs(TypedDict): scrollEdge: tuple[int, int] class GridCell(TypedDict): - span: Optional[list[int]] + span: list[int] | None class GridColumn(TypedDict): title: str diff --git a/reflex/components/radix/primitives/drawer.py b/reflex/components/radix/primitives/drawer.py index 25f49e026..658562c09 100644 --- a/reflex/components/radix/primitives/drawer.py +++ b/reflex/components/radix/primitives/drawer.py @@ -4,7 +4,7 @@ # Style based on https://ui.shadcn.com/docs/components/drawer from __future__ import annotations -from typing import Any, Literal, Optional +from typing import Any, Literal from reflex.components.component import Component, ComponentNamespace from reflex.components.radix.primitives.base import RadixPrimitiveComponent @@ -58,7 +58,7 @@ class DrawerRoot(DrawerComponent): handle_only: Var[bool] # Array of numbers from 0 to 100 that corresponds to % of the screen a given snap point should take up. Should go from least visible. Also Accept px values, which doesn't take screen height into account. - snap_points: Optional[list[str | float]] + snap_points: list[str | float] | None # Index of a snapPoint from which the overlay fade should be applied. Defaults to the last snap point. fade_from_index: Var[int] diff --git a/reflex/components/radix/themes/color_mode.py b/reflex/components/radix/themes/color_mode.py index 30d88a8fe..6259e73a7 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, Optional, Union, get_args +from typing import Any, Literal, Union, 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: Optional[Union[LiteralPosition, Var[LiteralPosition]]] = None + position: Union[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/radio_group.py b/reflex/components/radix/themes/components/radio_group.py index e388ff650..4b93cb5f2 100644 --- a/reflex/components/radix/themes/components/radio_group.py +++ b/reflex/components/radix/themes/components/radio_group.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import Literal, Optional, Union +from typing import Literal import reflex as rx from reflex.components.component import Component, ComponentNamespace @@ -118,7 +118,7 @@ class HighLevelRadioGroup(RadixThemesComponent): @classmethod def create( cls, - items: Var[list[Optional[Union[str, int, float, list, dict, bool]]]], + items: Var[list[str | int | float | list | dict | bool | None]], **props, ) -> Component: """Create a radio group component. diff --git a/reflex/components/suneditor/editor.py b/reflex/components/suneditor/editor.py index d7126859f..55972cd0d 100644 --- a/reflex/components/suneditor/editor.py +++ b/reflex/components/suneditor/editor.py @@ -3,7 +3,7 @@ from __future__ import annotations import enum -from typing import Any, Dict, Literal, Optional, Union +from typing import Any, Dict, Literal, Union from reflex.base import Base from reflex.components.component import Component, NoSSRComponent @@ -65,7 +65,7 @@ class EditorOptions(Base): rtl: bool | None = None # List of buttons to use in the toolbar. - button_list: Optional[list[Union[list[str], str]]] + button_list: list[Union[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 310baa1e0..5cffa68f7 100644 --- a/reflex/components/suneditor/editor.pyi +++ b/reflex/components/suneditor/editor.pyi @@ -42,7 +42,7 @@ class EditorOptions(Base): default_tag: str | None mode: str | None rtl: bool | None - button_list: Optional[list[Union[list[str], str]]] + button_list: list[Union[list[str], str]] | None def on_blur_spec(e: Var, content: Var[str]) -> tuple[Var[str]]: ... def on_paste_spec( diff --git a/reflex/config.py b/reflex/config.py index 35db8c4b9..35592f8dd 100644 --- a/reflex/config.py +++ b/reflex/config.py @@ -23,7 +23,6 @@ from typing import ( Any, Callable, Generic, - Optional, TypeVar, get_args, get_origin, @@ -789,7 +788,7 @@ class Config(Base): cors_allowed_origins: list[str] = ["*"] # Tailwind config. - tailwind: Optional[dict[str, Any]] = {"plugins": ["@tailwindcss/typography"]} + tailwind: dict[str, Any] | None = {"plugins": ["@tailwindcss/typography"]} # Timeout when launching the gunicorn server. TODO(rename this to backend_timeout?) timeout: int = 120 diff --git a/reflex/event.py b/reflex/event.py index 2969799b3..55314b3f8 100644 --- a/reflex/event.py +++ b/reflex/event.py @@ -15,7 +15,6 @@ from typing import ( Callable, Generic, List, - Optional, Protocol, Sequence, Type, @@ -341,7 +340,7 @@ class CallableEventSpec(EventSpec): API with functions that return a family of EventSpec. """ - fn: Optional[Callable[..., EventSpec]] = None + fn: Callable[..., EventSpec] | None = None def __init__(self, fn: Callable[..., EventSpec] | None = None, **kwargs): """Initialize a CallableEventSpec. @@ -394,7 +393,7 @@ class EventChain(EventActionsMixin): default_factory=list ) - args_spec: Optional[Union[Callable, Sequence[Callable]]] = dataclasses.field( + args_spec: Union[Callable, Sequence[Callable]] | None = dataclasses.field( default=None ) diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index 3e7280f03..25ac5a274 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -23,7 +23,7 @@ import zipfile from datetime import datetime from pathlib import Path from types import ModuleType -from typing import Any, Callable, NamedTuple, Optional +from typing import Any, Callable, NamedTuple from urllib.parse import urlparse import httpx @@ -886,7 +886,7 @@ def init_reflex_json(project_hash: int | None): def update_next_config( - export: bool = False, transpile_packages: Optional[list[str]] = None + export: bool = False, transpile_packages: list[str] | None = None ): """Update Next.js config from Reflex config. @@ -908,7 +908,7 @@ def update_next_config( def _update_next_config( - config: Config, export: bool = False, transpile_packages: Optional[list[str]] = None + config: Config, export: bool = False, transpile_packages: list[str] | None = None ): next_config = { "basePath": config.frontend_path or "", diff --git a/reflex/vars/base.py b/reflex/vars/base.py index 34f623d4c..b7a6cb3ac 100644 --- a/reflex/vars/base.py +++ b/reflex/vars/base.py @@ -28,7 +28,6 @@ from typing import ( Literal, Mapping, NoReturn, - Optional, ParamSpec, Sequence, Set, @@ -1967,7 +1966,7 @@ class ComputedVar(Var[RETURN_TYPE]): fget: Callable[[BASE_STATE], RETURN_TYPE], initial_value: RETURN_TYPE | types.Unset = types.Unset(), cache: bool = True, - deps: Optional[list[str | Var]] = None, + deps: list[str | Var] | None = None, auto_deps: bool = True, interval: int | datetime.timedelta | None = None, backend: bool | None = None, @@ -2562,7 +2561,7 @@ def computed_var( fget: None = None, initial_value: Any | types.Unset = types.Unset(), cache: bool = True, - deps: Optional[list[str | Var]] = None, + deps: list[str | Var] | None = None, auto_deps: bool = True, interval: datetime.timedelta | int | None = None, backend: bool | None = None, @@ -2575,7 +2574,7 @@ def computed_var( fget: Callable[[BASE_STATE], RETURN_TYPE], initial_value: RETURN_TYPE | types.Unset = types.Unset(), cache: bool = True, - deps: Optional[list[str | Var]] = None, + deps: list[str | Var] | None = None, auto_deps: bool = True, interval: datetime.timedelta | int | None = None, backend: bool | None = None, @@ -2587,7 +2586,7 @@ def computed_var( fget: Callable[[BASE_STATE], Any] | None = None, initial_value: Any | types.Unset = types.Unset(), cache: bool = True, - deps: Optional[list[str | Var]] = None, + deps: list[str | Var] | None = None, auto_deps: bool = True, interval: datetime.timedelta | int | None = None, backend: bool | None = None, diff --git a/reflex/vars/function.py b/reflex/vars/function.py index 9d95c65e2..bd10ced48 100644 --- a/reflex/vars/function.py +++ b/reflex/vars/function.py @@ -9,7 +9,6 @@ from typing import ( Callable, Concatenate, Generic, - Optional, ParamSpec, Protocol, Sequence, @@ -242,7 +241,7 @@ class FunctionStringVar(FunctionVar[CALLABLE_TYPE]): class VarOperationCall(Generic[P, R], CachedVarOperation, Var[R]): """Base class for immutable vars that are the result of a function call.""" - _func: Optional[FunctionVar[ReflexCallable[P, R]]] = dataclasses.field(default=None) + _func: FunctionVar[ReflexCallable[P, R]] | None = dataclasses.field(default=None) _args: tuple[Var | Any, ...] = dataclasses.field(default_factory=tuple) @cached_property_no_lock