types.py 18 KB

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