Skip to content

Commit f1d2d68

Browse files
committed
Add type annotations to public API functions
- Add type annotations to parse(), parsestream(), format(), and split() - Add py.typed marker for PEP 561 compliance - Enable type checking with mypy and other type checkers - Compatible with Python 3.8+ Fixes #756
1 parent 21e78dc commit f1d2d68

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Development Version
44
Enhancements
55

66
* Add support for Python 3.14.
7+
* Add type annotations to top-level API functions and include py.typed marker
8+
for PEP 561 compliance, enabling type checking with mypy and other tools
9+
(issue756).
710
* Add `ATTACH` and `DETACH` to PostgreSQL keywords (pr808).
811
* Add `INTERSECT` to close keywords in WHERE clause (pr820).
912
* Support `REGEXP BINARY` comparison operator (pr817).

sqlparse/__init__.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"""Parse SQL statements."""
99

1010
# Setup namespace
11+
from typing import Any, Generator, IO, List, Optional, Tuple, Union
12+
1113
from sqlparse import sql
1214
from sqlparse import cli
1315
from sqlparse import engine
@@ -16,11 +18,13 @@
1618
from sqlparse import formatter
1719

1820

19-
__version__ = '0.5.4.dev0'
20-
__all__ = ['engine', 'filters', 'formatter', 'sql', 'tokens', 'cli']
21+
__version__ = "0.5.4.dev0"
22+
__all__ = ["engine", "filters", "formatter", "sql", "tokens", "cli"]
2123

2224

23-
def parse(sql, encoding=None):
25+
def parse(
26+
sql: str, encoding: Optional[str] = None
27+
) -> Tuple[sql.Statement, ...]:
2428
"""Parse sql and return a list of statements.
2529
2630
:param sql: A string containing one or more SQL statements.
@@ -30,7 +34,9 @@ def parse(sql, encoding=None):
3034
return tuple(parsestream(sql, encoding))
3135

3236

33-
def parsestream(stream, encoding=None):
37+
def parsestream(
38+
stream: Union[str, IO[str]], encoding: Optional[str] = None
39+
) -> Generator[sql.Statement, None, None]:
3440
"""Parses sql statements from file-like object.
3541
3642
:param stream: A file-like object.
@@ -42,7 +48,7 @@ def parsestream(stream, encoding=None):
4248
return stack.run(stream, encoding)
4349

4450

45-
def format(sql, encoding=None, **options):
51+
def format(sql: str, encoding: Optional[str] = None, **options: Any) -> str:
4652
"""Format *sql* according to *options*.
4753
4854
Available options are documented in :ref:`formatting`.
@@ -56,10 +62,12 @@ def format(sql, encoding=None, **options):
5662
options = formatter.validate_options(options)
5763
stack = formatter.build_filter_stack(stack, options)
5864
stack.postprocess.append(filters.SerializerUnicode())
59-
return ''.join(stack.run(sql, encoding))
65+
return "".join(stack.run(sql, encoding))
6066

6167

62-
def split(sql, encoding=None, strip_semicolon=False):
68+
def split(
69+
sql: str, encoding: Optional[str] = None, strip_semicolon: bool = False
70+
) -> List[str]:
6371
"""Split *sql* into single statements.
6472
6573
:param sql: A string containing one or more SQL statements.

sqlparse/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)