Skip to content

Commit 694115f

Browse files
committed
Ruff PTH118: pathlib everywhere except for tests, which get a noqa for now
1 parent 74bc341 commit 694115f

37 files changed

+127
-128
lines changed

scripts/generate_connection_methods.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import json
22
import os
3+
from pathlib import Path
34

45
os.chdir(os.path.dirname(__file__))
56

6-
JSON_PATH = os.path.join("connection_methods.json")
7-
PYCONNECTION_SOURCE = os.path.join("..", "src", "pyconnection.cpp")
7+
JSON_PATH = "connection_methods.json"
8+
PYCONNECTION_SOURCE = Path("..") / "src" / "duckdb_py" / "pyconnection.cpp"
89

910
INITIALIZE_METHOD = (
1011
"static void InitializeConnectionMethods(py::class_<DuckDBPyConnection, shared_ptr<DuckDBPyConnection>> &m) {"

scripts/generate_connection_stubs.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import json
22
import os
3+
from pathlib import Path
34

45
os.chdir(os.path.dirname(__file__))
56

6-
JSON_PATH = os.path.join("connection_methods.json")
7-
DUCKDB_STUBS_FILE = os.path.join("..", "duckdb-stubs", "__init__.pyi")
7+
JSON_PATH = "connection_methods.json"
8+
DUCKDB_STUBS_FILE = Path("..") / "duckdb" / "__init__.pyi"
89

910
START_MARKER = " # START OF CONNECTION METHODS"
1011
END_MARKER = " # END OF CONNECTION METHODS"

scripts/generate_connection_wrapper_methods.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import json
22
import os
3+
from pathlib import Path
34

45
# Requires `python3 -m pip install cxxheaderparser pcpp`
56
from get_cpp_methods import ConnectionMethod, get_methods
67

78
os.chdir(os.path.dirname(__file__))
89

9-
JSON_PATH = os.path.join("connection_methods.json")
10-
DUCKDB_PYTHON_SOURCE = os.path.join("..", "duckdb_python.cpp")
10+
JSON_PATH = "connection_methods.json"
11+
DUCKDB_PYTHON_SOURCE = Path("..") / "src" / "duckdb_py" / "duckdb_python.cpp"
1112

1213
START_MARKER = "\t// START_OF_CONNECTION_METHODS"
1314
END_MARKER = "\t// END_OF_CONNECTION_METHODS"
@@ -31,9 +32,9 @@
3132
])
3233
"""
3334

34-
WRAPPER_JSON_PATH = os.path.join("connection_wrapper_methods.json")
35+
WRAPPER_JSON_PATH = "connection_wrapper_methods.json"
3536

36-
DUCKDB_INIT_FILE = os.path.join("..", "duckdb", "__init__.py")
37+
DUCKDB_INIT_FILE = Path("..") / "duckdb" / "__init__.py"
3738
INIT_PY_START = "# START OF CONNECTION WRAPPER"
3839
INIT_PY_END = "# END OF CONNECTION WRAPPER"
3940

scripts/generate_connection_wrapper_stubs.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import json
22
import os
3+
from pathlib import Path
34

45
os.chdir(os.path.dirname(__file__))
56

6-
JSON_PATH = os.path.join("connection_methods.json")
7-
WRAPPER_JSON_PATH = os.path.join("connection_wrapper_methods.json")
8-
DUCKDB_STUBS_FILE = os.path.join("..", "duckdb-stubs", "__init__.pyi")
7+
JSON_PATH = "connection_methods.json"
8+
WRAPPER_JSON_PATH = "connection_wrapper_methods.json"
9+
DUCKDB_STUBS_FILE = Path("..") / "duckdb" / "__init__.pyi"
910

1011
START_MARKER = "# START OF CONNECTION WRAPPER"
1112
END_MARKER = "# END OF CONNECTION WRAPPER"

scripts/generate_import_cache_cpp.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import json
22
import os
3+
from pathlib import Path
34

45
script_dir = os.path.dirname(__file__)
56

67
# Load existing JSON data from a file if it exists
78
json_data = {}
8-
json_cache_path = os.path.join(script_dir, "cache_data.json")
9+
json_cache_path = Path(script_dir) / "cache_data.json"
910
try:
1011
with open(json_cache_path) as file:
1112
json_data = json.load(file)
@@ -182,7 +183,7 @@ def to_string(self):
182183
for file in files:
183184
content = file.to_string()
184185
path = f"src/duckdb_py/include/duckdb_python/import_cache/modules/{file.file_name}"
185-
import_cache_path = os.path.join(script_dir, '..', path)
186+
import_cache_path = Path(script_dir) / ".." / path
186187
with open(import_cache_path, "w") as f:
187188
f.write(content)
188189

@@ -236,7 +237,7 @@ def get_root_modules(files: list[ModuleFile]):
236237
237238
"""
238239

239-
import_cache_path = os.path.join(script_dir, "..", "src/duckdb_py/include/duckdb_python/import_cache/python_import_cache.hpp")
240+
import_cache_path = Path(script_dir) / ".." / "src/duckdb_py/include/duckdb_python/import_cache/python_import_cache.hpp"
240241
with open(import_cache_path, "w") as f:
241242
f.write(import_cache_file)
242243

@@ -250,9 +251,8 @@ def get_module_file_path_includes(files: list[ModuleFile]):
250251

251252
module_includes = get_module_file_path_includes(files)
252253

253-
modules_header = os.path.join(
254-
script_dir, "..", "src/duckdb_py/include/duckdb_python/import_cache/python_import_cache_modules.hpp"
255-
)
254+
modules_header = Path(script_dir) / ".." / ("src/duckdb_py/include/duckdb_python/"
255+
"import_cache/python_import_cache_modules.hpp")
256256
with open(modules_header, "w") as f:
257257
f.write(module_includes)
258258

scripts/generate_import_cache_json.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import os
3+
from pathlib import Path
34
from typing import Union
45

56
script_dir = os.path.dirname(__file__)
@@ -155,7 +156,7 @@ def to_json(self):
155156

156157
# Load existing JSON data from a file if it exists
157158
existing_json_data = {}
158-
json_cache_path = os.path.join(script_dir, "cache_data.json")
159+
json_cache_path = Path(script_dir) / "cache_data.json"
159160
try:
160161
with open(json_cache_path) as file:
161162
existing_json_data = json.load(file)

scripts/get_cpp_methods.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Requires `python3 -m pip install cxxheaderparser pcpp`
22
import os
3+
from pathlib import Path
34
from typing import Callable
45

56
import cxxheaderparser.parser
@@ -51,16 +52,8 @@ def on_class_method(self, state, node):
5152

5253
def get_methods(class_name: str) -> dict[str, ConnectionMethod]:
5354
CLASSES = {
54-
"DuckDBPyConnection": os.path.join(
55-
scripts_folder,
56-
"..",
57-
"src",
58-
"include",
59-
"duckdb_python",
60-
"pyconnection",
61-
"pyconnection.hpp",
62-
),
63-
"DuckDBPyRelation": os.path.join(scripts_folder, "..", "src", "include", "duckdb_python", "pyrelation.hpp"),
55+
"DuckDBPyConnection": Path(scripts_folder) / ".." / "src" / "duckdb_py" / "include" / "duckdb_python" / "pyconnection" / "pyconnection.hpp", # noqa: E501
56+
"DuckDBPyRelation": Path(scripts_folder) / ".." / "src" / "duckdb_py" / "include" / "duckdb_python" / "pyrelation.hpp", # noqa: E501
6457
}
6558

6659
path = CLASSES[class_name]

tests/conftest.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import warnings
55
from importlib import import_module
66
from os.path import abspath, dirname, join, normpath
7+
from pathlib import Path
78
from typing import Any
89

910
import pytest
@@ -192,12 +193,12 @@ def require():
192193
def _require(extension_name, db_name="") -> duckdb.DuckDBPyConnection | None:
193194
# Paths to search for extensions
194195

195-
build = normpath(join(dirname(__file__), "../../../build/"))
196+
build = Path(__file__).parent.parent / "build"
196197
extension = "extension/*/*.duckdb_extension"
197198

198199
extension_search_patterns = [
199-
join(build, "release", extension),
200-
join(build, "debug", extension),
200+
build / "release" / extension,
201+
build / "debug" / extension,
201202
]
202203

203204
# DUCKDB_PYTHON_TEST_EXTENSION_PATH can be used to add a path for the extension test to search for extensions

tests/extensions/json/test_read_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
def TestFile(name):
99
import os
1010

11-
filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data", name)
11+
filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data", name) # noqa: PTH118
1212
return filename
1313

1414

tests/fast/adbc/test_adbc.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def test_connection_get_objects_filters(duck_conn):
9999

100100

101101
def test_commit(tmp_path):
102-
db = os.path.join(tmp_path, "tmp.db")
102+
db = os.path.join(tmp_path, "tmp.db") # noqa: PTH118
103103
if os.path.exists(db):
104104
os.remove(db)
105105
table = example_table()
@@ -239,7 +239,7 @@ def test_insertion(duck_conn):
239239

240240
def test_read(duck_conn):
241241
with duck_conn.cursor() as cursor:
242-
filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "data", "category.csv")
242+
filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "data", "category.csv") # noqa: PTH118
243243
cursor.execute(f"SELECT * FROM '{filename}'")
244244
assert cursor.fetch_arrow_table().to_pydict() == {
245245
"CATEGORY_ID": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
@@ -301,7 +301,7 @@ def test_large_chunk(tmp_path):
301301
# Create the table
302302
table = pyarrow.table([col1, col2, col3], names=["ints", "floats", "strings"])
303303

304-
db = os.path.join(tmp_path, "tmp.db")
304+
db = os.path.join(tmp_path, "tmp.db") # noqa: PTH118
305305
if os.path.exists(db):
306306
os.remove(db)
307307
db_kwargs = {"path": f"{db}"}
@@ -327,7 +327,7 @@ def test_dictionary_data(tmp_path):
327327

328328
# Wrap in a table
329329
table = pyarrow.table({"fruits": dict_array})
330-
db = os.path.join(tmp_path, "tmp.db")
330+
db = os.path.join(tmp_path, "tmp.db") # noqa: PTH118
331331
if os.path.exists(db):
332332
os.remove(db)
333333
db_kwargs = {"path": f"{db}"}
@@ -355,7 +355,7 @@ def test_ree_data(tmp_path):
355355

356356
table = pyarrow.table({"fruits": ree_array})
357357

358-
db = os.path.join(tmp_path, "tmp.db")
358+
db = os.path.join(tmp_path, "tmp.db") # noqa: PTH118
359359
if os.path.exists(db):
360360
os.remove(db)
361361
db_kwargs = {"path": f"{db}"}

0 commit comments

Comments
 (0)