Skip to content

Commit

Permalink
Support LiteralString
Browse files Browse the repository at this point in the history
  • Loading branch information
cpburnz committed Mar 8, 2023
1 parent fad6ba8 commit 89ee914
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 18 deletions.
6 changes: 3 additions & 3 deletions Makefile
Expand Up @@ -57,9 +57,9 @@ dev-venv-base:
dev-venv-create: dev-venv-base dev-venv-install

dev-venv-install:
${VENV} pip install --upgrade pip setuptools wheel
${VENV} pip install --upgrade build sphinx tox twine
${VENV} pip install -e "${SRC_DIR}"
${VENV} pip3 install --upgrade pip setuptools wheel
${VENV} pip3 install --upgrade build sphinx tox twine typing-extensions
${VENV} pip3 install -e "${SRC_DIR}"


################################################################################
Expand Down
26 changes: 15 additions & 11 deletions sqlparams/__init__.py
Expand Up @@ -6,7 +6,6 @@
import re
from typing import (
Any,
AnyStr,
Dict,
Iterable,
List,
Expand All @@ -26,8 +25,9 @@
__copyright__,
__credits__,
__license__,
__version__,
)
__version__)
from ._util import (
SqlStr)

_BYTES_ENCODING = 'latin1'
"""
Expand Down Expand Up @@ -471,14 +471,15 @@ def expand_tuples(self) -> bool:

def format(
self,
sql: AnyStr,
sql: SqlStr,
params: Union[Dict[Union[str, int], Any], Sequence[Any]],
) -> Tuple[AnyStr, Union[Dict[Union[str, int], Any], Sequence[Any]]]:
) -> Tuple[SqlStr, Union[Dict[str, Any], Sequence[Any]]]:
"""
Convert the SQL query to use the out-style parameters instead of
the in-style parameters.
*sql* (:class:`str` or :class:`bytes`) is the SQL query.
*sql* (:class:`LiteralString`, :class:`str`, or :class:`bytes`) is
the SQL query.
*params* (:class:`~collections.abc.Mapping` or :class:`~collections.abc.Sequence`)
contains the set of in-style parameters. It maps each parameter
Expand All @@ -489,7 +490,8 @@ def format(
Returns a :class:`tuple` containing:
- The formatted SQL query (:class:`str` or :class:`bytes`).
- The formatted SQL query (:class:`LiteralString`, :class:`str` or
:class:`bytes`).
- The set of converted out-style parameters (:class:`dict` or
:class:`list`).
Expand Down Expand Up @@ -521,14 +523,15 @@ def format(

def formatmany(
self,
sql: AnyStr,
sql: SqlStr,
many_params: Union[Iterable[Dict[Union[str, int], Any]], Iterable[Sequence[Any]]],
) -> Tuple[AnyStr, Union[List[Dict[Union[str, int], Any]], List[Sequence[Any]]]]:
) -> Tuple[SqlStr, Union[List[Dict[str, Any]], List[Sequence[Any]]]]:
"""
Convert the SQL query to use the out-style parameters instead of the
in-style parameters.
*sql* (:class:`str` or :class:`bytes`) is the SQL query.
*sql* (:class:`LiteralString`, :class:`str` or :class:`bytes`) is
the SQL query.
*many_params* (:class:`~collections.abc.Iterable`) contains each set
of in-style parameters (*params*).
Expand All @@ -542,7 +545,8 @@ def formatmany(
Returns a :class:`tuple` containing:
- The formatted SQL query (:class:`str` or :class:`bytes`).
- The formatted SQL query (:class:`LiteralString`, :class:`str` or
:class:`bytes`).
- A :class:`list` containing each set of converted out-style
parameters (:class:`dict` or :class:`list`).
Expand Down
2 changes: 1 addition & 1 deletion sqlparams/_converting.py
Expand Up @@ -79,7 +79,7 @@ def convert(
self,
sql: str,
params: Union[Dict[Union[str, int], Any], Sequence[Any]],
) -> Tuple[str, Union[Dict[Union[str, int], Any], Sequence[Any]]]:
) -> Tuple[str, Union[Dict[str, Any], Sequence[Any]]]:
"""
Convert the SQL query to use the named out-style parameters from the
named the in-style parameters.
Expand Down
4 changes: 2 additions & 2 deletions sqlparams/_meta.py
Expand Up @@ -3,7 +3,7 @@
"""

__author__ = "Caleb P. Burns"
__copyright__ = "Copyright © 2012-2022 by Caleb P. Burns"
__copyright__ = "Copyright © 2012-2023 by Caleb P. Burns"
__credits__ = [
"khomyakov42 <https://github.com/khomyakov42>",
"pedermoller <https://github.com/pedermoller>",
Expand All @@ -14,4 +14,4 @@
"sergey-shambir <https://github.com/sergey-shambir>",
]
__license__ = "MIT License"
__version__ = "5.0.0"
__version__ = "5.1.0.dev1"
21 changes: 20 additions & 1 deletion sqlparams/_util.py
Expand Up @@ -2,7 +2,26 @@
This module defines internal utility methods.
"""

from collections.abc import Iterable, Sequence
from collections.abc import (
Iterable,
Sequence)
from typing import (
TypeVar)

# LiteralString: Python 3.11+
try:
from typing import LiteralString
except ImportError:
try:
from typing_extensions import LiteralString
except ImportError:
LiteralString = str

SqlStr = TypeVar('SqlStr', LiteralString, str, bytes)
"""
Constrained type variable for SQL strings (:class:`LiteralString`,
:class:`str`, :class:`bytes`).
"""


def is_iterable(value):
Expand Down

0 comments on commit 89ee914

Please sign in to comment.