type_assertions.py 788 B

1234567891011121314151617181920212223242526272829
  1. from collections.abc import Iterable
  2. from typing import Any
  3. from django.db.models.query import QuerySet
  4. def is_query_set(value: Any) -> bool:
  5. """Gets whether the specified value is a :see:QuerySet."""
  6. return isinstance(value, QuerySet) # type: ignore[misc]
  7. def is_sql(value: Any) -> bool:
  8. """Gets whether the specified value could be a raw SQL query."""
  9. return isinstance(value, str)
  10. def is_sql_with_params(value: Any) -> bool:
  11. """Gets whether the specified value is a tuple of a SQL query (as a string)
  12. and a tuple of bind parameters."""
  13. return (
  14. isinstance(value, tuple)
  15. and len(value) == 2
  16. and is_sql(value[0])
  17. and isinstance(value[1], Iterable)
  18. and not isinstance(value[1], (str, bytes, bytearray))
  19. )