Coverage for src/python/ensembl/vep/annotators/registry.py: 100%
26 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-18 14:41 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-18 14:41 +0000
1# See the NOTICE file distributed with this work for additional information
2# regarding copyright ownership.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Annotator registry keyed by ``<domain>.<strategy>`` names."""
16from __future__ import annotations
18from collections.abc import Callable
19from typing import TYPE_CHECKING, Any, TypeVar, cast
21from ensembl.vep.annotators.base import Annotator
23__all__ = ["available_annotators", "get_annotator", "register_annotator"]
25# What a registered annotator produces.
26if TYPE_CHECKING:
27 AnnotatorResultT = TypeVar("AnnotatorResultT", default=Any)
28else:
29 AnnotatorResultT = TypeVar("AnnotatorResultT")
31AnnotatorFactory = Callable[..., Annotator[AnnotatorResultT]]
32"""Factory for annotators, parameterized by result type."""
34_ANNOTATOR_REGISTRY: dict[str, AnnotatorFactory[Any]] = {}
37def register_annotator(
38 name: str, factory: AnnotatorFactory[AnnotatorResultT] | None = None
39) -> (
40 AnnotatorFactory[AnnotatorResultT]
41 | Callable[[AnnotatorFactory[AnnotatorResultT]], AnnotatorFactory[AnnotatorResultT]]
42):
43 """Register an annotator factory under a name.
45 Args:
46 name (str): Registry name, e.g. ``"consequence.linear"``.
47 factory (AnnotatorFactory | None): Factory to register, or ``None`` when used as a
48 decorator.
50 Returns:
51 AnnotatorFactory | Callable[[AnnotatorFactory], AnnotatorFactory]: The registered factory
52 or a decorator that registers one.
53 """
55 def decorator(
56 registered_factory: AnnotatorFactory[AnnotatorResultT],
57 ) -> AnnotatorFactory[AnnotatorResultT]:
58 _ANNOTATOR_REGISTRY[name] = cast(AnnotatorFactory[Any], registered_factory)
59 return registered_factory
61 if factory is not None:
62 return decorator(factory)
63 return decorator
66def annotator_factory(name: str) -> AnnotatorFactory[AnnotatorResultT]:
67 """Return the registered factory without calling it.
69 Args:
70 name (str): Registry name, e.g. ``"consequence.linear"``.
72 Returns:
73 AnnotatorFactory: The registered factory, usually an annotator class.
75 Raises:
76 KeyError: If no annotator is registered under ``name``.
77 """
78 try:
79 return cast(AnnotatorFactory[AnnotatorResultT], _ANNOTATOR_REGISTRY[name])
80 except KeyError as error:
81 available = ", ".join(sorted(_ANNOTATOR_REGISTRY)) or "<none>"
82 raise KeyError(f"Unknown annotator '{name}'. Available annotators: {available}") from error
85def get_annotator(name: str, /, *args: object, **kwargs: object) -> Annotator[AnnotatorResultT]:
86 """Return a registered annotator instance.
88 Args:
89 name (str): Registry name, e.g. ``"consequence.linear"``.
90 *args (object): Positional arguments forwarded to the registered factory.
91 **kwargs (object): Keyword arguments forwarded to the registered factory.
93 Returns:
94 Annotator[AnnotatorResultT]: A new annotator instance.
96 Raises:
97 KeyError: If no annotator is registered under ``name``.
98 """
99 return annotator_factory(name)(*args, **kwargs)
102def available_annotators() -> tuple[str, ...]:
103 """Return the sorted set of registered annotator names."""
104 return tuple(sorted(_ANNOTATOR_REGISTRY))