reflex 0.7.14a6__py3-none-any.whl → 0.8.0a2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of reflex might be problematic. Click here for more details.
- reflex/.templates/jinja/app/rxconfig.py.jinja2 +4 -1
- reflex/.templates/jinja/web/package.json.jinja2 +1 -1
- reflex/.templates/jinja/web/pages/_app.js.jinja2 +16 -10
- reflex/.templates/jinja/web/pages/_document.js.jinja2 +1 -1
- reflex/.templates/jinja/web/pages/base_page.js.jinja2 +0 -1
- reflex/.templates/jinja/web/pages/stateful_component.js.jinja2 +4 -0
- reflex/.templates/jinja/web/utils/context.js.jinja2 +25 -8
- reflex/.templates/web/app/entry.client.js +8 -0
- reflex/.templates/web/app/routes.js +10 -0
- reflex/.templates/web/components/reflex/radix_themes_color_mode_provider.js +12 -37
- reflex/.templates/web/postcss.config.js +1 -1
- reflex/.templates/web/react-router.config.js +6 -0
- reflex/.templates/web/utils/client_side_routing.js +21 -19
- reflex/.templates/web/utils/react-theme.js +92 -0
- reflex/.templates/web/utils/state.js +160 -67
- reflex/.templates/web/vite.config.js +32 -0
- reflex/__init__.py +1 -6
- reflex/__init__.pyi +0 -4
- reflex/app.py +53 -116
- reflex/base.py +1 -87
- reflex/compiler/compiler.py +41 -8
- reflex/compiler/templates.py +3 -3
- reflex/compiler/utils.py +73 -33
- reflex/components/__init__.py +0 -2
- reflex/components/__init__.pyi +0 -3
- reflex/components/base/__init__.py +1 -5
- reflex/components/base/__init__.pyi +4 -6
- reflex/components/base/app_wrap.pyi +5 -4
- reflex/components/base/body.pyi +5 -4
- reflex/components/base/document.py +18 -14
- reflex/components/base/document.pyi +83 -27
- reflex/components/base/error_boundary.pyi +5 -4
- reflex/components/base/fragment.pyi +5 -4
- reflex/components/base/link.pyi +9 -7
- reflex/components/base/meta.pyi +17 -13
- reflex/components/base/script.py +60 -58
- reflex/components/base/script.pyi +246 -31
- reflex/components/base/strict_mode.pyi +5 -4
- reflex/components/component.py +146 -217
- reflex/components/core/__init__.py +1 -0
- reflex/components/core/__init__.pyi +1 -0
- reflex/components/core/auto_scroll.pyi +5 -4
- reflex/components/core/banner.pyi +25 -19
- reflex/components/core/client_side_routing.py +7 -6
- reflex/components/core/client_side_routing.pyi +6 -56
- reflex/components/core/clipboard.pyi +5 -4
- reflex/components/core/debounce.py +1 -0
- reflex/components/core/debounce.pyi +5 -4
- reflex/components/core/foreach.py +3 -2
- reflex/components/core/helmet.py +14 -0
- reflex/components/{next/base.pyi → core/helmet.pyi} +10 -7
- reflex/components/core/html.pyi +5 -4
- reflex/components/core/sticky.pyi +17 -13
- reflex/components/core/upload.py +2 -1
- reflex/components/core/upload.pyi +21 -16
- reflex/components/datadisplay/code.py +2 -72
- reflex/components/datadisplay/code.pyi +9 -10
- reflex/components/datadisplay/dataeditor.pyi +11 -6
- reflex/components/datadisplay/shiki_code_block.pyi +13 -10
- reflex/components/dynamic.py +5 -5
- reflex/components/el/element.pyi +5 -4
- reflex/components/el/elements/base.pyi +5 -4
- reflex/components/el/elements/forms.pyi +69 -52
- reflex/components/el/elements/inline.pyi +113 -85
- reflex/components/el/elements/media.pyi +105 -79
- reflex/components/el/elements/metadata.pyi +25 -19
- reflex/components/el/elements/other.pyi +29 -22
- reflex/components/el/elements/scripts.pyi +13 -10
- reflex/components/el/elements/sectioning.pyi +61 -46
- reflex/components/el/elements/tables.pyi +41 -31
- reflex/components/el/elements/typography.pyi +61 -46
- reflex/components/field.py +175 -0
- reflex/components/gridjs/datatable.py +2 -2
- reflex/components/gridjs/datatable.pyi +11 -9
- reflex/components/lucide/icon.py +6 -2
- reflex/components/lucide/icon.pyi +15 -10
- reflex/components/markdown/markdown.pyi +5 -4
- reflex/components/moment/moment.pyi +5 -4
- reflex/components/plotly/plotly.pyi +19 -10
- reflex/components/props.py +376 -27
- reflex/components/radix/primitives/accordion.py +8 -1
- reflex/components/radix/primitives/accordion.pyi +29 -22
- reflex/components/radix/primitives/base.pyi +9 -7
- reflex/components/radix/primitives/drawer.pyi +45 -34
- reflex/components/radix/primitives/form.pyi +41 -31
- reflex/components/radix/primitives/progress.pyi +21 -16
- reflex/components/radix/primitives/slider.pyi +21 -16
- reflex/components/radix/themes/base.py +3 -3
- reflex/components/radix/themes/base.pyi +33 -25
- reflex/components/radix/themes/color_mode.pyi +13 -10
- reflex/components/radix/themes/components/alert_dialog.pyi +29 -22
- reflex/components/radix/themes/components/aspect_ratio.pyi +5 -4
- reflex/components/radix/themes/components/avatar.pyi +5 -4
- reflex/components/radix/themes/components/badge.pyi +5 -4
- reflex/components/radix/themes/components/button.pyi +5 -4
- reflex/components/radix/themes/components/callout.pyi +21 -16
- reflex/components/radix/themes/components/card.pyi +5 -4
- reflex/components/radix/themes/components/checkbox.pyi +13 -10
- reflex/components/radix/themes/components/checkbox_cards.pyi +9 -7
- reflex/components/radix/themes/components/checkbox_group.pyi +9 -7
- reflex/components/radix/themes/components/context_menu.pyi +53 -40
- reflex/components/radix/themes/components/data_list.pyi +17 -13
- reflex/components/radix/themes/components/dialog.pyi +29 -22
- reflex/components/radix/themes/components/dropdown_menu.pyi +33 -25
- reflex/components/radix/themes/components/hover_card.pyi +17 -13
- reflex/components/radix/themes/components/icon_button.pyi +5 -4
- reflex/components/radix/themes/components/inset.pyi +5 -4
- reflex/components/radix/themes/components/popover.pyi +17 -13
- reflex/components/radix/themes/components/progress.pyi +5 -4
- reflex/components/radix/themes/components/radio.pyi +5 -4
- reflex/components/radix/themes/components/radio_cards.pyi +9 -7
- reflex/components/radix/themes/components/radio_group.pyi +17 -13
- reflex/components/radix/themes/components/scroll_area.pyi +5 -4
- reflex/components/radix/themes/components/segmented_control.pyi +9 -7
- reflex/components/radix/themes/components/select.pyi +37 -28
- reflex/components/radix/themes/components/separator.pyi +5 -4
- reflex/components/radix/themes/components/skeleton.pyi +5 -4
- reflex/components/radix/themes/components/slider.pyi +5 -4
- reflex/components/radix/themes/components/spinner.pyi +5 -4
- reflex/components/radix/themes/components/switch.pyi +5 -4
- reflex/components/radix/themes/components/table.pyi +29 -22
- reflex/components/radix/themes/components/tabs.pyi +21 -16
- reflex/components/radix/themes/components/text_area.pyi +5 -4
- reflex/components/radix/themes/components/text_field.pyi +13 -10
- reflex/components/radix/themes/components/tooltip.pyi +5 -4
- reflex/components/radix/themes/layout/base.pyi +5 -4
- reflex/components/radix/themes/layout/box.pyi +5 -4
- reflex/components/radix/themes/layout/center.pyi +5 -4
- reflex/components/radix/themes/layout/container.pyi +5 -4
- reflex/components/radix/themes/layout/flex.pyi +5 -4
- reflex/components/radix/themes/layout/grid.pyi +5 -4
- reflex/components/radix/themes/layout/list.pyi +21 -16
- reflex/components/radix/themes/layout/section.pyi +5 -4
- reflex/components/radix/themes/layout/spacer.pyi +5 -4
- reflex/components/radix/themes/layout/stack.pyi +13 -10
- reflex/components/radix/themes/typography/blockquote.pyi +5 -4
- reflex/components/radix/themes/typography/code.pyi +5 -4
- reflex/components/radix/themes/typography/heading.pyi +5 -4
- reflex/components/radix/themes/typography/link.py +46 -11
- reflex/components/radix/themes/typography/link.pyi +311 -6
- reflex/components/radix/themes/typography/text.pyi +29 -22
- reflex/components/react_player/audio.pyi +5 -4
- reflex/components/react_player/react_player.pyi +5 -4
- reflex/components/react_player/video.pyi +5 -4
- reflex/components/recharts/cartesian.py +2 -1
- reflex/components/recharts/cartesian.pyi +65 -46
- reflex/components/recharts/charts.py +4 -2
- reflex/components/recharts/charts.pyi +36 -24
- reflex/components/recharts/general.pyi +24 -18
- reflex/components/recharts/polar.py +8 -4
- reflex/components/recharts/polar.pyi +16 -10
- reflex/components/recharts/recharts.pyi +9 -7
- reflex/components/sonner/toast.py +2 -2
- reflex/components/sonner/toast.pyi +10 -8
- reflex/config.py +3 -77
- reflex/constants/__init__.py +2 -2
- reflex/constants/base.py +28 -11
- reflex/constants/compiler.py +5 -3
- reflex/constants/event.py +1 -0
- reflex/constants/installer.py +22 -16
- reflex/constants/route.py +19 -7
- reflex/constants/state.py +2 -0
- reflex/custom_components/custom_components.py +0 -14
- reflex/environment.py +1 -1
- reflex/event.py +178 -81
- reflex/experimental/__init__.py +0 -30
- reflex/istate/proxy.py +5 -3
- reflex/page.py +0 -27
- reflex/plugins/__init__.py +3 -2
- reflex/plugins/base.py +5 -1
- reflex/plugins/shared_tailwind.py +158 -0
- reflex/plugins/sitemap.py +206 -0
- reflex/plugins/tailwind_v3.py +13 -106
- reflex/plugins/tailwind_v4.py +15 -108
- reflex/reflex.py +1 -0
- reflex/route.py +15 -21
- reflex/state.py +134 -140
- reflex/testing.py +58 -10
- reflex/utils/build.py +38 -82
- reflex/utils/exec.py +59 -161
- reflex/utils/export.py +2 -2
- reflex/utils/imports.py +0 -4
- reflex/utils/misc.py +28 -0
- reflex/utils/prerequisites.py +65 -62
- reflex/utils/processes.py +8 -7
- reflex/utils/pyi_generator.py +21 -9
- reflex/utils/serializers.py +14 -1
- reflex/utils/types.py +196 -61
- reflex/vars/__init__.py +2 -0
- reflex/vars/base.py +367 -134
- {reflex-0.7.14a6.dist-info → reflex-0.8.0a2.dist-info}/METADATA +12 -5
- {reflex-0.7.14a6.dist-info → reflex-0.8.0a2.dist-info}/RECORD +195 -202
- reflex/.templates/web/next.config.js +0 -7
- reflex/components/base/head.py +0 -20
- reflex/components/base/head.pyi +0 -116
- reflex/components/next/__init__.py +0 -10
- reflex/components/next/base.py +0 -7
- reflex/components/next/image.py +0 -117
- reflex/components/next/image.pyi +0 -94
- reflex/components/next/link.py +0 -20
- reflex/components/next/link.pyi +0 -67
- reflex/components/next/video.py +0 -38
- reflex/components/next/video.pyi +0 -68
- reflex/components/suneditor/__init__.py +0 -5
- reflex/components/suneditor/editor.py +0 -269
- reflex/components/suneditor/editor.pyi +0 -199
- reflex/experimental/layout.py +0 -254
- reflex/experimental/layout.pyi +0 -814
- {reflex-0.7.14a6.dist-info → reflex-0.8.0a2.dist-info}/WHEEL +0 -0
- {reflex-0.7.14a6.dist-info → reflex-0.8.0a2.dist-info}/entry_points.txt +0 -0
- {reflex-0.7.14a6.dist-info → reflex-0.8.0a2.dist-info}/licenses/LICENSE +0 -0
reflex/utils/types.py
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import dataclasses
|
|
6
|
-
import
|
|
6
|
+
import sys
|
|
7
7
|
import types
|
|
8
8
|
from collections.abc import Callable, Iterable, Mapping, Sequence
|
|
9
|
-
from functools import cached_property, lru_cache
|
|
9
|
+
from functools import cached_property, lru_cache
|
|
10
10
|
from types import GenericAlias
|
|
11
11
|
from typing import ( # noqa: UP035
|
|
12
12
|
TYPE_CHECKING,
|
|
@@ -19,8 +19,11 @@ from typing import ( # noqa: UP035
|
|
|
19
19
|
Literal,
|
|
20
20
|
MutableMapping,
|
|
21
21
|
NoReturn,
|
|
22
|
+
Protocol,
|
|
22
23
|
Tuple,
|
|
24
|
+
TypeVar,
|
|
23
25
|
Union,
|
|
26
|
+
_eval_type, # pyright: ignore [reportAttributeAccessIssue]
|
|
24
27
|
_GenericAlias, # pyright: ignore [reportAttributeAccessIssue]
|
|
25
28
|
_SpecialGenericAlias, # pyright: ignore [reportAttributeAccessIssue]
|
|
26
29
|
get_args,
|
|
@@ -58,18 +61,91 @@ StateIterVar = list | set | tuple
|
|
|
58
61
|
if TYPE_CHECKING:
|
|
59
62
|
from reflex.vars.base import Var
|
|
60
63
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
64
|
+
VAR1 = TypeVar("VAR1", bound="Var")
|
|
65
|
+
VAR2 = TypeVar("VAR2", bound="Var")
|
|
66
|
+
VAR3 = TypeVar("VAR3", bound="Var")
|
|
67
|
+
VAR4 = TypeVar("VAR4", bound="Var")
|
|
68
|
+
VAR5 = TypeVar("VAR5", bound="Var")
|
|
69
|
+
VAR6 = TypeVar("VAR6", bound="Var")
|
|
70
|
+
VAR7 = TypeVar("VAR7", bound="Var")
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class _ArgsSpec0(Protocol):
|
|
74
|
+
def __call__(self) -> Sequence[Var]: ...
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class _ArgsSpec1(Protocol):
|
|
78
|
+
def __call__(self, var1: VAR1, /) -> Sequence[Var]: ... # pyright: ignore [reportInvalidTypeVarUse]
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class _ArgsSpec2(Protocol):
|
|
82
|
+
def __call__(self, var1: VAR1, var2: VAR2, /) -> Sequence[Var]: ... # pyright: ignore [reportInvalidTypeVarUse]
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class _ArgsSpec3(Protocol):
|
|
86
|
+
def __call__(self, var1: VAR1, var2: VAR2, var3: VAR3, /) -> Sequence[Var]: ... # pyright: ignore [reportInvalidTypeVarUse]
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class _ArgsSpec4(Protocol):
|
|
90
|
+
def __call__(
|
|
91
|
+
self,
|
|
92
|
+
var1: VAR1, # pyright: ignore [reportInvalidTypeVarUse]
|
|
93
|
+
var2: VAR2, # pyright: ignore [reportInvalidTypeVarUse]
|
|
94
|
+
var3: VAR3, # pyright: ignore [reportInvalidTypeVarUse]
|
|
95
|
+
var4: VAR4, # pyright: ignore [reportInvalidTypeVarUse]
|
|
96
|
+
/,
|
|
97
|
+
) -> Sequence[Var]: ...
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class _ArgsSpec5(Protocol):
|
|
101
|
+
def __call__(
|
|
102
|
+
self,
|
|
103
|
+
var1: VAR1, # pyright: ignore [reportInvalidTypeVarUse]
|
|
104
|
+
var2: VAR2, # pyright: ignore [reportInvalidTypeVarUse]
|
|
105
|
+
var3: VAR3, # pyright: ignore [reportInvalidTypeVarUse]
|
|
106
|
+
var4: VAR4, # pyright: ignore [reportInvalidTypeVarUse]
|
|
107
|
+
var5: VAR5, # pyright: ignore [reportInvalidTypeVarUse]
|
|
108
|
+
/,
|
|
109
|
+
) -> Sequence[Var]: ...
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
class _ArgsSpec6(Protocol):
|
|
113
|
+
def __call__(
|
|
114
|
+
self,
|
|
115
|
+
var1: VAR1, # pyright: ignore [reportInvalidTypeVarUse]
|
|
116
|
+
var2: VAR2, # pyright: ignore [reportInvalidTypeVarUse]
|
|
117
|
+
var3: VAR3, # pyright: ignore [reportInvalidTypeVarUse]
|
|
118
|
+
var4: VAR4, # pyright: ignore [reportInvalidTypeVarUse]
|
|
119
|
+
var5: VAR5, # pyright: ignore [reportInvalidTypeVarUse]
|
|
120
|
+
var6: VAR6, # pyright: ignore [reportInvalidTypeVarUse]
|
|
121
|
+
/,
|
|
122
|
+
) -> Sequence[Var]: ...
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
class _ArgsSpec7(Protocol):
|
|
126
|
+
def __call__(
|
|
127
|
+
self,
|
|
128
|
+
var1: VAR1, # pyright: ignore [reportInvalidTypeVarUse]
|
|
129
|
+
var2: VAR2, # pyright: ignore [reportInvalidTypeVarUse]
|
|
130
|
+
var3: VAR3, # pyright: ignore [reportInvalidTypeVarUse]
|
|
131
|
+
var4: VAR4, # pyright: ignore [reportInvalidTypeVarUse]
|
|
132
|
+
var5: VAR5, # pyright: ignore [reportInvalidTypeVarUse]
|
|
133
|
+
var6: VAR6, # pyright: ignore [reportInvalidTypeVarUse]
|
|
134
|
+
var7: VAR7, # pyright: ignore [reportInvalidTypeVarUse]
|
|
135
|
+
/,
|
|
136
|
+
) -> Sequence[Var]: ...
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
ArgsSpec = (
|
|
140
|
+
_ArgsSpec0
|
|
141
|
+
| _ArgsSpec1
|
|
142
|
+
| _ArgsSpec2
|
|
143
|
+
| _ArgsSpec3
|
|
144
|
+
| _ArgsSpec4
|
|
145
|
+
| _ArgsSpec5
|
|
146
|
+
| _ArgsSpec6
|
|
147
|
+
| _ArgsSpec7
|
|
148
|
+
)
|
|
73
149
|
|
|
74
150
|
Scope = MutableMapping[str, Any]
|
|
75
151
|
Message = MutableMapping[str, Any]
|
|
@@ -85,11 +161,7 @@ PrimitiveToAnnotation = {
|
|
|
85
161
|
dict: Dict, # noqa: UP006
|
|
86
162
|
}
|
|
87
163
|
|
|
88
|
-
RESERVED_BACKEND_VAR_NAMES = {
|
|
89
|
-
"_abc_impl",
|
|
90
|
-
"_backend_vars",
|
|
91
|
-
"_was_touched",
|
|
92
|
-
}
|
|
164
|
+
RESERVED_BACKEND_VAR_NAMES = {"_abc_impl", "_backend_vars", "_was_touched", "_mixin"}
|
|
93
165
|
|
|
94
166
|
|
|
95
167
|
class Unset:
|
|
@@ -251,7 +323,11 @@ def is_optional(cls: GenericType) -> bool:
|
|
|
251
323
|
Returns:
|
|
252
324
|
Whether the class is an Optional.
|
|
253
325
|
"""
|
|
254
|
-
return
|
|
326
|
+
return (
|
|
327
|
+
cls is None
|
|
328
|
+
or cls is type(None)
|
|
329
|
+
or (is_union(cls) and type(None) in get_args(cls))
|
|
330
|
+
)
|
|
255
331
|
|
|
256
332
|
|
|
257
333
|
def is_classvar(a_type: Any) -> bool:
|
|
@@ -917,47 +993,6 @@ def validate_literal(key: str, value: Any, expected_type: type, comp_name: str):
|
|
|
917
993
|
raise ValueError(msg)
|
|
918
994
|
|
|
919
995
|
|
|
920
|
-
def validate_parameter_literals(func: Callable):
|
|
921
|
-
"""Decorator to check that the arguments passed to a function
|
|
922
|
-
correspond to the correct function parameter if it (the parameter)
|
|
923
|
-
is a literal type.
|
|
924
|
-
|
|
925
|
-
Args:
|
|
926
|
-
func: The function to validate.
|
|
927
|
-
|
|
928
|
-
Returns:
|
|
929
|
-
The wrapper function.
|
|
930
|
-
"""
|
|
931
|
-
console.deprecate(
|
|
932
|
-
"validate_parameter_literals",
|
|
933
|
-
reason="Use manual validation instead.",
|
|
934
|
-
deprecation_version="0.7.11",
|
|
935
|
-
removal_version="0.8.0",
|
|
936
|
-
dedupe=True,
|
|
937
|
-
)
|
|
938
|
-
|
|
939
|
-
func_params = list(inspect.signature(func).parameters.items())
|
|
940
|
-
annotations = {param[0]: param[1].annotation for param in func_params}
|
|
941
|
-
|
|
942
|
-
@wraps(func)
|
|
943
|
-
def wrapper(*args, **kwargs):
|
|
944
|
-
# validate args
|
|
945
|
-
for param, arg in zip(annotations, args, strict=False):
|
|
946
|
-
if annotations[param] is inspect.Parameter.empty:
|
|
947
|
-
continue
|
|
948
|
-
validate_literal(param, arg, annotations[param], func.__name__)
|
|
949
|
-
|
|
950
|
-
# validate kwargs.
|
|
951
|
-
for key, value in kwargs.items():
|
|
952
|
-
annotation = annotations.get(key)
|
|
953
|
-
if not annotation or annotation is inspect.Parameter.empty:
|
|
954
|
-
continue
|
|
955
|
-
validate_literal(key, value, annotation, func.__name__)
|
|
956
|
-
return func(*args, **kwargs)
|
|
957
|
-
|
|
958
|
-
return wrapper
|
|
959
|
-
|
|
960
|
-
|
|
961
996
|
# Store this here for performance.
|
|
962
997
|
StateBases = get_base_class(StateVar)
|
|
963
998
|
StateIterBases = get_base_class(StateIterVar)
|
|
@@ -1118,3 +1153,103 @@ def typehint_issubclass(
|
|
|
1118
1153
|
)
|
|
1119
1154
|
if accepted_arg is not Any
|
|
1120
1155
|
)
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
def resolve_annotations(
|
|
1159
|
+
raw_annotations: Mapping[str, type[Any]], module_name: str | None
|
|
1160
|
+
) -> dict[str, type[Any]]:
|
|
1161
|
+
"""Partially taken from typing.get_type_hints.
|
|
1162
|
+
|
|
1163
|
+
Resolve string or ForwardRef annotations into type objects if possible.
|
|
1164
|
+
|
|
1165
|
+
Args:
|
|
1166
|
+
raw_annotations: The raw annotations to resolve.
|
|
1167
|
+
module_name: The name of the module.
|
|
1168
|
+
|
|
1169
|
+
Returns:
|
|
1170
|
+
The resolved annotations.
|
|
1171
|
+
"""
|
|
1172
|
+
module = sys.modules.get(module_name, None) if module_name is not None else None
|
|
1173
|
+
|
|
1174
|
+
base_globals: dict[str, Any] | None = (
|
|
1175
|
+
module.__dict__ if module is not None else None
|
|
1176
|
+
)
|
|
1177
|
+
|
|
1178
|
+
annotations = {}
|
|
1179
|
+
for name, value in raw_annotations.items():
|
|
1180
|
+
if isinstance(value, str):
|
|
1181
|
+
if sys.version_info == (3, 10, 0):
|
|
1182
|
+
value = ForwardRef(value, is_argument=False)
|
|
1183
|
+
else:
|
|
1184
|
+
value = ForwardRef(value, is_argument=False, is_class=True)
|
|
1185
|
+
try:
|
|
1186
|
+
if sys.version_info >= (3, 13):
|
|
1187
|
+
value = _eval_type(value, base_globals, None, type_params=())
|
|
1188
|
+
else:
|
|
1189
|
+
value = _eval_type(value, base_globals, None)
|
|
1190
|
+
except NameError:
|
|
1191
|
+
# this is ok, it can be fixed with update_forward_refs
|
|
1192
|
+
pass
|
|
1193
|
+
annotations[name] = value
|
|
1194
|
+
return annotations
|
|
1195
|
+
|
|
1196
|
+
|
|
1197
|
+
TYPES_THAT_HAS_DEFAULT_VALUE = (int, float, tuple, list, set, dict, str)
|
|
1198
|
+
|
|
1199
|
+
|
|
1200
|
+
def get_default_value_for_type(t: GenericType) -> Any:
|
|
1201
|
+
"""Get the default value of the var.
|
|
1202
|
+
|
|
1203
|
+
Args:
|
|
1204
|
+
t: The type of the var.
|
|
1205
|
+
|
|
1206
|
+
Returns:
|
|
1207
|
+
The default value of the var, if it has one, else None.
|
|
1208
|
+
|
|
1209
|
+
Raises:
|
|
1210
|
+
ImportError: If the var is a dataframe and pandas is not installed.
|
|
1211
|
+
"""
|
|
1212
|
+
if is_optional(t):
|
|
1213
|
+
return None
|
|
1214
|
+
|
|
1215
|
+
origin = get_origin(t) if is_generic_alias(t) else t
|
|
1216
|
+
if origin is Literal:
|
|
1217
|
+
args = get_args(t)
|
|
1218
|
+
return args[0] if args else None
|
|
1219
|
+
if safe_issubclass(origin, TYPES_THAT_HAS_DEFAULT_VALUE):
|
|
1220
|
+
return origin()
|
|
1221
|
+
if safe_issubclass(origin, Mapping):
|
|
1222
|
+
return {}
|
|
1223
|
+
if is_dataframe(origin):
|
|
1224
|
+
try:
|
|
1225
|
+
import pandas as pd
|
|
1226
|
+
|
|
1227
|
+
return pd.DataFrame()
|
|
1228
|
+
except ImportError as e:
|
|
1229
|
+
msg = "Please install pandas to use dataframes in your app."
|
|
1230
|
+
raise ImportError(msg) from e
|
|
1231
|
+
return None
|
|
1232
|
+
|
|
1233
|
+
|
|
1234
|
+
IMMUTABLE_TYPES = (
|
|
1235
|
+
int,
|
|
1236
|
+
float,
|
|
1237
|
+
bool,
|
|
1238
|
+
str,
|
|
1239
|
+
bytes,
|
|
1240
|
+
frozenset,
|
|
1241
|
+
tuple,
|
|
1242
|
+
type(None),
|
|
1243
|
+
)
|
|
1244
|
+
|
|
1245
|
+
|
|
1246
|
+
def is_immutable(i: Any) -> bool:
|
|
1247
|
+
"""Check if a value is immutable.
|
|
1248
|
+
|
|
1249
|
+
Args:
|
|
1250
|
+
i: The value to check.
|
|
1251
|
+
|
|
1252
|
+
Returns:
|
|
1253
|
+
Whether the value is immutable.
|
|
1254
|
+
"""
|
|
1255
|
+
return isinstance(i, IMMUTABLE_TYPES)
|
reflex/vars/__init__.py
CHANGED