Skip to content

Commit 2e1584b

Browse files
committed
allow absolute file paths
1 parent 8f72763 commit 2e1584b

File tree

4 files changed

+44
-17
lines changed

4 files changed

+44
-17
lines changed

bioimageio/spec/_internal/types/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
from typing_extensions import Annotated
99

1010
from bioimageio.spec._internal.constants import DOI_REGEX, SI_UNIT_REGEX
11+
from bioimageio.spec._internal.types._file_source import FileSource as FileSource
12+
from bioimageio.spec._internal.types._file_source import RelativeFilePath as RelativeFilePath
1113
from bioimageio.spec._internal.types._generated_spdx_license_type import DeprecatedLicenseId as DeprecatedLicenseId
1214
from bioimageio.spec._internal.types._generated_spdx_license_type import LicenseId as LicenseId
13-
from bioimageio.spec._internal.types._relative_path import FileSource as FileSource
14-
from bioimageio.spec._internal.types._relative_path import RelativeFilePath as RelativeFilePath
1515
from bioimageio.spec._internal.types._version import Version as Version
1616
from bioimageio.spec._internal.types.field_validation import (
1717
AfterValidator,

bioimageio/spec/_internal/types/_relative_path.py renamed to bioimageio/spec/_internal/types/_file_source.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,34 @@
55
from typing import Any, Union
66
from urllib.parse import urljoin
77

8-
from pydantic import AnyUrl, DirectoryPath, FilePath, GetCoreSchemaHandler, HttpUrl, ValidationInfo
8+
from annotated_types import Predicate
9+
from pydantic import (
10+
AnyUrl,
11+
DirectoryPath,
12+
FilePath,
13+
GetCoreSchemaHandler,
14+
HttpUrl,
15+
ValidationInfo,
16+
)
917
from pydantic_core import core_schema
18+
from typing_extensions import Annotated
1019

1120

1221
class RelativePath:
1322
path: PurePosixPath
1423

1524
def __init__(self, path: Union[str, Path, RelativePath]) -> None:
1625
super().__init__()
17-
self.path = (
18-
path.path
19-
if isinstance(path, RelativePath)
20-
else PurePosixPath(path.as_posix())
21-
if isinstance(path, Path)
22-
else PurePosixPath(Path(path).as_posix())
23-
)
26+
if isinstance(path, RelativePath):
27+
self.path = path.path
28+
else:
29+
if not isinstance(path, Path):
30+
path = Path(path)
31+
32+
if path.is_absolute():
33+
raise ValueError(f"{path} is an absolute path.")
34+
35+
self.path = PurePosixPath(path.as_posix())
2436

2537
@property
2638
def __members(self):
@@ -68,6 +80,9 @@ def _validate(cls, value: Union[pathlib.Path, str], info: ValidationInfo):
6880
raise ValueError(f"{value} looks like a URL, not a relative path")
6981

7082
ret = cls(value)
83+
if ret.path.is_absolute():
84+
raise ValueError(f"{value} is an absolute path.")
85+
7186
root = (info.context or {}).get("root")
7287
if root is not None:
7388
ret._check_exists(root)
@@ -87,4 +102,6 @@ def _check_exists(self, root: Union[DirectoryPath, AnyUrl]) -> None:
87102
raise ValueError(f"{p} does not point to an existing directory")
88103

89104

90-
FileSource = Union[HttpUrl, RelativeFilePath] # todo: move to types/__init__
105+
AbsoluteFilePath = Annotated[FilePath, Predicate(Path.is_absolute)]
106+
107+
FileSource = Union[HttpUrl, AbsoluteFilePath, RelativeFilePath]

bioimageio/spec/_internal/types/field_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from bioimageio.spec._internal.constants import SLOTS
1717

18-
from ._relative_path import FileSource, RelativePath
18+
from ._file_source import FileSource, RelativePath
1919

2020

2121
@dataclasses.dataclass(frozen=True, **SLOTS)

tests/test_shared/test_types.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,27 @@
55
import pytest
66

77
from bioimageio.spec._internal.types import Datetime, SiUnit
8-
from bioimageio.spec._internal.types.field_validation import RelativePath
8+
from bioimageio.spec._internal.types._file_source import RelativePath
99
from tests.utils import check_type
1010

1111

12-
def test_relative_path():
13-
p = RelativePath(__file__)
14-
p2 = RelativePath(Path(__file__))
12+
@pytest.mark.parametrize("path", ["a.test", "there/b", "./here/or/there/c"])
13+
def test_relative_path(path: str):
14+
p = RelativePath(path)
15+
p2 = RelativePath(Path(path))
1516
assert p == p2
16-
p3 = RelativePath(Path(__file__).as_posix())
17+
p3 = RelativePath(Path(path).as_posix())
1718
assert p == p3
1819

20+
with pytest.raises(ValueError):
21+
_ = RelativePath(Path(path).absolute())
22+
23+
with pytest.raises(ValueError):
24+
_ = RelativePath(str(Path(path).absolute()))
25+
26+
with pytest.raises(ValueError):
27+
_ = RelativePath(Path(path).absolute().as_posix())
28+
1929

2030
@pytest.mark.parametrize("value", ["lx·s", "kg/m^2·s^-2"])
2131
def test_si_unit(value: str):

0 commit comments

Comments
 (0)