|
@@ -16,7 +16,7 @@ from itertools import chain
|
|
|
from multiprocessing import Pool, cpu_count
|
|
|
from pathlib import Path
|
|
|
from types import ModuleType, SimpleNamespace
|
|
|
-from typing import Any, Callable, Iterable, Sequence, Type, get_args, get_origin
|
|
|
+from typing import Any, Callable, Iterable, Sequence, Type, cast, get_args, get_origin
|
|
|
|
|
|
from reflex.components.component import Component
|
|
|
from reflex.utils import types as rx_types
|
|
@@ -229,7 +229,9 @@ def _generate_imports(
|
|
|
"""
|
|
|
return [
|
|
|
*[
|
|
|
- ast.ImportFrom(module=name, names=[ast.alias(name=val) for val in values])
|
|
|
+ ast.ImportFrom(
|
|
|
+ module=name, names=[ast.alias(name=val) for val in values], level=0
|
|
|
+ )
|
|
|
for name, values in DEFAULT_IMPORTS.items()
|
|
|
],
|
|
|
ast.Import([ast.alias("reflex")]),
|
|
@@ -428,16 +430,15 @@ def type_to_ast(typ, cls: type) -> ast.AST:
|
|
|
return ast.Name(id=base_name)
|
|
|
|
|
|
# Convert all type arguments recursively
|
|
|
- arg_nodes = [type_to_ast(arg, cls) for arg in args]
|
|
|
+ arg_nodes = cast(list[ast.expr], [type_to_ast(arg, cls) for arg in args])
|
|
|
|
|
|
# Special case for single-argument types (like List[T] or Optional[T])
|
|
|
if len(arg_nodes) == 1:
|
|
|
slice_value = arg_nodes[0]
|
|
|
else:
|
|
|
slice_value = ast.Tuple(elts=arg_nodes, ctx=ast.Load())
|
|
|
-
|
|
|
return ast.Subscript(
|
|
|
- value=ast.Name(id=base_name), slice=ast.Index(value=slice_value), ctx=ast.Load()
|
|
|
+ value=ast.Name(id=base_name), slice=slice_value, ctx=ast.Load()
|
|
|
)
|
|
|
|
|
|
|
|
@@ -630,7 +631,7 @@ def _generate_component_create_functiondef(
|
|
|
),
|
|
|
),
|
|
|
ast.Expr(
|
|
|
- value=ast.Ellipsis(),
|
|
|
+ value=ast.Constant(...),
|
|
|
),
|
|
|
],
|
|
|
decorator_list=[
|
|
@@ -641,8 +642,14 @@ def _generate_component_create_functiondef(
|
|
|
else [ast.Name(id="classmethod")]
|
|
|
),
|
|
|
],
|
|
|
- lineno=node.lineno if node is not None else None,
|
|
|
returns=ast.Constant(value=clz.__name__),
|
|
|
+ **(
|
|
|
+ {
|
|
|
+ "lineno": node.lineno,
|
|
|
+ }
|
|
|
+ if node is not None
|
|
|
+ else {}
|
|
|
+ ),
|
|
|
)
|
|
|
return definition
|
|
|
|
|
@@ -690,13 +697,19 @@ def _generate_staticmethod_call_functiondef(
|
|
|
),
|
|
|
],
|
|
|
decorator_list=[ast.Name(id="staticmethod")],
|
|
|
- lineno=node.lineno if node is not None else None,
|
|
|
returns=ast.Constant(
|
|
|
value=_get_type_hint(
|
|
|
typing.get_type_hints(clz.__call__).get("return", None),
|
|
|
type_hint_globals,
|
|
|
)
|
|
|
),
|
|
|
+ **(
|
|
|
+ {
|
|
|
+ "lineno": node.lineno,
|
|
|
+ }
|
|
|
+ if node is not None
|
|
|
+ else {}
|
|
|
+ ),
|
|
|
)
|
|
|
return definition
|
|
|
|
|
@@ -731,7 +744,12 @@ def _generate_namespace_call_functiondef(
|
|
|
# Determine which class is wrapped by the namespace __call__ method
|
|
|
component_clz = clz.__call__.__self__
|
|
|
|
|
|
- if clz.__call__.__func__.__name__ != "create":
|
|
|
+ func = getattr(clz.__call__, "__func__", None)
|
|
|
+
|
|
|
+ if func is None:
|
|
|
+ raise TypeError(f"__call__ method on {clz_name} does not have a __func__")
|
|
|
+
|
|
|
+ if func.__name__ != "create":
|
|
|
return None
|
|
|
|
|
|
definition = _generate_component_create_functiondef(
|
|
@@ -914,7 +932,7 @@ class StubGenerator(ast.NodeTransformer):
|
|
|
node.body.append(call_definition)
|
|
|
if not node.body:
|
|
|
# We should never return an empty body.
|
|
|
- node.body.append(ast.Expr(value=ast.Ellipsis()))
|
|
|
+ node.body.append(ast.Expr(value=ast.Constant(...)))
|
|
|
self.current_class = None
|
|
|
return node
|
|
|
|
|
@@ -941,9 +959,9 @@ class StubGenerator(ast.NodeTransformer):
|
|
|
if node.name.startswith("_") and node.name != "__call__":
|
|
|
return None # remove private methods
|
|
|
|
|
|
- if node.body[-1] != ast.Expr(value=ast.Ellipsis()):
|
|
|
+ if node.body[-1] != ast.Expr(value=ast.Constant(...)):
|
|
|
# Blank out the function body for public functions.
|
|
|
- node.body = [ast.Expr(value=ast.Ellipsis())]
|
|
|
+ node.body = [ast.Expr(value=ast.Constant(...))]
|
|
|
return node
|
|
|
|
|
|
def visit_Assign(self, node: ast.Assign) -> ast.Assign | None:
|