types.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. """Contains custom types and methods to check types."""
  2. from __future__ import annotations
  3. import contextlib
  4. import inspect
  5. import sys
  6. import types
  7. from functools import wraps
  8. from typing import (
  9. TYPE_CHECKING,
  10. Any,
  11. Callable,
  12. Dict,
  13. Iterable,
  14. List,
  15. Literal,
  16. Optional,
  17. Tuple,
  18. Type,
  19. Union,
  20. _GenericAlias, # type: ignore
  21. get_args,
  22. get_origin,
  23. get_type_hints,
  24. )
  25. import sqlalchemy
  26. try:
  27. # TODO The type checking guard can be removed once
  28. # reflex-hosting-cli tools are compatible with pydantic v2
  29. if not TYPE_CHECKING:
  30. from pydantic.v1.fields import ModelField
  31. else:
  32. raise ModuleNotFoundError
  33. except ModuleNotFoundError:
  34. from pydantic.fields import ModelField
  35. from sqlalchemy.ext.associationproxy import AssociationProxyInstance
  36. from sqlalchemy.ext.hybrid import hybrid_property
  37. from sqlalchemy.orm import (
  38. DeclarativeBase,
  39. Mapped,
  40. QueryableAttribute,
  41. Relationship,
  42. )
  43. from reflex import constants
  44. from reflex.base import Base
  45. from reflex.utils import console, serializers
  46. # Potential GenericAlias types for isinstance checks.
  47. GenericAliasTypes = [_GenericAlias]
  48. with contextlib.suppress(ImportError):
  49. # For newer versions of Python.
  50. from types import GenericAlias # type: ignore
  51. GenericAliasTypes.append(GenericAlias)
  52. with contextlib.suppress(ImportError):
  53. # For older versions of Python.
  54. from typing import _SpecialGenericAlias # type: ignore
  55. GenericAliasTypes.append(_SpecialGenericAlias)
  56. GenericAliasTypes = tuple(GenericAliasTypes)
  57. # Potential Union types for isinstance checks (UnionType added in py3.10).
  58. UnionTypes = (Union, types.UnionType) if hasattr(types, "UnionType") else (Union,)
  59. # Union of generic types.
  60. GenericType = Union[Type, _GenericAlias]
  61. # Valid state var types.
  62. JSONType = {str, int, float, bool}
  63. PrimitiveType = Union[int, float, bool, str, list, dict, set, tuple]
  64. StateVar = Union[PrimitiveType, Base, None]
  65. StateIterVar = Union[list, set, tuple]
  66. # ArgsSpec = Callable[[Var], list[Var]]
  67. ArgsSpec = Callable
  68. PrimitiveToAnnotation = {
  69. list: List,
  70. tuple: Tuple,
  71. dict: Dict,
  72. }
  73. class Unset:
  74. """A class to represent an unset value.
  75. This is used to differentiate between a value that is not set and a value that is set to None.
  76. """
  77. def __repr__(self) -> str:
  78. """Return the string representation of the class.
  79. Returns:
  80. The string representation of the class.
  81. """
  82. return "Unset"
  83. def __bool__(self) -> bool:
  84. """Return False when the class is used in a boolean context.
  85. Returns:
  86. False
  87. """
  88. return False
  89. def is_generic_alias(cls: GenericType) -> bool:
  90. """Check whether the class is a generic alias.
  91. Args:
  92. cls: The class to check.
  93. Returns:
  94. Whether the class is a generic alias.
  95. """
  96. return isinstance(cls, GenericAliasTypes)
  97. def is_union(cls: GenericType) -> bool:
  98. """Check if a class is a Union.
  99. Args:
  100. cls: The class to check.
  101. Returns:
  102. Whether the class is a Union.
  103. """
  104. return get_origin(cls) in UnionTypes
  105. def is_literal(cls: GenericType) -> bool:
  106. """Check if a class is a Literal.
  107. Args:
  108. cls: The class to check.
  109. Returns:
  110. Whether the class is a literal.
  111. """
  112. return get_origin(cls) is Literal
  113. def is_optional(cls: GenericType) -> bool:
  114. """Check if a class is an Optional.
  115. Args:
  116. cls: The class to check.
  117. Returns:
  118. Whether the class is an Optional.
  119. """
  120. return is_union(cls) and type(None) in get_args(cls)
  121. def get_property_hint(attr: Any | None) -> GenericType | None:
  122. """Check if an attribute is a property and return its type hint.
  123. Args:
  124. attr: The descriptor to check.
  125. Returns:
  126. The type hint of the property, if it is a property, else None.
  127. """
  128. if not isinstance(attr, (property, hybrid_property)):
  129. return None
  130. hints = get_type_hints(attr.fget)
  131. return hints.get("return", None)
  132. def get_attribute_access_type(cls: GenericType, name: str) -> GenericType | None:
  133. """Check if an attribute can be accessed on the cls and return its type.
  134. Supports pydantic models, unions, and annotated attributes on rx.Model.
  135. Args:
  136. cls: The class to check.
  137. name: The name of the attribute to check.
  138. Returns:
  139. The type of the attribute, if accessible, or None
  140. """
  141. from reflex.model import Model
  142. attr = getattr(cls, name, None)
  143. if hint := get_property_hint(attr):
  144. return hint
  145. if hasattr(cls, "__fields__") and name in cls.__fields__:
  146. # pydantic models
  147. field = cls.__fields__[name]
  148. type_ = field.outer_type_
  149. if isinstance(type_, ModelField):
  150. type_ = type_.type_
  151. if not field.required and field.default is None:
  152. # Ensure frontend uses null coalescing when accessing.
  153. type_ = Optional[type_]
  154. return type_
  155. elif isinstance(cls, type) and issubclass(cls, DeclarativeBase):
  156. insp = sqlalchemy.inspect(cls)
  157. if name in insp.columns:
  158. # check for list types
  159. column = insp.columns[name]
  160. column_type = column.type
  161. type_ = insp.columns[name].type.python_type
  162. if hasattr(column_type, "item_type") and (
  163. item_type := column_type.item_type.python_type # type: ignore
  164. ):
  165. if type_ in PrimitiveToAnnotation:
  166. type_ = PrimitiveToAnnotation[type_] # type: ignore
  167. type_ = type_[item_type] # type: ignore
  168. if column.nullable:
  169. type_ = Optional[type_]
  170. return type_
  171. if name not in insp.all_orm_descriptors:
  172. return None
  173. descriptor = insp.all_orm_descriptors[name]
  174. if hint := get_property_hint(descriptor):
  175. return hint
  176. if isinstance(descriptor, QueryableAttribute):
  177. prop = descriptor.property
  178. if not isinstance(prop, Relationship):
  179. return None
  180. type_ = prop.mapper.class_
  181. # TODO: check for nullable?
  182. type_ = List[type_] if prop.uselist else Optional[type_]
  183. return type_
  184. if isinstance(attr, AssociationProxyInstance):
  185. return List[
  186. get_attribute_access_type(
  187. attr.target_class,
  188. attr.remote_attr.key, # type: ignore[attr-defined]
  189. )
  190. ]
  191. elif isinstance(cls, type) and not is_generic_alias(cls) and issubclass(cls, Model):
  192. # Check in the annotations directly (for sqlmodel.Relationship)
  193. hints = get_type_hints(cls)
  194. if name in hints:
  195. type_ = hints[name]
  196. type_origin = get_origin(type_)
  197. if isinstance(type_origin, type) and issubclass(type_origin, Mapped):
  198. return get_args(type_)[0] # SQLAlchemy v2
  199. if isinstance(type_, ModelField):
  200. return type_.type_ # SQLAlchemy v1.4
  201. return type_
  202. elif is_union(cls):
  203. # Check in each arg of the annotation.
  204. for arg in get_args(cls):
  205. type_ = get_attribute_access_type(arg, name)
  206. if type_ is not None:
  207. # Return the first attribute type that is accessible.
  208. return type_
  209. elif isinstance(cls, type):
  210. # Bare class
  211. if sys.version_info >= (3, 10):
  212. exceptions = NameError
  213. else:
  214. exceptions = (NameError, TypeError)
  215. try:
  216. hints = get_type_hints(cls)
  217. if name in hints:
  218. return hints[name]
  219. except exceptions as e:
  220. console.warn(f"Failed to resolve ForwardRefs for {cls}.{name} due to {e}")
  221. pass
  222. return None # Attribute is not accessible.
  223. def get_base_class(cls: GenericType) -> Type:
  224. """Get the base class of a class.
  225. Args:
  226. cls: The class.
  227. Returns:
  228. The base class of the class.
  229. Raises:
  230. TypeError: If a literal has multiple types.
  231. """
  232. if is_literal(cls):
  233. # only literals of the same type are supported.
  234. arg_type = type(get_args(cls)[0])
  235. if not all(type(arg) == arg_type for arg in get_args(cls)):
  236. raise TypeError("only literals of the same type are supported")
  237. return type(get_args(cls)[0])
  238. if is_union(cls):
  239. return tuple(get_base_class(arg) for arg in get_args(cls))
  240. return get_base_class(cls.__origin__) if is_generic_alias(cls) else cls
  241. def _issubclass(cls: GenericType, cls_check: GenericType) -> bool:
  242. """Check if a class is a subclass of another class.
  243. Args:
  244. cls: The class to check.
  245. cls_check: The class to check against.
  246. Returns:
  247. Whether the class is a subclass of the other class.
  248. Raises:
  249. TypeError: If the base class is not valid for issubclass.
  250. """
  251. # Special check for Any.
  252. if cls_check == Any:
  253. return True
  254. if cls in [Any, Callable, None]:
  255. return False
  256. # Get the base classes.
  257. cls_base = get_base_class(cls)
  258. cls_check_base = get_base_class(cls_check)
  259. # The class we're checking should not be a union.
  260. if isinstance(cls_base, tuple):
  261. return False
  262. # Check if the types match.
  263. try:
  264. return cls_check_base == Any or issubclass(cls_base, cls_check_base)
  265. except TypeError as te:
  266. # These errors typically arise from bad annotations and are hard to
  267. # debug without knowing the type that we tried to compare.
  268. raise TypeError(f"Invalid type for issubclass: {cls_base}") from te
  269. def _isinstance(obj: Any, cls: GenericType) -> bool:
  270. """Check if an object is an instance of a class.
  271. Args:
  272. obj: The object to check.
  273. cls: The class to check against.
  274. Returns:
  275. Whether the object is an instance of the class.
  276. """
  277. return isinstance(obj, get_base_class(cls))
  278. def is_dataframe(value: Type) -> bool:
  279. """Check if the given value is a dataframe.
  280. Args:
  281. value: The value to check.
  282. Returns:
  283. Whether the value is a dataframe.
  284. """
  285. if is_generic_alias(value) or value == Any:
  286. return False
  287. return value.__name__ == "DataFrame"
  288. def is_valid_var_type(type_: Type) -> bool:
  289. """Check if the given type is a valid prop type.
  290. Args:
  291. type_: The type to check.
  292. Returns:
  293. Whether the type is a valid prop type.
  294. """
  295. if is_union(type_):
  296. return all((is_valid_var_type(arg) for arg in get_args(type_)))
  297. return _issubclass(type_, StateVar) or serializers.has_serializer(type_)
  298. def is_backend_variable(name: str, cls: Type | None = None) -> bool:
  299. """Check if this variable name correspond to a backend variable.
  300. Args:
  301. name: The name of the variable to check
  302. cls: The class of the variable to check
  303. Returns:
  304. bool: The result of the check
  305. """
  306. if cls is not None and name.startswith(f"_{cls.__name__}__"):
  307. return False
  308. return name.startswith("_") and not name.startswith("__")
  309. def check_type_in_allowed_types(value_type: Type, allowed_types: Iterable) -> bool:
  310. """Check that a value type is found in a list of allowed types.
  311. Args:
  312. value_type: Type of value.
  313. allowed_types: Iterable of allowed types.
  314. Returns:
  315. If the type is found in the allowed types.
  316. """
  317. return get_base_class(value_type) in allowed_types
  318. def check_prop_in_allowed_types(prop: Any, allowed_types: Iterable) -> bool:
  319. """Check that a prop value is in a list of allowed types.
  320. Does the check in a way that works regardless if it's a raw value or a state Var.
  321. Args:
  322. prop: The prop to check.
  323. allowed_types: The list of allowed types.
  324. Returns:
  325. If the prop type match one of the allowed_types.
  326. """
  327. from reflex.vars import Var
  328. type_ = prop._var_type if _isinstance(prop, Var) else type(prop)
  329. return type_ in allowed_types
  330. def is_encoded_fstring(value) -> bool:
  331. """Check if a value is an encoded Var f-string.
  332. Args:
  333. value: The value string to check.
  334. Returns:
  335. Whether the value is an f-string
  336. """
  337. return isinstance(value, str) and constants.REFLEX_VAR_OPENING_TAG in value
  338. def validate_literal(key: str, value: Any, expected_type: Type, comp_name: str):
  339. """Check that a value is a valid literal.
  340. Args:
  341. key: The prop name.
  342. value: The prop value to validate.
  343. expected_type: The expected type(literal type).
  344. comp_name: Name of the component.
  345. Raises:
  346. ValueError: When the value is not a valid literal.
  347. """
  348. from reflex.vars import Var
  349. if (
  350. is_literal(expected_type)
  351. and not isinstance(value, Var) # validating vars is not supported yet.
  352. and not is_encoded_fstring(value) # f-strings are not supported.
  353. and value not in expected_type.__args__
  354. ):
  355. allowed_values = expected_type.__args__
  356. if value not in allowed_values:
  357. allowed_value_str = ",".join(
  358. [str(v) if not isinstance(v, str) else f"'{v}'" for v in allowed_values]
  359. )
  360. value_str = f"'{value}'" if isinstance(value, str) else value
  361. raise ValueError(
  362. f"prop value for {str(key)} of the `{comp_name}` component should be one of the following: {allowed_value_str}. Got {value_str} instead"
  363. )
  364. def validate_parameter_literals(func):
  365. """Decorator to check that the arguments passed to a function
  366. correspond to the correct function parameter if it (the parameter)
  367. is a literal type.
  368. Args:
  369. func: The function to validate.
  370. Returns:
  371. The wrapper function.
  372. """
  373. @wraps(func)
  374. def wrapper(*args, **kwargs):
  375. func_params = list(inspect.signature(func).parameters.items())
  376. annotations = {param[0]: param[1].annotation for param in func_params}
  377. # validate args
  378. for param, arg in zip(annotations.keys(), args):
  379. if annotations[param] is inspect.Parameter.empty:
  380. continue
  381. validate_literal(param, arg, annotations[param], func.__name__)
  382. # validate kwargs.
  383. for key, value in kwargs.items():
  384. annotation = annotations.get(key)
  385. if not annotation or annotation is inspect.Parameter.empty:
  386. continue
  387. validate_literal(key, value, annotation, func.__name__)
  388. return func(*args, **kwargs)
  389. return wrapper
  390. # Store this here for performance.
  391. StateBases = get_base_class(StateVar)
  392. StateIterBases = get_base_class(StateIterVar)