Skip to content

Commit

Permalink
improve typing
Browse files Browse the repository at this point in the history
  • Loading branch information
FynnBe committed Dec 4, 2024
1 parent 0326f91 commit 0f91b95
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
5 changes: 3 additions & 2 deletions bioimageio/spec/_internal/common_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
from .io_utils import write_content_to_zip
from .node import Node
from .packaging_context import PackagingContext
from .root_url import RootHttpUrl
from .url import HttpUrl
from .utils import (
assert_all_params_set_explicitly,
Expand Down Expand Up @@ -343,12 +344,12 @@ def validation_summary(self) -> ValidationSummary:
assert self._validation_summary is not None, "access only after initialization"
return self._validation_summary

_root: Union[HttpUrl, DirectoryPath] = PrivateAttr(
_root: Union[RootHttpUrl, DirectoryPath, ZipPath] = PrivateAttr(
default_factory=lambda: validation_context_var.get().root
)

@property
def root(self) -> Union[HttpUrl, DirectoryPath]:
def root(self) -> Union[RootHttpUrl, DirectoryPath, ZipPath]:
return self._root

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion bioimageio/spec/_internal/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class HttpUrl(RootHttpUrl):
_exists: Optional[bool] = None

def _after_validator(self):
super()._after_validator()
self = super()._after_validator()
context = validation_context_var.get()
if (
context.perform_io_checks
Expand Down
4 changes: 2 additions & 2 deletions bioimageio/spec/_internal/validation_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pydantic import DirectoryPath

from ._settings import settings
from .io_basics import AbsoluteDirectory, FileName, Sha256
from .io_basics import FileName, Sha256
from .root_url import RootHttpUrl
from .warning_levels import WarningLevel

Expand All @@ -21,7 +21,7 @@ class ValidationContext:
init=False, default_factory=list
)

root: Union[RootHttpUrl, AbsoluteDirectory, ZipFile] = Path()
root: Union[RootHttpUrl, DirectoryPath, ZipFile] = Path()
"""url/directory serving as base to resolve any relative file paths"""

warning_level: WarningLevel = 50
Expand Down
17 changes: 10 additions & 7 deletions tests/test_internal/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest
import requests.exceptions
from requests_mock import Mocker as RequestsMocker

from bioimageio.spec._internal.validation_context import ValidationContext

Expand Down Expand Up @@ -32,11 +33,11 @@ def test_httpurl_valid(url: str):
("let's ignore this I guess??", 405),
],
)
def test_httpurl_mock_valid(text: str, status_code: int, requests_mock):
def test_httpurl_mock_valid(text: str, status_code: int, requests_mock: RequestsMocker):
from bioimageio.spec._internal.url import HttpUrl

url = "https://example.com"
requests_mock.get(url, text=text, status_code=status_code)
_ = requests_mock.get(url, text=text, status_code=status_code)
assert HttpUrl(url).exists()


Expand All @@ -48,12 +49,14 @@ def test_httpurl_mock_valid(text: str, status_code: int, requests_mock):
("just wrong", 199),
],
)
def test_httpurl_mock_invalid(text: str, status_code: int, requests_mock):
def test_httpurl_mock_invalid(
text: str, status_code: int, requests_mock: RequestsMocker
):
from bioimageio.spec._internal.url import HttpUrl

url = "https://example.com"
requests_mock.head(url, text=text, status_code=status_code)
requests_mock.get(url, text=text, status_code=status_code)
_ = requests_mock.head(url, text=text, status_code=status_code)
_ = requests_mock.get(url, text=text, status_code=status_code)
with ValidationContext(perform_io_checks=True):
with pytest.raises(ValueError):
_ = HttpUrl(url)
Expand All @@ -68,11 +71,11 @@ def test_httpurl_mock_invalid(text: str, status_code: int, requests_mock):
requests.exceptions.InvalidURL,
],
)
def test_httpurl_mock_exc(exc: Type[Exception], requests_mock):
def test_httpurl_mock_exc(exc: Type[Exception], requests_mock: RequestsMocker):
from bioimageio.spec._internal.url import HttpUrl

url = "https://example.com"
requests_mock.head(url, exc=exc)
_ = requests_mock.head(url, exc=exc)
with ValidationContext(perform_io_checks=True):
with pytest.raises(ValueError):
_ = HttpUrl(url)
Expand Down

0 comments on commit 0f91b95

Please sign in to comment.