types.py 15 KB

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