fix ObjectVar typing

This commit is contained in:
Benedikt Bartscher 2024-10-30 20:26:56 +01:00
parent 53e1c730fb
commit cd7214ae25
No known key found for this signature in database
3 changed files with 33 additions and 7 deletions

View File

@ -64,6 +64,7 @@ from reflex.utils.imports import (
parse_imports, parse_imports,
) )
from reflex.utils.types import GenericType, Self, get_origin, has_args, unionize from reflex.utils.types import GenericType, Self, get_origin, has_args, unionize
from reflex.vars.object import LiteralObjectVar
if TYPE_CHECKING: if TYPE_CHECKING:
from reflex.state import BaseState from reflex.state import BaseState
@ -579,10 +580,35 @@ class Var(Generic[VAR_TYPE]):
var_type: type[list] | type[tuple] | type[set] = list, var_type: type[list] | type[tuple] | type[set] = list,
) -> ArrayVar: ... ) -> ArrayVar: ...
VAR_TYPE_ARG = TypeVar("VAR_TYPE_ARG")
@overload @overload
def to( def to(
self, output: Type[ObjectVar], var_type: types.GenericType = dict self,
) -> ObjectVar: ... output: Type[LiteralObjectVar[Any]],
var_type: None = None,
) -> LiteralObjectVar[VAR_TYPE]: ...
@overload
def to(
self,
output: Type[LiteralObjectVar[Any]],
var_type: Type[VAR_TYPE_ARG],
) -> LiteralObjectVar[VAR_TYPE_ARG]: ...
@overload
def to(
self,
output: Type[ObjectVar[Any]],
var_type: None = None,
) -> ObjectVar[VAR_TYPE]: ...
@overload
def to(
self,
output: Type[ObjectVar[Any]],
var_type: Type[VAR_TYPE_ARG],
) -> ObjectVar[VAR_TYPE_ARG]: ...
@overload @overload
def to( def to(

View File

@ -36,7 +36,7 @@ from .base import (
from .number import BooleanVar, NumberVar, raise_unsupported_operand_types from .number import BooleanVar, NumberVar, raise_unsupported_operand_types
from .sequence import ArrayVar, StringVar from .sequence import ArrayVar, StringVar
OBJECT_TYPE = TypeVar("OBJECT_TYPE", bound=Dict) OBJECT_TYPE = TypeVar("OBJECT_TYPE")
KEY_TYPE = TypeVar("KEY_TYPE") KEY_TYPE = TypeVar("KEY_TYPE")
VALUE_TYPE = TypeVar("VALUE_TYPE") VALUE_TYPE = TypeVar("VALUE_TYPE")

View File

@ -94,17 +94,17 @@ def test_typing() -> None:
# Bare # Bare
var = ObjectState.bare.to(ObjectVar) var = ObjectState.bare.to(ObjectVar)
reveal_type(var) reveal_type(var)
assert_type(var, ObjectVar[Bare]) _ = assert_type(var, ObjectVar[Bare])
var = ObjectState.base.to(ObjectVar, Base) var = ObjectState.base.to(ObjectVar, Base)
reveal_type(var) reveal_type(var)
assert_type(var, ObjectVar[Base]) _ = assert_type(var, ObjectVar[Base])
# Base # Base
var = ObjectState.base.to(ObjectVar) var = ObjectState.base.to(ObjectVar)
reveal_type(var) reveal_type(var)
assert_type(var, ObjectVar[Base]) _ = assert_type(var, ObjectVar[Base])
var = ObjectState.base.to(LiteralObjectVar, Base) var = ObjectState.base.to(LiteralObjectVar, Base)
reveal_type(var) reveal_type(var)
assert_type(var, LiteralObjectVar[Base]) _ = assert_type(var, LiteralObjectVar[Base])