Skip to content

Commit bf2d461

Browse files
authored
refactor: enable ruff PTH110 for Path.exists() (thousandbrainsproject#601)
1 parent ecef6ed commit bf2d461

8 files changed

Lines changed: 20 additions & 28 deletions

File tree

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ ignore = [
256256
"PT009", # PT009: Use a regular `assert` instead of unittest-style `assertEqual`
257257
"PT018", # PT018: Assertion should be broken down into multiple parts
258258
"PT027", # PT027: Use `pytest.raises` instead of unittest-style `assertRaises`
259-
"PTH110", # PTH110: `os.path.exists()` should be replaced by `Path.exists()`
260259
"PTH111", # PTH111: `os.path.expanduser()` should be replaced by `Path.expanduser()`
261260
"PTH119", # PTH119: `os.path.basename()` should be replaced by `Path.name`
262261
"PTH120", # PTH120: `os.path.dirname()` should be replaced by `Path.parent`

src/tbp/monty/frameworks/environments/two_d_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ def load_new_scene_data(self):
725725
current_rgb_path = self.data_path / f"rgb_{self.current_scene}.png"
726726
# Load rgb image
727727
wait_count = 0
728-
while not os.path.exists(current_rgb_path):
728+
while not current_rgb_path.exists():
729729
if wait_count % 10 == 0:
730730
# Print every 10 seconds
731731
print("Waiting for new RGBD data...")
@@ -743,7 +743,7 @@ def load_new_scene_data(self):
743743
height, width, _ = current_rgb_image.shape
744744

745745
# Load depth image
746-
while not os.path.exists(current_depth_path):
746+
while not current_depth_path.exists():
747747
print(f"Waiting for new depth data. Looking for {current_depth_path}")
748748
time.sleep(1)
749749
load_succeeded = False

src/tbp/monty/frameworks/models/evidence_sdr_matching.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
# https://opensource.org/licenses/MIT.
1010

1111
import logging
12-
import os
1312
import shutil
1413
from pathlib import Path
1514

@@ -50,7 +49,7 @@ def __init__(self, path):
5049
path = Path(path).expanduser()
5150

5251
# overwrite existing logs
53-
if os.path.exists(path):
52+
if path.exists():
5453
shutil.rmtree(path)
5554
path.mkdir(parents=True)
5655

src/tbp/monty/frameworks/run_parallel.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def mv_files(filenames: Iterable[Path], outdir: Path):
7171

7272
def cat_files(filenames, outfile):
7373
outfile = Path(outfile)
74-
if os.path.exists(outfile):
74+
if outfile.exists():
7575
print(f"Removing existing file before writing new one: {outfile}")
7676
outfile.unlink()
7777

@@ -96,7 +96,7 @@ def sample_params_to_init_args(params):
9696

9797

9898
def post_parallel_log_cleanup(filenames, outfile, cat_fn):
99-
existing_files = [f for f in filenames if os.path.exists(f)]
99+
existing_files = [f for f in map(Path, filenames) if f.exists()]
100100
if len(existing_files) == 0:
101101
return
102102

@@ -105,7 +105,7 @@ def post_parallel_log_cleanup(filenames, outfile, cat_fn):
105105

106106
# Remove json files
107107
for f in existing_files:
108-
Path(f).unlink(missing_ok=True)
108+
f.unlink(missing_ok=True)
109109

110110

111111
def post_parallel_profile_cleanup(parallel_dirs, base_dir, mode):
@@ -135,7 +135,7 @@ def post_parallel_profile_cleanup(parallel_dirs, base_dir, mode):
135135

136136
def move_reproducibility_data(base_dir, parallel_dirs):
137137
outdir = Path(base_dir) / "reproduce_episode_data"
138-
if os.path.exists(outdir):
138+
if outdir.exists():
139139
shutil.rmtree(outdir)
140140

141141
outdir.mkdir(parents=True)
@@ -649,7 +649,7 @@ def run_episodes_parallel(
649649
post_parallel_train(experiments, base_dir)
650650
if log_parallel_wandb:
651651
csv_path = base_dir / "train_stats.csv"
652-
if os.path.exists(csv_path):
652+
if csv_path.exists():
653653
train_stats = pd.read_csv(csv_path)
654654
train_table = wandb.Table(dataframe=train_stats)
655655
if run is not None:
@@ -660,7 +660,7 @@ def run_episodes_parallel(
660660
post_parallel_eval(experiments, base_dir)
661661
if log_parallel_wandb:
662662
csv_path = base_dir / "eval_stats.csv"
663-
if os.path.exists(csv_path):
663+
if csv_path.exists():
664664
eval_stats = pd.read_csv(csv_path)
665665
eval_table = wandb.Table(dataframe=eval_stats)
666666
run.log({"eval_stats": eval_table})

tools/github_readme_sync/export.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
# https://opensource.org/licenses/MIT.
1010

1111
import logging
12-
import os
1312
import shutil
1413
from pathlib import Path
1514

@@ -25,7 +24,7 @@ def export(output_dir: str, rdme: ReadMe):
2524
hierarchy = []
2625
categories = rdme.get_categories()
2726

28-
if os.path.exists(output_dir):
27+
if output_dir.exists():
2928
shutil.rmtree(output_dir)
3029

3130
output_dir.mkdir(exist_ok=True, parents=True)

tools/github_readme_sync/hierarchy.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def check_hierarchy_file(folder: str):
7474
hierarchy = []
7575

7676
hierarchy_file = folder / HIERARCHY_FILE
77-
if not os.path.exists(hierarchy_file):
77+
if not hierarchy_file.exists():
7878
logging.error(f"File {hierarchy_file} does not exist")
7979
sys.exit(1)
8080

@@ -135,7 +135,7 @@ def extract_slug(line: str):
135135

136136

137137
def sanity_check(path):
138-
if not os.path.exists(path):
138+
if not path.exists():
139139
return [f"File {path} does not exist"]
140140

141141
return check_links(path)
@@ -163,7 +163,7 @@ def check_links(path):
163163
f"{YELLOW} {len(table_matches)} tables{RESET}"
164164
)
165165

166-
current_dir = path.parent
166+
current_dir = path.resolve().parent
167167
errors = []
168168

169169
for match in table_matches:
@@ -172,29 +172,26 @@ def check_links(path):
172172
continue
173173

174174
path_to_check = current_dir / match
175-
path_to_check = os.path.normpath(path_to_check)
176-
if not os.path.exists(path_to_check):
175+
if not path_to_check.exists():
177176
errors.append(f" CSV {match} does not exist")
178177

179178
for match in md_link_matches:
180179
if match[1].startswith(("http://", "https://", "mailto:")):
181180
continue
182181

183182
path_to_check = current_dir / match[1].split("#")[0]
184-
path_to_check = os.path.normpath(path_to_check)
185183
if any(placeholder in match[1] for placeholder in IGNORE_DOCS):
186184
continue
187-
logging.debug(f"{GREEN} {path_to_check.split('/')[-1]}{RESET}")
188-
if not os.path.exists(path_to_check):
185+
logging.debug(f"{GREEN} {path_to_check.name}{RESET}")
186+
if not path_to_check.exists():
189187
errors.append(f" Linked {match[1]} does not exist")
190188

191189
for match in image_link_matches:
192190
path_to_check = current_dir / match.split("#")[0]
193-
path_to_check = os.path.normpath(path_to_check)
194191
if any(placeholder in match for placeholder in IGNORE_IMAGES):
195192
continue
196-
logging.debug(f"{CYAN} {path_to_check.split('/')[-1]}{RESET}")
197-
if not os.path.exists(path_to_check):
193+
logging.debug(f"{CYAN} {path_to_check.name}{RESET}")
194+
if not path_to_check.exists():
198195
errors.append(f" Image {path_to_check} does not exist")
199196

200197
if errors:

tools/github_readme_sync/tests/hierarchy_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import contextlib
1212
import http.server
13-
import os
1413
import socketserver
1514
import tempfile
1615
import threading
@@ -63,7 +62,7 @@ def test_create_hierarchy_file(self):
6362
create_hierarchy_file(self.test_dir, hierarchy_structure)
6463

6564
hierarchy_file_path = self.test_dir / HIERARCHY_FILE
66-
self.assertTrue(os.path.exists(hierarchy_file_path))
65+
self.assertTrue(hierarchy_file_path.exists())
6766

6867
with open(hierarchy_file_path) as f:
6968
content = f.read()

tools/github_readme_sync/upload.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
# https://opensource.org/licenses/MIT.
1010

1111
import logging
12-
import os
1312
from pathlib import Path
1413

1514
from tools.github_readme_sync.colors import BLUE, CYAN, GRAY, RESET, WHITE
@@ -125,7 +124,7 @@ def print_child(level: int, doc: dict, created: bool):
125124

126125
def load_doc(file_path: str, category_slug: str, child: dict):
127126
file_path = Path(file_path) / category_slug / f"{child['slug']}.md"
128-
if not os.path.exists(file_path):
127+
if not file_path.exists():
129128
raise ValueError(f"File {file_path} does not exist")
130129

131130
with open(file_path, encoding="utf-8") as file:

0 commit comments

Comments
 (0)