types.py 19 KB

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