File: type_assertions.py

package info (click to toggle)
python-django-postgres-extra 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,096 kB
  • sloc: python: 9,057; makefile: 17; sh: 7; sql: 1
file content (29 lines) | stat: -rw-r--r-- 788 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from collections.abc import Iterable
from typing import Any

from django.db.models.query import QuerySet


def is_query_set(value: Any) -> bool:
    """Gets whether the specified value is a :see:QuerySet."""

    return isinstance(value, QuerySet)  # type: ignore[misc]


def is_sql(value: Any) -> bool:
    """Gets whether the specified value could be a raw SQL query."""

    return isinstance(value, str)


def is_sql_with_params(value: Any) -> bool:
    """Gets whether the specified value is a tuple of a SQL query (as a string)
    and a tuple of bind parameters."""

    return (
        isinstance(value, tuple)
        and len(value) == 2
        and is_sql(value[0])
        and isinstance(value[1], Iterable)
        and not isinstance(value[1], (str, bytes, bytearray))
    )