Skip to content

Commit c75c279

Browse files
committed
refactor(sqlstream): adopt PEP 585 type hinting
- Replace `List`, `Dict`, `Optional`, and `Tuple` with built-in generics `list`, `dict`, `None`, and `tuple` - Update function signatures across multiple modules to use new type hinting - Modify import statements to reflect changes in type usage This refactor aligns the codebase with PEP 585, simplifying type hinting and improving readability. Note that these changes introduce breaking changes due to updated function signatures.
1 parent 5d94735 commit c75c279

41 files changed

Lines changed: 233 additions & 223 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/json_nested_paths.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import tempfile
88
from pathlib import Path
9+
910
from sqlstream import query
1011

1112
# Create example data
@@ -18,30 +19,28 @@
1819
"name": "Alice",
1920
"transactions": [
2021
{"id": "t1", "amount": 50, "status": "completed"},
21-
{"id": "t2", "amount": 75, "status": "pending"}
22-
]
22+
{"id": "t2", "amount": 75, "status": "pending"},
23+
],
2324
},
2425
{
2526
"id": 2,
2627
"name": "Bob",
2728
"transactions": [
2829
{"id": "t3", "amount": 100, "status": "completed"},
29-
{"id": "t4", "amount": 125, "status": "completed"}
30-
]
30+
{"id": "t4", "amount": 125, "status": "completed"},
31+
],
3132
},
3233
{
3334
"id": 3,
3435
"name": "Charlie",
35-
"transactions": [
36-
{"id": "t5", "amount": 200, "status": "failed"}
37-
]
38-
}
36+
"transactions": [{"id": "t5", "amount": 200, "status": "failed"}],
37+
},
3938
]
40-
}
39+
},
4140
}
4241

4342
# Save to temporary file
44-
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
43+
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
4544
json.dump(data, f)
4645
json_file = f.name
4746

@@ -58,7 +57,9 @@
5857
# Example 2: Array indexing
5958
print("\n2. Get first user's transactions: result.users[0].transactions")
6059
print(" Query: SELECT * FROM transactions")
61-
for row in query(f"{json_file}#json:result.users[0].transactions").sql("SELECT * FROM transactions"):
60+
for row in query(f"{json_file}#json:result.users[0].transactions").sql(
61+
"SELECT * FROM transactions"
62+
):
6263
print(f" {row}")
6364

6465
# Example 3: Array flattening
@@ -72,10 +73,12 @@
7273
# Example 4: Aggregation on flattened data
7374
print("\n4. Total amount of completed transactions")
7475
print(" Query: SELECT amount FROM transactions WHERE status = 'completed'")
75-
completed = list(query(f"{json_file}#json:result.users[].transactions").sql(
76-
"SELECT amount FROM transactions WHERE status = 'completed'"
77-
))
78-
total = sum(row['amount'] for row in completed)
76+
completed = list(
77+
query(f"{json_file}#json:result.users[].transactions").sql(
78+
"SELECT amount FROM transactions WHERE status = 'completed'"
79+
)
80+
)
81+
total = sum(row["amount"] for row in completed)
7982
print(f" Total: ${total} from {len(completed)} transactions")
8083

8184
# Clean up

scripts/generate_api_docs.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import os
1414
import re
1515
from pathlib import Path
16-
from typing import Dict, List
1716

1817

1918
class APIDocGenerator:
@@ -51,7 +50,7 @@ def __init__(self, source_dir: str = "sqlstream", docs_dir: str = "docs/api/refe
5150
"utils": "Utility functions and helpers.",
5251
}
5352

54-
def discover_modules(self) -> Dict[str, List[str]]:
53+
def discover_modules(self) -> dict[str, list[str]]:
5554
"""Automatically discover all Python modules in the source directory"""
5655
modules_by_category = {}
5756

@@ -87,7 +86,7 @@ def discover_modules(self) -> Dict[str, List[str]]:
8786

8887
return modules_by_category
8988

90-
def get_classes_and_functions(self, module_path: str) -> Dict[str, List[str]]:
89+
def get_classes_and_functions(self, module_path: str) -> dict[str, list[str]]:
9190
"""Extract classes and functions from a Python module"""
9291
# Convert module path to file path
9392
relative_path = module_path.replace("sqlstream.", "").replace(".", "/")
@@ -131,7 +130,7 @@ def generate_mkdocstrings_block(
131130
show_source: true
132131
"""
133132

134-
def generate_category_doc(self, category_name: str, modules: List[str]) -> str:
133+
def generate_category_doc(self, category_name: str, modules: list[str]) -> str:
135134
"""Generate markdown file for a category"""
136135
title = self.category_titles.get(category_name, f"{category_name.title()} Reference")
137136
description = self.category_descriptions.get(
@@ -160,7 +159,7 @@ def generate_category_doc(self, category_name: str, modules: List[str]) -> str:
160159
return "\n".join(lines)
161160

162161
def update_mkdocs_nav(
163-
self, modules_by_category: Dict[str, List[str]], mkdocs_file: str = "mkdocs.yml"
162+
self, modules_by_category: dict[str, list[str]], mkdocs_file: str = "mkdocs.yml"
164163
):
165164
"""Update mkdocs.yml navigation with generated API reference pages using text manipulation"""
166165
mkdocs_path = Path(mkdocs_file)

sqlstream/cli/formatters/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
All formatters must implement the format() method.
55
"""
66

7-
from typing import Any, Dict, List
7+
from typing import Any
88

99

1010
class BaseFormatter:
1111
"""Base class for all output formatters"""
1212

13-
def format(self, results: List[Dict[str, Any]], **kwargs) -> str:
13+
def format(self, results: list[dict[str, Any]], **kwargs) -> str:
1414
"""
1515
Format query results for output
1616

sqlstream/cli/formatters/csv.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
import csv
66
import io
7-
from typing import Any, Dict, List
7+
from typing import Any
88

99
from sqlstream.cli.formatters.base import BaseFormatter
1010

1111

1212
class CSVFormatter(BaseFormatter):
1313
"""Format results as CSV"""
1414

15-
def format(self, results: List[Dict[str, Any]], **kwargs) -> str:
15+
def format(self, results: list[dict[str, Any]], **kwargs) -> str:
1616
"""
1717
Format results as CSV
1818

sqlstream/cli/formatters/json.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
import json
66
import math
7-
from typing import Any, Dict, List
7+
from typing import Any
88

99
from sqlstream.cli.formatters.base import BaseFormatter
1010

1111

1212
class JSONFormatter(BaseFormatter):
1313
"""Format results as JSON"""
1414

15-
def format(self, results: List[Dict[str, Any]], **kwargs) -> str:
15+
def format(self, results: list[dict[str, Any]], **kwargs) -> str:
1616
"""
1717
Format results as JSON
1818

sqlstream/cli/formatters/markdown.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
Markdown formatter for documentation and sharing
33
"""
44

5-
from typing import Any, Dict, List
5+
from typing import Any
66

77
from sqlstream.cli.formatters.base import BaseFormatter
88

99

1010
class MarkdownFormatter(BaseFormatter):
1111
"""Format results as a Markdown table"""
1212

13-
def format(self, results: List[Dict[str, Any]], **kwargs) -> str:
13+
def format(self, results: list[dict[str, Any]], **kwargs) -> str:
1414
"""
1515
Format results as a Markdown table
1616

sqlstream/cli/formatters/table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Rich table formatter for beautiful terminal output
33
"""
44

5-
from typing import Any, Dict, List
5+
from typing import Any
66

77
try:
88
from rich import box
@@ -22,7 +22,7 @@
2222
class TableFormatter(BaseFormatter):
2323
"""Format results as a beautiful Rich table"""
2424

25-
def format(self, results: List[Dict[str, Any]], **kwargs) -> str:
25+
def format(self, results: list[dict[str, Any]], **kwargs) -> str:
2626
"""
2727
Format results as a Rich table
2828

sqlstream/cli/interactive.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import shutil
88
import sys
9-
from typing import Any, Dict, List
9+
from typing import Any
1010

1111
try:
1212
from textual.app import App, ComposeResult
@@ -25,7 +25,7 @@
2525

2626

2727
def should_use_interactive(
28-
results: List[Dict[str, Any]],
28+
results: list[dict[str, Any]],
2929
force: bool = False,
3030
no_interactive: bool = False,
3131
output_file: str = None,
@@ -116,7 +116,7 @@ class TableApp(App):
116116
("l", "cursor_right", "Right"),
117117
]
118118

119-
def __init__(self, results: List[Dict[str, Any]], **kwargs):
119+
def __init__(self, results: list[dict[str, Any]], **kwargs):
120120
super().__init__(**kwargs)
121121
self.results = results
122122

@@ -159,7 +159,7 @@ def action_quit(self) -> None:
159159
TableApp = None
160160

161161

162-
def launch_interactive(results: List[Dict[str, Any]]) -> None:
162+
def launch_interactive(results: list[dict[str, Any]]) -> None:
163163
"""
164164
Launch interactive table viewer.
165165

sqlstream/core/duckdb_executor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
from __future__ import annotations
99

10-
from typing import Any, Callable, Iterator
10+
from collections.abc import Callable, Iterator
11+
from typing import Any
1112

1213
from sqlstream.readers.base import BaseReader
1314

sqlstream/core/executor.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
that implement the query using the Volcano pull-based model.
66
"""
77

8-
from typing import Any, Callable, Dict, Iterator, Optional
8+
from collections.abc import Callable, Iterator
9+
from typing import Any
910

1011
from sqlstream.operators.filter import Filter
1112
from sqlstream.operators.groupby import GroupByOperator
@@ -48,8 +49,8 @@ def execute(
4849
self,
4950
ast: SelectStatement,
5051
reader: BaseReader,
51-
reader_factory: Optional[Callable[[str], BaseReader]] = None,
52-
) -> Iterator[Dict[str, Any]]:
52+
reader_factory: Callable[[str], BaseReader] | None = None,
53+
) -> Iterator[dict[str, Any]]:
5354
"""
5455
Execute query and return iterator over results
5556
@@ -81,7 +82,7 @@ def _build_plan(
8182
self,
8283
ast: SelectStatement,
8384
reader: BaseReader,
84-
reader_factory: Optional[Callable[[str], BaseReader]] = None,
85+
reader_factory: Callable[[str], BaseReader] | None = None,
8586
):
8687
"""
8788
Build operator tree from AST
@@ -158,7 +159,7 @@ def explain(
158159
self,
159160
ast: SelectStatement,
160161
reader: BaseReader,
161-
reader_factory: Optional[Callable[[str], BaseReader]] = None,
162+
reader_factory: Callable[[str], BaseReader] | None = None,
162163
) -> str:
163164
"""
164165
Explain query execution plan (for debugging)

0 commit comments

Comments
 (0)