Skip to content

Commit 3c0fbb7

Browse files
Bugfix/266 load path access type (#274)
* Fix bug in `pins.drivers.load_path` * Add tests. * Update pins/drivers.py Co-authored-by: Isabel Zimmerman <[email protected]> * Update pins/drivers.py Co-authored-by: Isabel Zimmerman <[email protected]> * Update pins/drivers.py Co-authored-by: Isabel Zimmerman <[email protected]> * Add missing `from __future__ import annotations` --------- Co-authored-by: Isabel Zimmerman <[email protected]>
1 parent 71c2044 commit 3c0fbb7

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

pins/drivers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ def _assert_is_pandas_df(x, file_type: str) -> None:
2525
def load_path(meta, path_to_version):
2626
# Check that only a single file name was given
2727
fnames = [meta.file] if isinstance(meta.file, str) else meta.file
28-
if len(fnames) > 1 and type in REQUIRES_SINGLE_FILE:
28+
29+
_type = meta.type
30+
31+
if len(fnames) > 1 and _type in REQUIRES_SINGLE_FILE:
2932
raise ValueError("Cannot load data when more than 1 file")
3033

3134
# file path creation ------------------------------------------------------
3235

33-
if type == "table": # noqa: E721 False Positive due to bug: https://github.com/rstudio/pins-python/issues/266
36+
if _type == "table":
3437
# this type contains an rds and csv files named data.{ext}, so we match
3538
# R pins behavior and hardcode the name
3639
target_fname = "data.csv"

pins/tests/test_drivers.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
from __future__ import annotations
2+
13
from pathlib import Path
24

35
import fsspec
46
import pandas as pd
57
import pytest
68

79
from pins.config import PINS_ENV_INSECURE_READ
8-
from pins.drivers import default_title, load_data, save_data
10+
from pins.drivers import default_title, load_data, load_path, save_data
911
from pins.errors import PinsInsecureReadError
1012
from pins.meta import MetaRaw
1113
from pins.tests.helpers import rm_env
@@ -159,3 +161,34 @@ def test_driver_apply_suffix_false(tmp_path: Path):
159161
res_fname = save_data(df, p_obj, type_, apply_suffix=False)
160162

161163
assert Path(res_fname).name == "some_df"
164+
165+
166+
class TestLoadFile:
167+
def test_multi_file_raises(self):
168+
class _MockMetaMultiFile:
169+
file: str | list[str] = ["a", "b"]
170+
type: str = "csv"
171+
172+
with pytest.raises(ValueError, match="Cannot load data when more than 1 file"):
173+
load_path(_MockMetaMultiFile(), None)
174+
175+
def test_str_file(self):
176+
class _MockMetaStrFile:
177+
file: str = "a"
178+
type: str = "csv"
179+
180+
assert load_path(_MockMetaStrFile(), None) == "a"
181+
182+
def test_table(self):
183+
class _MockMetaTable:
184+
file: str = "a"
185+
type: str = "table"
186+
187+
assert load_path(_MockMetaTable(), None) == "data.csv"
188+
189+
def test_version(self):
190+
class _MockMetaTable:
191+
file: str = "a"
192+
type: str = "csv"
193+
194+
assert load_path(_MockMetaTable(), "v1") == "v1/a"

0 commit comments

Comments
 (0)