types.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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_none(cls: GenericType) -> bool:
  98. """Check if a class is None.
  99. Args:
  100. cls: The class to check.
  101. Returns:
  102. Whether the class is None.
  103. """
  104. return cls is type(None) or cls is None
  105. def is_union(cls: GenericType) -> bool:
  106. """Check if a class is a Union.
  107. Args:
  108. cls: The class to check.
  109. Returns:
  110. Whether the class is a Union.
  111. """
  112. return get_origin(cls) in UnionTypes
  113. def is_literal(cls: GenericType) -> bool:
  114. """Check if a class is a Literal.
  115. Args:
  116. cls: The class to check.
  117. Returns:
  118. Whether the class is a literal.
  119. """
  120. return get_origin(cls) is Literal
  121. def is_optional(cls: GenericType) -> bool:
  122. """Check if a class is an Optional.
  123. Args:
  124. cls: The class to check.
  125. Returns:
  126. Whether the class is an Optional.
  127. """
  128. return is_union(cls) and type(None) in get_args(cls)
  129. def get_property_hint(attr: Any | None) -> GenericType | None:
  130. """Check if an attribute is a property and return its type hint.
  131. Args:
  132. attr: The descriptor to check.
  133. Returns:
  134. The type hint of the property, if it is a property, else None.
  135. """
  136. if not isinstance(attr, (property, hybrid_property)):
  137. return None
  138. hints = get_type_hints(attr.fget)
  139. return hints.get("return", None)
  140. def get_attribute_access_type(cls: GenericType, name: str) -> GenericType | None:
  141. """Check if an attribute can be accessed on the cls and return its type.
  142. Supports pydantic models, unions, and annotated attributes on rx.Model.
  143. Args:
  144. cls: The class to check.
  145. name: The name of the attribute to check.
  146. Returns:
  147. The type of the attribute, if accessible, or None
  148. """
  149. from reflex.model import Model
  150. attr = getattr(cls, name, None)
  151. if hint := get_property_hint(attr):
  152. return hint
  153. if hasattr(cls, "__fields__") and name in cls.__fields__:
  154. # pydantic models
  155. field = cls.__fields__[name]
  156. type_ = field.outer_type_
  157. if isinstance(type_, ModelField):
  158. type_ = type_.type_
  159. if not field.required and field.default is None:
  160. # Ensure frontend uses null coalescing when accessing.
  161. type_ = Optional[type_]
  162. return type_
  163. elif isinstance(cls, type) and issubclass(cls, DeclarativeBase):
  164. insp = sqlalchemy.inspect(cls)
  165. if name in insp.columns:
  166. # check for list types
  167. column = insp.columns[name]
  168. column_type = column.type
  169. type_ = insp.columns[name].type.python_type
  170. if hasattr(column_type, "item_type") and (
  171. item_type := column_type.item_type.python_type # type: ignore
  172. ):
  173. if type_ in PrimitiveToAnnotation:
  174. type_ = PrimitiveToAnnotation[type_] # type: ignore
  175. type_ = type_[item_type] # type: ignore
  176. if column.nullable:
  177. type_ = Optional[type_]
  178. return type_
  179. if name not in insp.all_orm_descriptors:
  180. return None
  181. descriptor = insp.all_orm_descriptors[name]
  182. if hint := get_property_hint(descriptor):
  183. return hint
  184. if isinstance(descriptor, QueryableAttribute):
  185. prop = descriptor.property
  186. if not isinstance(prop, Relationship):
  187. return None
  188. type_ = prop.mapper.class_
  189. # TODO: check for nullable?
  190. type_ = List[type_] if prop.uselist else Optional[type_]
  191. return type_
  192. if isinstance(attr, AssociationProxyInstance):
  193. return List[
  194. get_attribute_access_type(
  195. attr.target_class,
  196. attr.remote_attr.key, # type: ignore[attr-defined]
  197. )
  198. ]
  199. elif isinstance(cls, type) and not is_generic_alias(cls) and issubclass(cls, Model):
  200. # Check in the annotations directly (for sqlmodel.Relationship)
  201. hints = get_type_hints(cls)
  202. if name in hints:
  203. type_ = hints[name]
  204. type_origin = get_origin(type_)
  205. if isinstance(type_origin, type) and issubclass(type_origin, Mapped):
  206. return get_args(type_)[0] # SQLAlchemy v2
  207. if isinstance(type_, ModelField):
  208. return type_.type_ # SQLAlchemy v1.4
  209. return type_
  210. elif is_union(cls):
  211. # Check in each arg of the annotation.
  212. for arg in get_args(cls):
  213. type_ = get_attribute_access_type(arg, name)
  214. if type_ is not None:
  215. # Return the first attribute type that is accessible.
  216. return type_
  217. elif isinstance(cls, type):
  218. # Bare class
  219. if sys.version_info >= (3, 10):
  220. exceptions = NameError
  221. else:
  222. exceptions = (NameError, TypeError)
  223. try:
  224. hints = get_type_hints(cls)
  225. if name in hints:
  226. return hints[name]
  227. except exceptions as e:
  228. console.warn(f"Failed to resolve ForwardRefs for {cls}.{name} due to {e}")
  229. pass
  230. return None # Attribute is not accessible.
  231. def get_base_class(cls: GenericType) -> Type:
  232. """Get the base class of a class.
  233. Args:
  234. cls: The class.
  235. Returns:
  236. The base class of the class.
  237. Raises:
  238. TypeError: If a literal has multiple types.
  239. """
  240. if is_literal(cls):
  241. # only literals of the same type are supported.
  242. arg_type = type(get_args(cls)[0])
  243. if not all(type(arg) == arg_type for arg in get_args(cls)):
  244. raise TypeError("only literals of the same type are supported")
  245. return type(get_args(cls)[0])
  246. if is_union(cls):
  247. return tuple(get_base_class(arg) for arg in get_args(cls))
  248. return get_base_class(cls.__origin__) if is_generic_alias(cls) else cls
  249. def _issubclass(cls: GenericType, cls_check: GenericType) -> bool:
  250. """Check if a class is a subclass of another class.
  251. Args:
  252. cls: The class to check.
  253. cls_check: The class to check against.
  254. Returns:
  255. Whether the class is a subclass of the other class.
  256. Raises:
  257. TypeError: If the base class is not valid for issubclass.
  258. """
  259. # Special check for Any.
  260. if cls_check == Any:
  261. return True
  262. if cls in [Any, Callable, None]:
  263. return False
  264. # Get the base classes.
  265. cls_base = get_base_class(cls)
  266. cls_check_base = get_base_class(cls_check)
  267. # The class we're checking should not be a union.
  268. if isinstance(cls_base, tuple):
  269. return False
  270. # Check if the types match.
  271. try:
  272. return cls_check_base == Any or issubclass(cls_base, cls_check_base)
  273. except TypeError as te:
  274. # These errors typically arise from bad annotations and are hard to
  275. # debug without knowing the type that we tried to compare.
  276. raise TypeError(f"Invalid type for issubclass: {cls_base}") from te
  277. def _isinstance(obj: Any, cls: GenericType) -> bool:
  278. """Check if an object is an instance of a class.
  279. Args:
  280. obj: The object to check.
  281. cls: The class to check against.
  282. Returns:
  283. Whether the object is an instance of the class.
  284. """
  285. return isinstance(obj, get_base_class(cls))
  286. def is_dataframe(value: Type) -> bool:
  287. """Check if the given value is a dataframe.
  288. Args:
  289. value: The value to check.
  290. Returns:
  291. Whether the value is a dataframe.
  292. """
  293. if is_generic_alias(value) or value == Any:
  294. return False
  295. return value.__name__ == "DataFrame"
  296. def is_valid_var_type(type_: Type) -> bool:
  297. """Check if the given type is a valid prop type.
  298. Args:
  299. type_: The type to check.
  300. Returns:
  301. Whether the type is a valid prop type.
  302. """
  303. if is_union(type_):
  304. return all((is_valid_var_type(arg) for arg in get_args(type_)))
  305. return _issubclass(type_, StateVar) or serializers.has_serializer(type_)
  306. def is_backend_variable(name: str, cls: Type | None = None) -> bool:
  307. """Check if this variable name correspond to a backend variable.
  308. Args:
  309. name: The name of the variable to check
  310. cls: The class of the variable to check
  311. Returns:
  312. bool: The result of the check
  313. """
  314. if cls is not None and name.startswith(f"_{cls.__name__}__"):
  315. return False
  316. return name.startswith("_") and not name.startswith("__")
  317. def check_type_in_allowed_types(value_type: Type, allowed_types: Iterable) -> bool:
  318. """Check that a value type is found in a list of allowed types.
  319. Args:
  320. value_type: Type of value.
  321. allowed_types: Iterable of allowed types.
  322. Returns:
  323. If the type is found in the allowed types.
  324. """
  325. return get_base_class(value_type) in allowed_types
  326. def check_prop_in_allowed_types(prop: Any, allowed_types: Iterable) -> bool:
  327. """Check that a prop value is in a list of allowed types.
  328. Does the check in a way that works regardless if it's a raw value or a state Var.
  329. Args:
  330. prop: The prop to check.
  331. allowed_types: The list of allowed types.
  332. Returns:
  333. If the prop type match one of the allowed_types.
  334. """
  335. from reflex.vars import Var
  336. type_ = prop._var_type if _isinstance(prop, Var) else type(prop)
  337. return type_ in allowed_types
  338. def is_encoded_fstring(value) -> bool:
  339. """Check if a value is an encoded Var f-string.
  340. Args:
  341. value: The value string to check.
  342. Returns:
  343. Whether the value is an f-string
  344. """
  345. return isinstance(value, str) and constants.REFLEX_VAR_OPENING_TAG in value
  346. def validate_literal(key: str, value: Any, expected_type: Type, comp_name: str):
  347. """Check that a value is a valid literal.
  348. Args:
  349. key: The prop name.
  350. value: The prop value to validate.
  351. expected_type: The expected type(literal type).
  352. comp_name: Name of the component.
  353. Raises:
  354. ValueError: When the value is not a valid literal.
  355. """
  356. from reflex.vars import Var
  357. if (
  358. is_literal(expected_type)
  359. and not isinstance(value, Var) # validating vars is not supported yet.
  360. and not is_encoded_fstring(value) # f-strings are not supported.
  361. and value not in expected_type.__args__
  362. ):
  363. allowed_values = expected_type.__args__
  364. if value not in allowed_values:
  365. allowed_value_str = ",".join(
  366. [str(v) if not isinstance(v, str) else f"'{v}'" for v in allowed_values]
  367. )
  368. value_str = f"'{value}'" if isinstance(value, str) else value
  369. raise ValueError(
  370. f"prop value for {str(key)} of the `{comp_name}` component should be one of the following: {allowed_value_str}. Got {value_str} instead"
  371. )
  372. def validate_parameter_literals(func):
  373. """Decorator to check that the arguments passed to a function
  374. correspond to the correct function parameter if it (the parameter)
  375. is a literal type.
  376. Args:
  377. func: The function to validate.
  378. Returns:
  379. The wrapper function.
  380. """
  381. @wraps(func)
  382. def wrapper(*args, **kwargs):
  383. func_params = list(inspect.signature(func).parameters.items())
  384. annotations = {param[0]: param[1].annotation for param in func_params}
  385. # validate args
  386. for param, arg in zip(annotations.keys(), args):
  387. if annotations[param] is inspect.Parameter.empty:
  388. continue
  389. validate_literal(param, arg, annotations[param], func.__name__)
  390. # validate kwargs.
  391. for key, value in kwargs.items():
  392. annotation = annotations.get(key)
  393. if not annotation or annotation is inspect.Parameter.empty:
  394. continue
  395. validate_literal(key, value, annotation, func.__name__)
  396. return func(*args, **kwargs)
  397. return wrapper
  398. # Store this here for performance.
  399. StateBases = get_base_class(StateVar)
  400. StateIterBases = get_base_class(StateIterVar)