# -*- coding: utf-8 -*-
# Copyright (c) 2025-present tandemdude
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import annotations
__all__ = ["Compose"]
import inspect
import textwrap
import typing as t
if t.TYPE_CHECKING:
from collections.abc import Callable
from collections.abc import Iterable
import typing_extensions as t_ex
from typing_extensions import dataclass_transform
else:
dataclass_transform = lambda: lambda x: x # noqa: E731
_MARKER_ATTR: t.Final[str] = "__linkd_compose__"
_DEPS_ATTR: t.Final[str] = "__linkd_deps__"
def _is_compose_class(item: t.Any) -> t_ex.TypeIs[type[Compose]]: # type: ignore[reportUnusedFunction]
return hasattr(item, _MARKER_ATTR) and inspect.isclass(item)
class ComposeMeta(type):
"""Metaclass handling code generation for user-defined :obj:`~Compose` subclasses."""
@staticmethod
def _codegen_init(names: Iterable[str]) -> Callable[..., None]:
joined_names = ",".join(names)
lines = [
f"def __init__(self,{joined_names}):",
*(textwrap.indent(f"self.{n} = {n}", " " * 4) for n in names),
]
exec("\n".join(lines), {}, (generated_locals := {}))
return t.cast("Callable[..., None]", generated_locals["__init__"])
def __new__(cls, name: str, bases: tuple[type[t.Any]], attrs: dict[str, t.Any], **kwargs: t.Any) -> type[t.Any]:
if attrs["__module__"] == "linkd.compose" and attrs["__qualname__"] == "Compose":
return super().__new__(cls, name, bases, attrs)
annotations: dict[str, t.Any]
if "__annotations__" in attrs:
annotations = attrs["__annotations__"]
elif (func := attrs.get("__annotate__")) or (func := attrs.get("__annotate_func__")):
import annotationlib
annotations = annotationlib.call_annotate_function(func, annotationlib.Format.VALUE)
else:
raise RuntimeError("Could not resolve annotations for Compose subclass")
attrs["__slots__"] = tuple(annotations)
attrs["__init__"] = cls._codegen_init(annotations)
attrs[_MARKER_ATTR] = True
return super().__new__(cls, name, bases, attrs)
[docs]
@dataclass_transform()
class Compose(metaclass=ComposeMeta):
"""
Class allowing for "composing" of multiple dependencies into a single object, to help declutter
the signatures of functions that require a large number of dependencies.
If you have ever used msgspec or pydantic this will feel familiar. To use this feature, simply create a
subclass of this class and define fields for the dependencies you wish to use.
.. code-block:: python
class ComposedDependencies(linkd.Compose):
foo: FooDep
bar: BarDep
baz: BazDep
Then, in place of specifying ``foo``, ``bar`` and ``baz`` within the function signature, you can request
a single object of type ``ComposedDependencies``.
.. code-block:: python
async def function(deps: ComposedDependencies):
...
Linkd will automatically try to create an instance of your composed class with all the dependencies that
it requires. As with defining dependencies within the function signature, you can use fallback and `If` or `Try`
syntax within the composed class field annotations.
Methods, classmethods, staticmethods and properties defined on the subclass are preserved and accessible
on instances as normal.
.. warning::
None of the fields may contain a dependency on a composed class.
"""
__slots__ = ()