types.py 14 KB

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