create generic variable for field

This commit is contained in:
Khaleel Al-Adhami 2025-01-07 16:55:15 -08:00
parent 5d099262f3
commit d82fac61cc

View File

@ -1462,7 +1462,7 @@ def get_python_literal(value: Union[LiteralVar, Any]) -> Any | None:
P = ParamSpec("P") P = ParamSpec("P")
T = TypeVar("T", covariant=True) T = TypeVar("T")
# NoReturn is used to match CustomVarOperationReturn with no type hint. # NoReturn is used to match CustomVarOperationReturn with no type hint.
@ -2928,11 +2928,14 @@ V = TypeVar("V")
BASE_TYPE = TypeVar("BASE_TYPE", bound=Base) BASE_TYPE = TypeVar("BASE_TYPE", bound=Base)
FIELD_TYPE = TypeVar("FIELD_TYPE")
MAPPING_TYPE = TypeVar("MAPPING_TYPE", bound=Mapping)
class Field(Generic[T]):
class Field(Generic[FIELD_TYPE]):
"""Shadow class for Var to allow for type hinting in the IDE.""" """Shadow class for Var to allow for type hinting in the IDE."""
def __set__(self, instance, value: T): # pyright: ignore[reportGeneralTypeIssues] def __set__(self, instance, value: FIELD_TYPE):
"""Set the Var. """Set the Var.
Args: Args:
@ -2944,7 +2947,9 @@ class Field(Generic[T]):
def __get__(self: Field[bool], instance: None, owner) -> BooleanVar: ... def __get__(self: Field[bool], instance: None, owner) -> BooleanVar: ...
@overload @overload
def __get__(self: Field[int], instance: None, owner) -> NumberVar: ... def __get__(
self: Field[int] | Field[float] | Field[int | float], instance: None, owner
) -> NumberVar: ...
@overload @overload
def __get__(self: Field[str], instance: None, owner) -> StringVar: ... def __get__(self: Field[str], instance: None, owner) -> StringVar: ...
@ -2961,8 +2966,8 @@ class Field(Generic[T]):
@overload @overload
def __get__( def __get__(
self: Field[Mapping[str, V]], instance: None, owner self: Field[MAPPING_TYPE], instance: None, owner
) -> ObjectVar[Mapping[str, V]]: ... ) -> ObjectVar[MAPPING_TYPE]: ...
@overload @overload
def __get__( def __get__(
@ -2970,10 +2975,10 @@ class Field(Generic[T]):
) -> ObjectVar[BASE_TYPE]: ... ) -> ObjectVar[BASE_TYPE]: ...
@overload @overload
def __get__(self, instance: None, owner) -> Var[T]: ... def __get__(self, instance: None, owner) -> Var[FIELD_TYPE]: ...
@overload @overload
def __get__(self, instance, owner) -> T: ... def __get__(self, instance, owner) -> FIELD_TYPE: ...
def __get__(self, instance, owner): # type: ignore def __get__(self, instance, owner): # type: ignore
"""Get the Var. """Get the Var.
@ -2984,7 +2989,7 @@ class Field(Generic[T]):
""" """
def field(value: T) -> Field[T]: def field(value: FIELD_TYPE) -> Field[FIELD_TYPE]:
"""Create a Field with a value. """Create a Field with a value.
Args: Args: