Skip to content

Commit 89ee914

Browse files
committed
Support LiteralString
1 parent fad6ba8 commit 89ee914

File tree

5 files changed

+41
-18
lines changed

5 files changed

+41
-18
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ dev-venv-base:
5757
dev-venv-create: dev-venv-base dev-venv-install
5858

5959
dev-venv-install:
60-
${VENV} pip install --upgrade pip setuptools wheel
61-
${VENV} pip install --upgrade build sphinx tox twine
62-
${VENV} pip install -e "${SRC_DIR}"
60+
${VENV} pip3 install --upgrade pip setuptools wheel
61+
${VENV} pip3 install --upgrade build sphinx tox twine typing-extensions
62+
${VENV} pip3 install -e "${SRC_DIR}"
6363

6464

6565
################################################################################

sqlparams/__init__.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import re
77
from typing import (
88
Any,
9-
AnyStr,
109
Dict,
1110
Iterable,
1211
List,
@@ -26,8 +25,9 @@
2625
__copyright__,
2726
__credits__,
2827
__license__,
29-
__version__,
30-
)
28+
__version__)
29+
from ._util import (
30+
SqlStr)
3131

3232
_BYTES_ENCODING = 'latin1'
3333
"""
@@ -471,14 +471,15 @@ def expand_tuples(self) -> bool:
471471

472472
def format(
473473
self,
474-
sql: AnyStr,
474+
sql: SqlStr,
475475
params: Union[Dict[Union[str, int], Any], Sequence[Any]],
476-
) -> Tuple[AnyStr, Union[Dict[Union[str, int], Any], Sequence[Any]]]:
476+
) -> Tuple[SqlStr, Union[Dict[str, Any], Sequence[Any]]]:
477477
"""
478478
Convert the SQL query to use the out-style parameters instead of
479479
the in-style parameters.
480480
481-
*sql* (:class:`str` or :class:`bytes`) is the SQL query.
481+
*sql* (:class:`LiteralString`, :class:`str`, or :class:`bytes`) is
482+
the SQL query.
482483
483484
*params* (:class:`~collections.abc.Mapping` or :class:`~collections.abc.Sequence`)
484485
contains the set of in-style parameters. It maps each parameter
@@ -489,7 +490,8 @@ def format(
489490
490491
Returns a :class:`tuple` containing:
491492
492-
- The formatted SQL query (:class:`str` or :class:`bytes`).
493+
- The formatted SQL query (:class:`LiteralString`, :class:`str` or
494+
:class:`bytes`).
493495
494496
- The set of converted out-style parameters (:class:`dict` or
495497
:class:`list`).
@@ -521,14 +523,15 @@ def format(
521523

522524
def formatmany(
523525
self,
524-
sql: AnyStr,
526+
sql: SqlStr,
525527
many_params: Union[Iterable[Dict[Union[str, int], Any]], Iterable[Sequence[Any]]],
526-
) -> Tuple[AnyStr, Union[List[Dict[Union[str, int], Any]], List[Sequence[Any]]]]:
528+
) -> Tuple[SqlStr, Union[List[Dict[str, Any]], List[Sequence[Any]]]]:
527529
"""
528530
Convert the SQL query to use the out-style parameters instead of the
529531
in-style parameters.
530532
531-
*sql* (:class:`str` or :class:`bytes`) is the SQL query.
533+
*sql* (:class:`LiteralString`, :class:`str` or :class:`bytes`) is
534+
the SQL query.
532535
533536
*many_params* (:class:`~collections.abc.Iterable`) contains each set
534537
of in-style parameters (*params*).
@@ -542,7 +545,8 @@ def formatmany(
542545
543546
Returns a :class:`tuple` containing:
544547
545-
- The formatted SQL query (:class:`str` or :class:`bytes`).
548+
- The formatted SQL query (:class:`LiteralString`, :class:`str` or
549+
:class:`bytes`).
546550
547551
- A :class:`list` containing each set of converted out-style
548552
parameters (:class:`dict` or :class:`list`).

sqlparams/_converting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def convert(
7979
self,
8080
sql: str,
8181
params: Union[Dict[Union[str, int], Any], Sequence[Any]],
82-
) -> Tuple[str, Union[Dict[Union[str, int], Any], Sequence[Any]]]:
82+
) -> Tuple[str, Union[Dict[str, Any], Sequence[Any]]]:
8383
"""
8484
Convert the SQL query to use the named out-style parameters from the
8585
named the in-style parameters.

sqlparams/_meta.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
__author__ = "Caleb P. Burns"
6-
__copyright__ = "Copyright © 2012-2022 by Caleb P. Burns"
6+
__copyright__ = "Copyright © 2012-2023 by Caleb P. Burns"
77
__credits__ = [
88
"khomyakov42 <https://github.com/khomyakov42>",
99
"pedermoller <https://github.com/pedermoller>",
@@ -14,4 +14,4 @@
1414
"sergey-shambir <https://github.com/sergey-shambir>",
1515
]
1616
__license__ = "MIT License"
17-
__version__ = "5.0.0"
17+
__version__ = "5.1.0.dev1"

sqlparams/_util.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,26 @@
22
This module defines internal utility methods.
33
"""
44

5-
from collections.abc import Iterable, Sequence
5+
from collections.abc import (
6+
Iterable,
7+
Sequence)
8+
from typing import (
9+
TypeVar)
10+
11+
# LiteralString: Python 3.11+
12+
try:
13+
from typing import LiteralString
14+
except ImportError:
15+
try:
16+
from typing_extensions import LiteralString
17+
except ImportError:
18+
LiteralString = str
19+
20+
SqlStr = TypeVar('SqlStr', LiteralString, str, bytes)
21+
"""
22+
Constrained type variable for SQL strings (:class:`LiteralString`,
23+
:class:`str`, :class:`bytes`).
24+
"""
625

726

827
def is_iterable(value):

0 commit comments

Comments
 (0)