Skip to content

Commit cda3b33

Browse files
Feature/251 add ruff (#267)
* Bump black: 22.3.0 -> 24.4.2 & Apply new format rules Disable E704 for consistency between Flake8 and black * Replace black and flake8 with ruff. * Add blank line after rebase.
1 parent 896bea5 commit cda3b33

File tree

16 files changed

+44
-103
lines changed

16 files changed

+44
-103
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ repos:
33
- repo: https://github.com/pre-commit/pre-commit-hooks
44
rev: v2.4.0
55
hooks:
6-
- id: flake8
7-
# line too long and line before binary operator (black is ok with these)
8-
types:
9-
- python
106
- id: trailing-whitespace
117
- id: end-of-file-fixer
128
- id: check-yaml
139
args: ["--unsafe"]
1410
- id: check-added-large-files
15-
- repo: https://github.com/psf/black
16-
rev: 22.3.0
11+
- repo: https://github.com/charliermarsh/ruff-pre-commit
12+
rev: "v0.5.4" # Sync with pyproject.toml
1713
hooks:
18-
- id: black
14+
- id: ruff
15+
args: ["--fix"]
16+
- id: ruff-format

pins/boards.py

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,23 @@
2525

2626

2727
class IFileSystem(Protocol):
28-
2928
protocol: "str | list"
3029

31-
def ls(self, path: str) -> Sequence[str]:
32-
...
30+
def ls(self, path: str) -> Sequence[str]: ...
3331

34-
def put(self) -> None:
35-
...
32+
def put(self) -> None: ...
3633

37-
def open(self, path: str, mode: str, *args, **kwargs) -> IOBase:
38-
...
34+
def open(self, path: str, mode: str, *args, **kwargs) -> IOBase: ...
3935

40-
def get(self) -> None:
41-
...
36+
def get(self) -> None: ...
4237

43-
def exists(self, path: str, **kwargs) -> bool:
44-
...
38+
def exists(self, path: str, **kwargs) -> bool: ...
4539

46-
def mkdir(self, path, create_parents=True, **kwargs) -> None:
47-
...
40+
def mkdir(self, path, create_parents=True, **kwargs) -> None: ...
4841

49-
def rm(self, path, recursive=False, maxdepth=None) -> None:
50-
...
42+
def rm(self, path, recursive=False, maxdepth=None) -> None: ...
5143

52-
def info(self, path):
53-
...
44+
def info(self, path): ...
5445

5546

5647
class BaseBoard:
@@ -163,9 +154,7 @@ def pin_meta(self, name, version: str = None) -> Meta:
163154
path_meta = self.construct_path([*components, meta_name])
164155
f, local = self._open_pin_meta(path_meta)
165156

166-
meta = self.meta_factory.read_pin_yaml(
167-
f, pin_name, selected_version, local=local
168-
)
157+
meta = self.meta_factory.read_pin_yaml(f, pin_name, selected_version, local=local)
169158

170159
return meta
171160

@@ -237,7 +226,6 @@ def _pin_store(
237226
versioned: Optional[bool] = None,
238227
created: Optional[datetime] = None,
239228
) -> Meta:
240-
241229
if type == "feather":
242230
warn_deprecated(
243231
'Writing pin type "feather" is unsupported. Switching type to "arrow".'
@@ -299,9 +287,7 @@ def _pin_store(
299287
"but that directory already exists."
300288
)
301289

302-
inform(
303-
_log, f"Writing pin:\nName: {repr(pin_name)}\nVersion: {dst_version}"
304-
)
290+
inform(_log, f"Writing pin:\nName: {repr(pin_name)}\nVersion: {dst_version}")
305291

306292
res = self.fs.put(tmp_dir, dst_version_path, recursive=True)
307293

@@ -456,9 +442,7 @@ def pin_version_delete(self, name: str, version: str):
456442
pin_version_path = self.construct_path([pin_name, version])
457443
self.fs.rm(pin_version_path, recursive=True)
458444

459-
def pin_versions_prune(
460-
self, name, n: "int | None" = None, days: "int | None" = None
461-
):
445+
def pin_versions_prune(self, name, n: "int | None" = None, days: "int | None" = None):
462446
"""Delete old versions of a pin.
463447
464448
Parameters

pins/cache.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,7 @@ def _open(self, path, mode="rb", **kwargs):
190190
fn = self._check_file(path)
191191

192192
if fn is None:
193-
raise ValueError(
194-
f"Cached file should exist for path, but none found: {path}"
195-
)
193+
raise ValueError(f"Cached file should exist for path, but none found: {path}")
196194

197195
touch_access_time(fn)
198196

pins/constructors.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ def board_folder(path: str, versioned=True, allow_pickle_read=None):
152152
takes precedence.
153153
"""
154154

155-
return board(
156-
"file", path, versioned, cache=None, allow_pickle_read=allow_pickle_read
157-
)
155+
return board("file", path, versioned, cache=None, allow_pickle_read=allow_pickle_read)
158156

159157

160158
def board_temp(versioned=True, allow_pickle_read=None):
@@ -206,9 +204,7 @@ def board_local(versioned=True, allow_pickle_read=None):
206204
"""
207205
path = get_data_dir()
208206

209-
return board(
210-
"file", path, versioned, cache=None, allow_pickle_read=allow_pickle_read
211-
)
207+
return board("file", path, versioned, cache=None, allow_pickle_read=allow_pickle_read)
212208

213209

214210
def board_github(
@@ -425,9 +421,7 @@ def board_connect(
425421
server_url = os.environ.get("CONNECT_SERVER")
426422

427423
kwargs = dict(server_url=server_url, api_key=api_key)
428-
return board(
429-
"rsc", None, versioned, cache, allow_pickle_read, storage_options=kwargs
430-
)
424+
return board("rsc", None, versioned, cache, allow_pickle_read, storage_options=kwargs)
431425

432426

433427
board_rsconnect = board_connect
@@ -569,6 +563,4 @@ def board_azure(path, versioned=True, cache=DEFAULT, allow_pickle_read=None):
569563
"""
570564

571565
opts = {"use_listings_cache": False}
572-
return board(
573-
"abfs", path, versioned, cache, allow_pickle_read, storage_options=opts
574-
)
566+
return board("abfs", path, versioned, cache, allow_pickle_read, storage_options=opts)

pins/drivers.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def load_path(meta, path_to_version):
3131

3232
# file path creation ------------------------------------------------------
3333

34-
if type == "table":
34+
if type == "table": # noqa: E721 False Positive due to bug: https://github.com/rstudio/pins-python/issues/266
3535
# this type contains an rds and csv files named data.{ext}, so we match
3636
# R pins behavior and hardcode the name
3737
target_fname = "data.csv"
@@ -134,9 +134,7 @@ def load_data(
134134
raise NotImplementedError(f"No driver for type {meta.type}")
135135

136136

137-
def save_data(
138-
obj, fname, type=None, apply_suffix: bool = True
139-
) -> "str | Sequence[str]":
137+
def save_data(obj, fname, type=None, apply_suffix: bool = True) -> "str | Sequence[str]":
140138
# TODO: extensible saving with deferred importing
141139
# TODO: how to encode arguments to saving / loading drivers?
142140
# e.g. pandas index options

pins/meta.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ def create(
223223
created=None,
224224
user=None,
225225
) -> Meta:
226-
227226
if title is None:
228227
raise NotImplementedError("title arguments required")
229228
if isinstance(files, str):

pins/rsconnect/api.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ def get_users(
278278
asc_order: "bool | None" = None,
279279
walk_pages=True,
280280
) -> "Sequence[User] | Sequence[dict]":
281-
282281
params = {k: v for k, v in locals().items() if k != "self" if v is not None}
283282

284283
if walk_pages:
@@ -290,9 +289,7 @@ def get_users(
290289

291290
# content ----
292291

293-
def get_content(
294-
self, owner_guid: str = None, name: str = None
295-
) -> Sequence[Content]:
292+
def get_content(self, owner_guid: str = None, name: str = None) -> Sequence[Content]:
296293
params = self._get_params(locals())
297294

298295
results = self.query_v1("content", params=params)
@@ -355,7 +352,6 @@ def get_content_bundle(self, guid: str, id: int) -> Bundle:
355352
def get_content_bundle_archive(
356353
self, guid: str, id: str, f_obj: "str | IOBase"
357354
) -> None:
358-
359355
r = self.query_v1(
360356
f"content/{guid}/bundles/{id}/download", stream=True, return_request=True
361357
)

pins/rsconnect/fs.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ class PinBundleManifest:
4545
version: int = 1
4646
local: str = "en_US"
4747
platform: str = "3.5.1"
48-
metadata: PinBundleManifestMetadata = field(
49-
default_factory=PinBundleManifestMetadata
50-
)
48+
metadata: PinBundleManifestMetadata = field(default_factory=PinBundleManifestMetadata)
5149
packages: None = None
5250
files: list = field(default_factory=list)
5351
users: None = None
@@ -119,9 +117,7 @@ def __init__(self, server_url, **kwargs):
119117
self._user_name_cache = {}
120118
self._content_name_cache = {}
121119

122-
def ls(
123-
self, path, details=False, **kwargs
124-
) -> "Sequence[BaseEntity] | Sequence[str]":
120+
def ls(self, path, details=False, **kwargs) -> "Sequence[BaseEntity] | Sequence[str]":
125121
"""List contents of Rstudio Connect Server.
126122
127123
Parameters
@@ -219,9 +215,7 @@ def put(
219215
# Deploy bundle ----
220216

221217
if deploy:
222-
task = self.api.post_content_item_deploy(
223-
bundle["content_guid"], bundle["id"]
224-
)
218+
task = self.api.post_content_item_deploy(bundle["content_guid"], bundle["id"])
225219

226220
task = self.api.poll_tasks(task["task_id"])
227221
if task["code"] != 0 or not task["finished"]:
@@ -399,9 +393,7 @@ def _get_content_from_name(self, user_guid, content_name):
399393
if len(contents) == 0
400394
else RsConnectApiResultError
401395
)
402-
raise err(
403-
f"Expecting 1 content entry, but found {len(contents)}: {contents}"
404-
)
396+
raise err(f"Expecting 1 content entry, but found {len(contents)}: {contents}")
405397

406398
res = contents[0]
407399

pins/tests/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ def http_example_board_path():
4444
# yield backend.create_tmp_board(str(PATH_TO_EXAMPLE_BOARD.absolute())).board
4545
# backend.teardown()
4646
# TODO: could putting it in a publically available bucket folder
47-
return "https://raw.githubusercontent.com/machow/pins-python/main/pins/tests/pins-compat"
47+
return (
48+
"https://raw.githubusercontent.com/machow/pins-python/main/pins/tests/pins-compat"
49+
)
4850

4951

5052
@pytest.fixture

pins/tests/test_boards.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,22 @@ def test_board_validate_pin_name_root(board):
7171

7272

7373
def test_board_pin_write_default_title(board):
74-
7574
df = pd.DataFrame({"x": [1, 2, 3], "y": [4, 5, 6]})
7675
meta = board.pin_write(df, "df_csv", title=None, type="csv")
7776
assert meta.title == "df_csv: a pinned 3 x 2 DataFrame"
7877

7978

8079
def test_board_pin_write_prepare_pin(board, tmp_dir2):
81-
8280
df = pd.DataFrame({"x": [1, 2, 3], "y": [4, 5, 6]})
8381

84-
meta = board.prepare_pin_version(
85-
str(tmp_dir2), df, "df_csv", title=None, type="csv"
86-
)
82+
meta = board.prepare_pin_version(str(tmp_dir2), df, "df_csv", title=None, type="csv")
8783
assert meta.file == "df_csv.csv"
8884
assert (tmp_dir2 / "data.txt").exists()
8985
assert (tmp_dir2 / "df_csv.csv").exists()
9086
assert not (tmp_dir2 / "df_csv.csv").is_dir()
9187

9288

9389
def test_board_pin_write_roundtrip(board):
94-
9590
df = pd.DataFrame({"x": [1, 2, 3], "y": [4, 5, 6]})
9691

9792
assert not board.pin_exists("df_csv")
@@ -413,7 +408,6 @@ def test_board_pin_version_delete_latest(board, pin_name, pin_del):
413408

414409
@pytest.mark.parametrize("n", [1, 2])
415410
def test_board_pin_versions_prune_n(board, pin_prune, pin_name, n):
416-
417411
board.pin_versions_prune(pin_name, n=n)
418412
new_versions = board.pin_versions(pin_name, as_df=False)
419413

@@ -428,7 +422,6 @@ def test_board_pin_versions_prune_n(board, pin_prune, pin_name, n):
428422

429423
@pytest.mark.parametrize("days", [1, 2])
430424
def test_board_pin_versions_prune_days(board, pin_prune, pin_name, days):
431-
432425
# RStudio cannot handle days, since it involves pulling metadata
433426
if board.fs.protocol == "rsc":
434427
with pytest.raises(NotImplementedError):
@@ -482,7 +475,6 @@ def test_board_pin_search_name(board, df, search, matches):
482475

483476

484477
def test_board_base_pin_meta_cache_touch(tmp_dir2, df):
485-
486478
cache = fsspec.filesystem(
487479
"pinscache",
488480
target_protocol="file",
@@ -635,7 +627,6 @@ def test_board_rsc_pin_read_public(df, board_short):
635627

636628

637629
def test_board_manual_http_file_download():
638-
639630
path = "https://raw.githubusercontent.com/rstudio/pins-python"
640631
license_path = "main/LICENSE"
641632

0 commit comments

Comments
 (0)