Skip to content

Commit 6de2ab7

Browse files
committed
Ruff PERF: fixed all remaining performance linting issues
1 parent a0e2b04 commit 6de2ab7

14 files changed

+28
-57
lines changed

duckdb/experimental/spark/exception.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Optional
22

33

4-
class ContributionsAcceptedError(NotImplementedError): # noqa: D100
4+
class ContributionsAcceptedError(NotImplementedError):
55
"""This method is not planned to be implemented, if you would like to implement this method
66
or show your interest in this method to other members of the community,
77
feel free to open up a PR or a Discussion over on https://github.com/duckdb/duckdb.

duckdb/experimental/spark/sql/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import re
99
import time
1010
from builtins import tuple
11-
from collections.abc import Iterator
11+
from collections.abc import Iterator, Mapping
1212
from types import MappingProxyType
1313
from typing import (
1414
Any,
@@ -18,7 +18,7 @@
1818
TypeVar,
1919
Union,
2020
cast,
21-
overload, Mapping,
21+
overload,
2222
)
2323

2424
import duckdb

duckdb/polars_io.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,12 @@ def source_generator(
235235
results = relation_final.fetch_arrow_reader()
236236
else:
237237
results = relation_final.fetch_arrow_reader(batch_size)
238-
while True:
239-
try:
240-
record_batch = results.read_next_batch()
241-
if predicate is not None and duck_predicate is None:
242-
# We have a predicate, but did not manage to push it down, we fallback here
243-
yield pl.from_arrow(record_batch).filter(predicate)
244-
else:
245-
yield pl.from_arrow(record_batch)
246-
except StopIteration:
247-
break
238+
239+
for record_batch in iter(results.read_next_batch, None):
240+
if predicate is not None and duck_predicate is None:
241+
# We have a predicate, but did not manage to push it down, we fallback here
242+
yield pl.from_arrow(record_batch).filter(predicate)
243+
else:
244+
yield pl.from_arrow(record_batch)
248245

249246
return register_io_source(source_generator, schema=schema)

scripts/generate_connection_methods.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ def create_definition(name, method) -> str:
109109
body = []
110110
for method in connection_methods:
111111
names = method["name"] if isinstance(method["name"], list) else [method["name"]]
112-
for name in names:
113-
body.append(create_definition(name, method))
112+
body.extend(create_definition(name, method) for name in names)
114113

115114
# ---- End of generation code ----
116115

scripts/generate_import_cache_cpp.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,7 @@ def __init__(self, module: dict) -> None:
133133
self.classes.reverse()
134134

135135
def get_classes(self):
136-
classes = []
137-
for item in self.classes:
138-
classes.append(item.to_string())
139-
return "".join(classes)
136+
return "".join(item.to_string() for item in self.classes)
140137

141138
def to_string(self):
142139
string = f"""
@@ -233,10 +230,8 @@ def get_root_modules(files: list[ModuleFile]):
233230

234231

235232
def get_module_file_path_includes(files: list[ModuleFile]):
236-
includes = []
237-
for file in files:
238-
includes.append(f'#include "duckdb_python/import_cache/modules/{file.file_name}"')
239-
return "\n".join(includes)
233+
template = '#include "duckdb_python/import_cache/modules/{}'
234+
return "\n".join(template.format(f.file_name) for f in files)
240235

241236

242237
module_includes = get_module_file_path_includes(files)

tests/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,7 @@ def _require(extension_name, db_name="") -> Union[duckdb.DuckDBPyConnection, Non
207207

208208
extension_paths_found = []
209209
for pattern in extension_search_patterns:
210-
extension_pattern_abs = Path(pattern).resolve()
211-
for path in extension_pattern_abs.glob("*"):
212-
extension_paths_found.append(path)
210+
extension_paths_found.extend(list(Path(pattern).resolve().glob("*")))
213211

214212
for path in extension_paths_found:
215213
print(path)

tests/coverage/test_pandas_categorical_coverage.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,10 @@ def check_create_table(category, pandas):
7272
class TestCategory:
7373
@pytest.mark.parametrize("pandas", [NumpyPandas()])
7474
def test_category_string_uint16(self, duckdb_cursor, pandas):
75-
category = []
76-
for i in range(300):
77-
category.append(str(i))
75+
category = [str(i) for i in range(300)]
7876
check_create_table(category, pandas)
7977

8078
@pytest.mark.parametrize("pandas", [NumpyPandas()])
8179
def test_category_string_uint32(self, duckdb_cursor, pandas):
82-
category = []
83-
for i in range(70000):
84-
category.append(str(i))
80+
category = [str(i) for i in range(70000)]
8581
check_create_table(category, pandas)

tests/fast/api/test_duckdb_connection.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,9 @@ def test_cursor_lifetime(self):
8787
con = duckdb.connect()
8888

8989
def use_cursors() -> None:
90-
cursors = []
91-
for _ in range(10):
92-
cursors.append(con.cursor())
90+
cursors = [con.cursor() for _ in range(10)]
9391

9492
for cursor in cursors:
95-
print("closing cursor")
9693
cursor.close()
9794

9895
use_cursors()

tests/fast/arrow/test_arrow_list.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ def create_and_register_comparison_result(column_list, duckdb_cursor):
2424
column_amount = len(column_list)
2525
assert column_amount
2626
row_amount = len(column_list[0][2])
27-
inserted_values = []
28-
for row in range(row_amount):
29-
for col in range(column_amount):
30-
inserted_values.append(column_list[col][2][row])
31-
inserted_values = tuple(inserted_values)
27+
inserted_values = tuple(column_list[col][2][row] for row in range(row_amount) for col in range(column_amount))
3228

3329
column_format = ",".join(["?" for _ in range(column_amount)])
3430
row_format = ",".join([f"({column_format})" for _ in range(row_amount)])

tests/fast/pandas/test_df_object_resolution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def test_structs_in_nested_types(self, pandas, duckdb_cursor):
372372
"v4": ObjectPair({}, {"key1": 21}),
373373
}
374374

375-
for _, pair in pairs.items():
375+
for pair in pairs.values():
376376
check_struct_upgrade("MAP(VARCHAR, INTEGER)[]", construct_list, pair, pandas, duckdb_cursor)
377377

378378
for key, pair in pairs.items():

0 commit comments

Comments
 (0)