Skip to content

Commit 2a2a8b4

Browse files
committed
Ruff PT012: pytest.raises contextmanagers should only have a single statement
1 parent f2ab732 commit 2a2a8b4

16 files changed

+57
-115
lines changed

tests/conftest.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -274,35 +274,3 @@ def timestamps(duckdb_cursor):
274274
cursor.execute("INSERT INTO timestamps VALUES ('1992-10-03 18:34:45'), ('2010-01-01 00:00:01'), (NULL)")
275275
yield
276276
cursor.execute("drop table timestamps")
277-
278-
279-
@pytest.fixture
280-
def duckdb_cursor_autocommit(request, tmp_path):
281-
test_dbfarm = tmp_path.resolve().as_posix()
282-
283-
def finalizer() -> None:
284-
duckdb.shutdown()
285-
if tmp_path.is_dir():
286-
shutil.rmtree(test_dbfarm)
287-
288-
request.addfinalizer(finalizer)
289-
290-
connection = duckdb.connect(test_dbfarm)
291-
connection.set_autocommit(True)
292-
cursor = connection.cursor()
293-
return (cursor, connection, test_dbfarm)
294-
295-
296-
@pytest.fixture
297-
def initialize_duckdb(request, tmp_path):
298-
test_dbfarm = tmp_path.resolve().as_posix()
299-
300-
def finalizer() -> None:
301-
duckdb.shutdown()
302-
if tmp_path.is_dir():
303-
shutil.rmtree(test_dbfarm)
304-
305-
request.addfinalizer(finalizer)
306-
307-
duckdb.connect(test_dbfarm)
308-
return test_dbfarm

tests/extensions/test_extensions_loading.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ def test_install_non_existent_extension():
2727
with pytest.raises(duckdb.IOException) as exc:
2828
conn.install_extension("non-existent")
2929

30-
if not isinstance(exc, duckdb.HTTPException):
31-
pytest.skip(reason="This test does not throw an HTTPException, only an IOException")
32-
value = exc.value
33-
34-
assert value.status_code == 404
35-
assert value.reason == "Not Found"
36-
assert "Example Domain" in value.body
37-
assert "Content-Length" in value.headers
30+
if not isinstance(exc, duckdb.HTTPException):
31+
pytest.skip(reason="This test does not throw an HTTPException, only an IOException")
32+
value = exc.value
33+
34+
assert value.status_code == 404
35+
assert value.reason == "Not Found"
36+
assert "Example Domain" in value.body
37+
assert "Content-Length" in value.headers
3838

3939

4040
def test_install_misuse_errors(duckdb_cursor):

tests/fast/api/test_connection_close.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ def test_connection_close(self, duckdb_cursor):
2727
check_exception(lambda: cursor.execute("select * from a"))
2828

2929
def test_open_and_exit(self):
30-
with pytest.raises(TypeError), duckdb.connect() as connection:
31-
connection.execute("select 42")
32-
# This exception does not get swallowed by __exit__
30+
with pytest.raises(TypeError), duckdb.connect():
31+
# This exception does not get swallowed by DuckDBPyConnection's __exit__
3332
raise TypeError()
3433

34+
3535
def test_reopen_connection(self, duckdb_cursor):
3636
with tempfile.NamedTemporaryFile(delete=False) as tmp:
3737
db = tmp.name

tests/fast/api/test_read_csv.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -491,15 +491,14 @@ def test_read_csv_names(self, tmp_path):
491491
assert rel.columns == ["a", "b", "c", "four"]
492492

493493
with pytest.raises(duckdb.InvalidInputException, match="read_csv only accepts 'names' as a list of strings"):
494-
rel = con.read_csv(file, names=True)
494+
con.read_csv(file, names=True)
495495

496496
with pytest.raises(duckdb.InvalidInputException, match="not possible to detect the CSV Header"):
497-
rel = con.read_csv(file, names=["a", "b", "c", "d", "e"])
497+
con.read_csv(file, names=["a", "b", "c", "d", "e"])
498498

499499
# Duplicates are not okay
500500
with pytest.raises(duckdb.BinderException, match="names must have unique values"):
501-
rel = con.read_csv(file, names=["a", "b", "a", "b"])
502-
assert rel.columns == ["a", "b", "a", "b"]
501+
con.read_csv(file, names=["a", "b", "a", "b"])
503502

504503
def test_read_csv_names_mixed_with_dtypes(self, tmp_path):
505504
file = tmp_path / "file.csv"
@@ -565,8 +564,7 @@ def test_read_csv_empty_list(self):
565564
with pytest.raises(
566565
duckdb.InvalidInputException, match="Please provide a non-empty list of paths or file-like objects"
567566
):
568-
rel = con.read_csv(files)
569-
rel.fetchall()
567+
con.read_csv(files)
570568

571569
def test_read_auto_detect(self, tmp_path):
572570
file1 = tmp_path / "file1.csv"
@@ -587,8 +585,7 @@ def test_read_csv_list_invalid_path(self, tmp_path):
587585

588586
files = [str(file1), "not_valid_path", str(file3)]
589587
with pytest.raises(duckdb.IOException, match='No files found that match the pattern "not_valid_path"'):
590-
rel = con.read_csv(files)
591-
rel.fetchall()
588+
con.read_csv(files)
592589

593590
@pytest.mark.parametrize(
594591
"options",
@@ -672,15 +669,16 @@ def test_strict_mode(self, tmp_path):
672669
file1.write_text("one|two|three|four\n1|2|3|4\n1|2|3|4|5\n1|2|3|4\n")
673670

674671
con = duckdb.connect()
672+
rel = con.read_csv(
673+
str(file1),
674+
header=True,
675+
delimiter="|",
676+
columns={"a": "INTEGER", "b": "INTEGER", "c": "INTEGER", "d": "INTEGER"},
677+
auto_detect=False,
678+
)
675679
with pytest.raises(duckdb.InvalidInputException, match="CSV Error on Line"):
676-
rel = con.read_csv(
677-
str(file1),
678-
header=True,
679-
delimiter="|",
680-
columns={"a": "INTEGER", "b": "INTEGER", "c": "INTEGER", "d": "INTEGER"},
681-
auto_detect=False,
682-
)
683680
rel.fetchall()
681+
684682
rel = con.read_csv(
685683
str(file1),
686684
header=True,
@@ -734,8 +732,8 @@ def test_files_to_sniff_option(self, tmp_path):
734732

735733
file_path = tmp_path / "file*.csv"
736734
con = duckdb.connect()
735+
rel = con.read_csv(file_path, files_to_sniff=1)
737736
with pytest.raises(duckdb.ConversionException, match="Conversion Error"):
738-
rel = con.read_csv(file_path, files_to_sniff=1)
739737
rel.fetchall()
740738
rel = con.read_csv(file_path, files_to_sniff=-1)
741739
assert rel.fetchall() == [("2025-05-12", "baz"), ("bar", "baz")]

tests/fast/api/test_streaming_result.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ def test_fetch_one(self, duckdb_cursor):
1818
"SELECT CASE WHEN i < 10000 THEN i ELSE concat('hello', i::VARCHAR)::INT END FROM range(100000) t(i)"
1919
)
2020
with pytest.raises(duckdb.ConversionException):
21-
while True:
22-
tpl = res.fetchone()
23-
if tpl is None:
24-
break
21+
res.fetchone()
2522

2623
def test_fetch_many(self, duckdb_cursor):
2724
# fetch many
@@ -37,10 +34,7 @@ def test_fetch_many(self, duckdb_cursor):
3734
"SELECT CASE WHEN i < 10000 THEN i ELSE concat('hello', i::VARCHAR)::INT END FROM range(100000) t(i)"
3835
)
3936
with pytest.raises(duckdb.ConversionException):
40-
while True:
41-
tpl = res.fetchmany(10)
42-
if tpl is None:
43-
break
37+
res.fetchmany(10)
4438

4539
def test_record_batch_reader(self, duckdb_cursor):
4640
pytest.importorskip("pyarrow")

tests/fast/api/test_with_propagating_exceptions.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ class TestWithPropagatingExceptions:
77
def test_with(self):
88
# Should propagate exception raised in the 'with duckdb.connect() ..'
99
with pytest.raises(duckdb.ParserException, match="syntax error at or near *"), duckdb.connect() as con:
10-
print("before")
1110
con.execute("invalid")
12-
print("after")
1311

1412
# Does not raise an exception
1513
with duckdb.connect() as con:
16-
print("before")
1714
con.execute("select 1")
18-
print("after")

tests/fast/arrow/test_arrow_fetch_recordbatch.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,7 @@ def test_record_batch_query_error(self):
252252
duckdb_cursor.execute("CREATE table t as select 'foo' as a;")
253253
with pytest.raises(duckdb.ConversionException, match="Conversion Error"):
254254
# 'execute' materializes the result, causing the error directly
255-
query = duckdb_cursor.execute("SELECT cast(a as double) FROM t")
256-
record_batch_reader = query.fetch_record_batch(1024)
257-
record_batch_reader.read_next_batch()
255+
duckdb_cursor.execute("SELECT cast(a as double) FROM t")
258256

259257
def test_many_list_batches(self):
260258
conn = duckdb.connect()

tests/fast/arrow/test_arrow_types.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,3 @@ def test_invalid_union(self, duckdb_cursor):
4343
match="Attempted to convert a UNION with no fields to DuckDB which is not supported",
4444
):
4545
duckdb_cursor.register("invalid_union", arrow_table)
46-
47-
res = duckdb_cursor.sql("select * from invalid_union").fetchall()
48-
print(res)

tests/fast/arrow/test_timestamp_timezone.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def test_timestamp_timezone_overflow(self, duckdb_cursor):
3333
precisions = ["s", "ms"]
3434
current_time = 9223372036854775807
3535
for precision in precisions:
36+
arrow_table = generate_table(current_time, precision, "UTC")
3637
with pytest.raises(duckdb.ConversionException, match="Could not convert"):
37-
arrow_table = generate_table(current_time, precision, "UTC")
3838
duckdb.from_arrow(arrow_table).execute().fetchall()
3939

4040
def test_timestamp_tz_to_arrow(self, duckdb_cursor):

tests/fast/pandas/test_df_object_resolution.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,6 @@ def test_structs_of_different_sizes(self, pandas, duckdb_cursor):
418418
),
419419
):
420420
res = duckdb_cursor.execute("select $1", [malformed_struct])
421-
print(res)
422421

423422
@pytest.mark.parametrize("pandas", [NumpyPandas(), ArrowPandas()])
424423
def test_struct_key_conversion(self, pandas, duckdb_cursor):

0 commit comments

Comments
 (0)