Skip to content

Commit a99d067

Browse files
ArmavicaricardoV94
authored andcommitted
Replace more os.path with pathlib
1 parent 1bdbddf commit a99d067

File tree

6 files changed

+18
-32
lines changed

6 files changed

+18
-32
lines changed

pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ line-length = 88
125125
exclude = ["doc/", "pytensor/_version.py"]
126126

127127
[tool.ruff.lint]
128-
select = ["C", "E", "F", "I", "UP", "W", "RUF", "PERF"]
128+
select = ["C", "E", "F", "I", "UP", "W", "RUF", "PERF", "PTH"]
129129
ignore = ["C408", "C901", "E501", "E741", "RUF012", "PERF203"]
130130

131131

@@ -136,6 +136,7 @@ lines-after-imports = 2
136136
# TODO: Get rid of these:
137137
"**/__init__.py" = ["F401", "E402", "F403"]
138138
"pytensor/tensor/linalg.py" = ["F403"]
139+
"pytensor/link/c/cmodule.py" = ["PTH"]
139140
# For the tests we skip because `pytest.importorskip` is used:
140141
"tests/link/jax/test_scalar.py" = ["E402"]
141142
"tests/link/jax/test_tensor_basic.py" = ["E402"]

pytensor/link/c/lazylinker_c.py

+4-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import errno
21
import logging
3-
import os
42
import sys
53
import warnings
64
from importlib import reload
@@ -43,23 +41,12 @@ def try_reload():
4341
# compile_str()) but if another lazylinker_ext does exist then it will be
4442
# imported and compile_str won't get called at all.
4543
location = config.compiledir / "lazylinker_ext"
46-
if not location.exists():
47-
try:
48-
# Try to make the location
49-
os.mkdir(location)
50-
except OSError as e:
51-
# If we get an error, verify that the error was # 17, the
52-
# path already exists, and that it is a directory Note: we
53-
# can't check if it exists before making it, because we
54-
# are not holding the lock right now, so we could race
55-
# another process and get error 17 if we lose the race
56-
assert e.errno == errno.EEXIST
57-
assert location.is_dir()
44+
location.mkdir(exist_ok=True)
5845

5946
init_file = location / "__init__.py"
6047
if not init_file.exists():
6148
try:
62-
with open(init_file, "w"):
49+
with init_file.open("w"):
6350
pass
6451
except OSError as e:
6552
if init_file.exists():
@@ -129,12 +116,7 @@ def try_reload():
129116
code = cfile.read_text("utf-8")
130117

131118
loc = config.compiledir / dirname
132-
if not loc.exists():
133-
try:
134-
os.mkdir(loc)
135-
except OSError as e:
136-
assert e.errno == errno.EEXIST
137-
assert loc.exists()
119+
loc.mkdir(exist_ok=True)
138120

139121
args = GCC_compiler.compile_args()
140122
GCC_compiler.compile_str(dirname, code, location=loc, preargs=args)
@@ -147,8 +129,7 @@ def try_reload():
147129
# imported at the same time: we need to make sure we do not
148130
# reload the now outdated __init__.pyc below.
149131
init_pyc = loc / "__init__.pyc"
150-
if init_pyc.is_file():
151-
os.remove(init_pyc)
132+
init_pyc.unlink(missing_ok=True)
152133

153134
try_import()
154135
try_reload()

pytensor/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def subprocess_Popen(command: str | list[str], **params):
156156
# with the default None values.
157157
stdin = None
158158
if "stdin" not in params:
159-
stdin = open(os.devnull)
159+
stdin = Path(os.devnull).open()
160160
params["stdin"] = stdin.fileno()
161161

162162
try:

tests/misc/test_pkl_utils.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import os
22
import shutil
3+
from pathlib import Path
34
from tempfile import mkdtemp
45

56
from pytensor.misc.pkl_utils import StripPickler
67
from pytensor.tensor.type import matrix
78

89

10+
# FIXME: this test looks weird
911
class TestStripPickler:
1012
def setup_method(self):
1113
# Work in a temporary directory to avoid cluttering the repository
12-
self.origdir = os.getcwd()
14+
self.origdir = Path.cwd()
1315
self.tmpdir = mkdtemp()
1416
os.chdir(self.tmpdir)
1517

@@ -20,9 +22,9 @@ def teardown_method(self):
2022
shutil.rmtree(self.tmpdir)
2123

2224
def test_basic(self):
23-
with open("test.pkl", "wb") as f:
25+
with Path("test.pkl").open("wb"):
2426
m = matrix()
2527
dest_pkl = "my_test.pkl"
26-
with open(dest_pkl, "wb") as f:
28+
with Path(dest_pkl).open("wb") as f:
2729
strip_pickler = StripPickler(f, protocol=-1)
2830
strip_pickler.dump(m)

tests/scan/test_basic.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import pickle
1414
import shutil
1515
import sys
16+
from pathlib import Path
1617
from tempfile import mkdtemp
1718

1819
import numpy as np
@@ -327,15 +328,15 @@ def f_pow2(x_tm1):
327328
[state, n_steps], output, updates=updates, allow_input_downcast=True
328329
)
329330

330-
origdir = os.getcwd()
331+
origdir = Path.cwd()
331332
tmpdir = None
332333
try:
333334
tmpdir = mkdtemp()
334335
os.chdir(tmpdir)
335336

336-
with open("tmp_scan_test_pickle.pkl", "wb") as f_out:
337+
with Path("tmp_scan_test_pickle.pkl").open("wb") as f_out:
337338
pickle.dump(_my_f, f_out, protocol=-1)
338-
with open("tmp_scan_test_pickle.pkl", "rb") as f_in:
339+
with Path("tmp_scan_test_pickle.pkl").open("rb") as f_in:
339340
my_f = pickle.load(f_in)
340341
finally:
341342
# Get back to the original dir, and delete the temporary one.

tests/tensor/utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
from copy import copy
33
from itertools import combinations
4+
from pathlib import Path
45
from tempfile import mkstemp
56

67
import numpy as np
@@ -442,7 +443,7 @@ def teardown_method(self):
442443
gc.collect()
443444
for f, fname in self.tmp_files:
444445
os.close(f)
445-
os.remove(fname)
446+
Path(fname).unlink()
446447

447448
@pytest.mark.skipif(skip, reason="Skipped")
448449
def test_good(self):

0 commit comments

Comments
 (0)