-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #681 from bioimage-io/improve_docs
Improve documentation
- Loading branch information
Showing
14 changed files
with
461 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
"""internal helper modules; do not use outside of bioimageio.spec!""" | ||
|
||
from ._settings import settings | ||
|
||
__all__ = ["settings"] | ||
from ._settings import settings as settings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import ( | ||
Any, | ||
Dict, | ||
Final, | ||
Generic, | ||
Type, | ||
Union, | ||
cast, | ||
) | ||
|
||
from typing_extensions import ( | ||
TypeVar, | ||
TypeVarTuple, | ||
Unpack, | ||
) | ||
|
||
from .common_nodes import StringNode | ||
from .node import Node | ||
from .utils import ( | ||
assert_all_params_set_explicitly, | ||
) | ||
|
||
SRC = TypeVar("SRC", bound=Union[Node, StringNode]) | ||
TGT = TypeVar("TGT", bound=Node) | ||
|
||
|
||
# converter without any additional args or kwargs: | ||
# class Converter(Generic[SRC, TGT], ABC): | ||
# # src: ClassVar[Type[SRC]] | ||
# # tgt: ClassVar[Type[TGT]] | ||
# # note: the above is not yet possible, see https://github.com/python/typing/discussions/1424 | ||
# # we therefore use an instance | ||
# def __init__(self, src: Type[SRC], tgt: Type[TGT], /): | ||
# super().__init__() | ||
# self.src: Final[Type[SRC]] = src | ||
# self.tgt: Final[Type[TGT]] = tgt | ||
|
||
# @abstractmethod | ||
# def _convert(self, src: SRC, tgt: "type[TGT | dict[str, Any]] ", /) -> "TGT | dict[str, Any]": | ||
# ... | ||
|
||
# def convert(self, source: SRC, /) -> TGT: | ||
# """convert `source` node | ||
|
||
# Args: | ||
# source: A bioimageio description node | ||
|
||
# Raises: | ||
# ValidationError: conversion failed | ||
# """ | ||
# data = self.convert_as_dict(source) | ||
# return assert_all_params_set_explicitly(self.tgt)(**data) | ||
|
||
# def convert_as_dict(self, source: SRC) -> Dict[str, Any]: | ||
# return cast(Dict[str, Any], self._convert(source, dict)) | ||
|
||
|
||
# A TypeVar bound to a TypedDict seemed like a good way to add converter kwargs: | ||
# ``` | ||
# class ConverterKwargs(TypedDict): | ||
# pass | ||
# KW = TypeVar("KW", bound=ConverterKwargs, default=ConverterKwargs) | ||
# ``` | ||
# sadly we cannot use a TypeVar bound to TypedDict and then unpack it in the Converter methods, | ||
# see https://github.com/python/typing/issues/1399 | ||
# Therefore we use a TypeVarTuple and positional only args instead | ||
# (We are avoiding ParamSpec for its ambiguity 'args vs kwargs') | ||
CArgs = TypeVarTuple("CArgs") | ||
|
||
|
||
class Converter(Generic[SRC, TGT, Unpack[CArgs]], ABC): | ||
# src: ClassVar[Type[SRC]] | ||
# tgt: ClassVar[Type[TGT]] | ||
# note: the above is not yet possible, see https://github.com/python/typing/discussions/1424 | ||
# we therefore use an instance | ||
def __init__(self, src: Type[SRC], tgt: Type[TGT], /): | ||
super().__init__() | ||
self.src: Final[Type[SRC]] = src | ||
self.tgt: Final[Type[TGT]] = tgt | ||
|
||
@abstractmethod | ||
def _convert( | ||
self, src: SRC, tgt: "type[TGT | dict[str, Any]]", /, *args: Unpack[CArgs] | ||
) -> "TGT | dict[str, Any]": ... | ||
|
||
# note: the following is not (yet) allowed, see https://github.com/python/typing/issues/1399 | ||
# we therefore use `kwargs` (and not `**kwargs`) | ||
# def convert(self, source: SRC, /, **kwargs: Unpack[KW]) -> TGT: | ||
def convert(self, source: SRC, /, *args: Unpack[CArgs]) -> TGT: | ||
"""convert `source` node | ||
Args: | ||
source: A bioimageio description node | ||
Raises: | ||
ValidationError: conversion failed | ||
""" | ||
data = self.convert_as_dict(source, *args) | ||
return assert_all_params_set_explicitly(self.tgt)(**data) | ||
|
||
def convert_as_dict(self, source: SRC, /, *args: Unpack[CArgs]) -> Dict[str, Any]: | ||
return cast(Dict[str, Any], self._convert(source, dict, *args)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.