Skip to content

Commit 346e848

Browse files
authored
make pyright happier (#5006)
* make pyright happier * fix more stuff why not * make typing think that LiteralVar.create is the same as Var.create * assert stuff * you cannot chain classmethods in 3.13 * change pyi generator a good bunch, but no changes in output thankfully * more cleanup * ok one more
1 parent 553da33 commit 346e848

File tree

172 files changed

+2719
-921
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

172 files changed

+2719
-921
lines changed

reflex/app.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@
114114
if TYPE_CHECKING:
115115
from reflex.vars import Var
116116

117+
# Define custom types.
118+
ComponentCallable = Callable[[], Component | tuple[Component, ...] | str | Var]
119+
else:
120+
ComponentCallable = Callable[[], Component | tuple[Component, ...] | str]
117121

118-
# Define custom types.
119-
ComponentCallable = Callable[[], Component]
120122
Reducer = Callable[[Event], Coroutine[Any, Any, StateUpdate]]
121123

122124

@@ -1224,13 +1226,15 @@ def memoized_toast_provider():
12241226
custom_components |= component._get_all_custom_components()
12251227

12261228
if self.error_boundary:
1229+
from reflex.compiler.compiler import into_component
1230+
12271231
console.deprecate(
12281232
feature_name="App.error_boundary",
12291233
reason="Use app_wraps instead.",
12301234
deprecation_version="0.7.1",
12311235
removal_version="0.8.0",
12321236
)
1233-
app_wrappers[(55, "ErrorBoundary")] = self.error_boundary()
1237+
app_wrappers[(55, "ErrorBoundary")] = into_component(self.error_boundary)
12341238

12351239
# Perform auto-memoization of stateful components.
12361240
with console.timing("Auto-memoize StatefulComponents"):

reflex/app_mixins/middleware.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from __future__ import annotations
44

5-
import asyncio
65
import dataclasses
6+
import inspect
77

88
from reflex.event import Event
99
from reflex.middleware import HydrateMiddleware, Middleware
@@ -51,12 +51,11 @@ async def _preprocess(self, state: BaseState, event: Event) -> StateUpdate | Non
5151
An optional state to return.
5252
"""
5353
for middleware in self._middlewares:
54-
if asyncio.iscoroutinefunction(middleware.preprocess):
55-
out = await middleware.preprocess(app=self, state=state, event=event) # pyright: ignore [reportArgumentType]
56-
else:
57-
out = middleware.preprocess(app=self, state=state, event=event) # pyright: ignore [reportArgumentType]
54+
out = middleware.preprocess(app=self, state=state, event=event) # pyright: ignore [reportArgumentType]
55+
if inspect.isawaitable(out):
56+
out = await out
5857
if out is not None:
59-
return out # pyright: ignore [reportReturnType]
58+
return out
6059

6160
async def _postprocess(
6261
self, state: BaseState, event: Event, update: StateUpdate
@@ -76,18 +75,12 @@ async def _postprocess(
7675
"""
7776
out = update
7877
for middleware in self._middlewares:
79-
if asyncio.iscoroutinefunction(middleware.postprocess):
80-
out = await middleware.postprocess(
81-
app=self, # pyright: ignore [reportArgumentType]
82-
state=state,
83-
event=event,
84-
update=update,
85-
)
86-
else:
87-
out = middleware.postprocess(
88-
app=self, # pyright: ignore [reportArgumentType]
89-
state=state,
90-
event=event,
91-
update=update,
92-
)
78+
out = middleware.postprocess(
79+
app=self, # pyright: ignore [reportArgumentType]
80+
state=state,
81+
event=event,
82+
update=update,
83+
)
84+
if inspect.isawaitable(out):
85+
out = await out
9386
return out # pyright: ignore[reportReturnType]

reflex/compiler/compiler.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,15 @@ def purge_web_pages_dir():
610610
from reflex.app import ComponentCallable, UnevaluatedPage
611611

612612

613-
def _into_component_once(component: Component | ComponentCallable) -> Component | None:
613+
def _into_component_once(
614+
component: Component
615+
| ComponentCallable
616+
| tuple[Component, ...]
617+
| str
618+
| Var
619+
| int
620+
| float,
621+
) -> Component | None:
614622
"""Convert a component to a Component.
615623
616624
Args:

reflex/compiler/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import traceback
88
from datetime import datetime
99
from pathlib import Path
10-
from typing import Any, Callable, Type
10+
from typing import Any, Sequence, Type
1111
from urllib.parse import urlparse
1212

1313
from pydantic.v1.fields import ModelField
@@ -335,7 +335,7 @@ def compile_custom_component(
335335

336336

337337
def create_document_root(
338-
head_components: list[Component] | None = None,
338+
head_components: Sequence[Component] | None = None,
339339
html_lang: str | None = None,
340340
html_custom_attrs: dict[str, Var | str] | None = None,
341341
) -> Component:
@@ -371,7 +371,7 @@ def create_theme(style: ComponentStyle) -> dict:
371371
The base style for the app.
372372
"""
373373
# Get the global style from the style dict.
374-
style_rules = Style({k: v for k, v in style.items() if not isinstance(k, Callable)})
374+
style_rules = Style({k: v for k, v in style.items() if isinstance(k, str)})
375375

376376
root_style = {
377377
# Root styles.

reflex/components/base/app_wrap.pyi

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
# ------------------- DO NOT EDIT ----------------------
44
# This file was generated by `reflex/utils/pyi_generator.py`!
55
# ------------------------------------------------------
6-
from typing import Any, Optional, overload
6+
from typing import Any, Mapping, Optional, Sequence, overload
77

88
from reflex.components.base.fragment import Fragment
9+
from reflex.components.core.breakpoints import Breakpoints
910
from reflex.event import EventType
10-
from reflex.style import Style
1111
from reflex.vars.base import Var
1212

1313
class AppWrap(Fragment):
@@ -16,7 +16,11 @@ class AppWrap(Fragment):
1616
def create( # type: ignore
1717
cls,
1818
*children,
19-
style: Style | None = None,
19+
style: Sequence[Mapping[str, Any]]
20+
| Mapping[str, Any]
21+
| Var[Mapping[str, Any]]
22+
| Breakpoints
23+
| None = None,
2024
key: Any | None = None,
2125
id: Any | None = None,
2226
class_name: Any | None = None,

reflex/components/base/body.pyi

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
# ------------------- DO NOT EDIT ----------------------
44
# This file was generated by `reflex/utils/pyi_generator.py`!
55
# ------------------------------------------------------
6-
from typing import Any, Optional, overload
6+
from typing import Any, Mapping, Optional, Sequence, overload
77

88
from reflex.components.component import Component
9+
from reflex.components.core.breakpoints import Breakpoints
910
from reflex.event import EventType
10-
from reflex.style import Style
1111
from reflex.vars.base import Var
1212

1313
class Body(Component):
@@ -16,7 +16,11 @@ class Body(Component):
1616
def create( # type: ignore
1717
cls,
1818
*children,
19-
style: Style | None = None,
19+
style: Sequence[Mapping[str, Any]]
20+
| Mapping[str, Any]
21+
| Var[Mapping[str, Any]]
22+
| Breakpoints
23+
| None = None,
2024
key: Any | None = None,
2125
id: Any | None = None,
2226
class_name: Any | None = None,

reflex/components/base/document.pyi

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
# ------------------- DO NOT EDIT ----------------------
44
# This file was generated by `reflex/utils/pyi_generator.py`!
55
# ------------------------------------------------------
6-
from typing import Any, Optional, overload
6+
from typing import Any, Mapping, Optional, Sequence, overload
77

88
from reflex.components.component import Component
9+
from reflex.components.core.breakpoints import Breakpoints
910
from reflex.event import EventType
10-
from reflex.style import Style
1111
from reflex.vars.base import Var
1212

1313
class NextDocumentLib(Component):
@@ -16,7 +16,11 @@ class NextDocumentLib(Component):
1616
def create( # type: ignore
1717
cls,
1818
*children,
19-
style: Style | None = None,
19+
style: Sequence[Mapping[str, Any]]
20+
| Mapping[str, Any]
21+
| Var[Mapping[str, Any]]
22+
| Breakpoints
23+
| None = None,
2024
key: Any | None = None,
2125
id: Any | None = None,
2226
class_name: Any | None = None,
@@ -63,7 +67,11 @@ class Html(NextDocumentLib):
6367
cls,
6468
*children,
6569
lang: str | None = None,
66-
style: Style | None = None,
70+
style: Sequence[Mapping[str, Any]]
71+
| Mapping[str, Any]
72+
| Var[Mapping[str, Any]]
73+
| Breakpoints
74+
| None = None,
6775
key: Any | None = None,
6876
id: Any | None = None,
6977
class_name: Any | None = None,
@@ -109,7 +117,11 @@ class DocumentHead(NextDocumentLib):
109117
def create( # type: ignore
110118
cls,
111119
*children,
112-
style: Style | None = None,
120+
style: Sequence[Mapping[str, Any]]
121+
| Mapping[str, Any]
122+
| Var[Mapping[str, Any]]
123+
| Breakpoints
124+
| None = None,
113125
key: Any | None = None,
114126
id: Any | None = None,
115127
class_name: Any | None = None,
@@ -155,7 +167,11 @@ class Main(NextDocumentLib):
155167
def create( # type: ignore
156168
cls,
157169
*children,
158-
style: Style | None = None,
170+
style: Sequence[Mapping[str, Any]]
171+
| Mapping[str, Any]
172+
| Var[Mapping[str, Any]]
173+
| Breakpoints
174+
| None = None,
159175
key: Any | None = None,
160176
id: Any | None = None,
161177
class_name: Any | None = None,
@@ -201,7 +217,11 @@ class NextScript(NextDocumentLib):
201217
def create( # type: ignore
202218
cls,
203219
*children,
204-
style: Style | None = None,
220+
style: Sequence[Mapping[str, Any]]
221+
| Mapping[str, Any]
222+
| Var[Mapping[str, Any]]
223+
| Breakpoints
224+
| None = None,
205225
key: Any | None = None,
206226
id: Any | None = None,
207227
class_name: Any | None = None,

reflex/components/base/error_boundary.pyi

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
# ------------------- DO NOT EDIT ----------------------
44
# This file was generated by `reflex/utils/pyi_generator.py`!
55
# ------------------------------------------------------
6-
from typing import Any, Optional, overload
6+
from typing import Any, Mapping, Optional, Sequence, overload
77

88
from reflex.components.component import Component
9+
from reflex.components.core.breakpoints import Breakpoints
910
from reflex.event import EventType
10-
from reflex.style import Style
1111
from reflex.vars.base import Var
1212
from reflex.vars.object import ObjectVar
1313

@@ -22,7 +22,11 @@ class ErrorBoundary(Component):
2222
cls,
2323
*children,
2424
fallback_render: Component | Var[Component] | None = None,
25-
style: Style | None = None,
25+
style: Sequence[Mapping[str, Any]]
26+
| Mapping[str, Any]
27+
| Var[Mapping[str, Any]]
28+
| Breakpoints
29+
| None = None,
2630
key: Any | None = None,
2731
id: Any | None = None,
2832
class_name: Any | None = None,

reflex/components/base/fragment.pyi

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
# ------------------- DO NOT EDIT ----------------------
44
# This file was generated by `reflex/utils/pyi_generator.py`!
55
# ------------------------------------------------------
6-
from typing import Any, Optional, overload
6+
from typing import Any, Mapping, Optional, Sequence, overload
77

88
from reflex.components.component import Component
9+
from reflex.components.core.breakpoints import Breakpoints
910
from reflex.event import EventType
10-
from reflex.style import Style
1111
from reflex.vars.base import Var
1212

1313
class Fragment(Component):
@@ -16,7 +16,11 @@ class Fragment(Component):
1616
def create( # type: ignore
1717
cls,
1818
*children,
19-
style: Style | None = None,
19+
style: Sequence[Mapping[str, Any]]
20+
| Mapping[str, Any]
21+
| Var[Mapping[str, Any]]
22+
| Breakpoints
23+
| None = None,
2024
key: Any | None = None,
2125
id: Any | None = None,
2226
class_name: Any | None = None,

reflex/components/base/head.pyi

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
# ------------------- DO NOT EDIT ----------------------
44
# This file was generated by `reflex/utils/pyi_generator.py`!
55
# ------------------------------------------------------
6-
from typing import Any, Optional, overload
6+
from typing import Any, Mapping, Optional, Sequence, overload
77

88
from reflex.components.component import Component, MemoizationLeaf
9+
from reflex.components.core.breakpoints import Breakpoints
910
from reflex.event import EventType
10-
from reflex.style import Style
1111
from reflex.vars.base import Var
1212

1313
class NextHeadLib(Component):
@@ -16,7 +16,11 @@ class NextHeadLib(Component):
1616
def create( # type: ignore
1717
cls,
1818
*children,
19-
style: Style | None = None,
19+
style: Sequence[Mapping[str, Any]]
20+
| Mapping[str, Any]
21+
| Var[Mapping[str, Any]]
22+
| Breakpoints
23+
| None = None,
2024
key: Any | None = None,
2125
id: Any | None = None,
2226
class_name: Any | None = None,
@@ -62,7 +66,11 @@ class Head(NextHeadLib, MemoizationLeaf):
6266
def create( # type: ignore
6367
cls,
6468
*children,
65-
style: Style | None = None,
69+
style: Sequence[Mapping[str, Any]]
70+
| Mapping[str, Any]
71+
| Var[Mapping[str, Any]]
72+
| Breakpoints
73+
| None = None,
6674
key: Any | None = None,
6775
id: Any | None = None,
6876
class_name: Any | None = None,

0 commit comments

Comments
 (0)