Coverage for src/bioimageio/spec/_internal/node_converter.py: 100%
20 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 16:20 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 16:20 +0000
1from __future__ import annotations
3from abc import ABC, abstractmethod
4from typing import (
5 Any,
6 Final,
7 Generic,
8 cast,
9)
11from typing_extensions import (
12 TypeVar,
13 TypeVarTuple,
14 Unpack,
15)
17from .node import Node
18from .utils import (
19 assert_all_params_set_explicitly,
20)
21from .validated_string import ValidatedString
23SRC = TypeVar("SRC", bound=Node | ValidatedString)
24TGT = TypeVar("TGT", bound=Node)
27# converter without any additional args or kwargs:
28# class Converter(Generic[SRC, TGT], ABC):
29# # src: ClassVar[Type[SRC]]
30# # tgt: ClassVar[Type[TGT]]
31# # note: the above is not yet possible, see https://github.com/python/typing/discussions/1424
32# # we therefore use an instance
33# def __init__(self, src: Type[SRC], tgt: Type[TGT], /):
34# super().__init__()
35# self.src: Final[Type[SRC]] = src
36# self.tgt: Final[Type[TGT]] = tgt
38# @abstractmethod
39# def _convert(self, src: SRC, tgt: "type[TGT | dict[str, Any]] ", /) -> "TGT | dict[str, Any]":
40# ...
42# def convert(self, source: SRC, /) -> TGT:
43# """convert `source` node
45# Args:
46# source: A bioimageio description node
48# Raises:
49# ValidationError: conversion failed
50# """
51# data = self.convert_as_dict(source)
52# return assert_all_params_set_explicitly(self.tgt)(**data)
54# def convert_as_dict(self, source: SRC) -> Dict[str, Any]:
55# return cast(Dict[str, Any], self._convert(source, dict))
58# A TypeVar bound to a TypedDict seemed like a good way to add converter kwargs:
59# ```
60# class ConverterKwargs(TypedDict):
61# pass
62# KW = TypeVar("KW", bound=ConverterKwargs, default=ConverterKwargs)
63# ```
64# sadly we cannot use a TypeVar bound to TypedDict and then unpack it in the Converter methods,
65# see https://github.com/python/typing/issues/1399
66# Therefore we use a TypeVarTuple and positional only args instead
67# (We are avoiding ParamSpec for its ambiguity 'args vs kwargs')
68CArgs = TypeVarTuple("CArgs")
71class Converter(ABC, Generic[SRC, TGT, Unpack[CArgs]]):
72 # src: ClassVar[Type[SRC]]
73 # tgt: ClassVar[Type[TGT]]
74 # note: the above is not yet possible, see https://github.com/python/typing/discussions/1424
75 # we therefore use an instance
76 def __init__(self, src: type[SRC], tgt: type[TGT], /):
77 super().__init__()
78 self.src: Final[type[SRC]] = src
79 self.tgt: Final[type[TGT]] = tgt
81 @abstractmethod
82 def _convert(
83 self, src: SRC, tgt: type[TGT | dict[str, Any]], /, *args: Unpack[CArgs]
84 ) -> TGT | dict[str, Any]: ...
86 # note: the following is not (yet) allowed, see https://github.com/python/typing/issues/1399
87 # we therefore use `kwargs` (and not `**kwargs`)
88 # def convert(self, source: SRC, /, **kwargs: Unpack[KW]) -> TGT:
89 def convert(self, source: SRC, /, *args: Unpack[CArgs]) -> TGT:
90 """convert `source` node
92 Args:
93 source: A bioimageio description node
95 Raises:
96 ValidationError: conversion failed
97 """
98 data = self.convert_as_dict(source, *args)
99 return assert_all_params_set_explicitly(self.tgt)(**data)
101 def convert_as_dict(self, source: SRC, /, *args: Unpack[CArgs]) -> dict[str, Any]:
102 return cast(dict[str, Any], self._convert(source, dict, *args))