Skip to main content

A simple dependency resolver

Project description

pydres: Python Dependency Resolver

PyPI version

pydres (Python Dependency Resolver) is a lightweight and flexible library to instantiate Python classes with resolved dependencies. It supports hierarchical, string-based, and custom class annotations for dependency injection. With pydres, you can easily manage and override dependencies for your Python classes, even when dealing with complex dependency chains.


Features

  • Resolves both class and string-based type annotations.
  • Enables dependency overriding by parameter name or type.
  • Recursively instantiates classes based on their constructors.
  • Provides utility functions for inspecting and resolving dependencies.

Installation

Install pydres via pip:

pip install pydres

Or via Poetry:

poetry add pydres

Usage

Basic Dependency Resolution

from pydres import instantiate_with_dependencies

class B:
    def __init__(self, message: str = "default message"):
        self.message = message

class A:
    def __init__(self, b: B):
        self.b = b

# Automatically resolves and instantiates dependencies
instance = instantiate_with_dependencies(A)
print(instance.b.message)  # Output: default message

Overriding Dependencies

overrides = {
    "message": "custom message"
}

instance = instantiate_with_dependencies(B, overrides)
print(instance.message)  # Output: custom message

Resolving String-Based Annotations

class C:
    pass

class B:
    def __init__(self, c: "C"):
        self.c = c

instance = instantiate_with_dependencies(B)
print(isinstance(instance.c, C))  # Output: True

Handling Circular Dependencies

pydres handles most dependency chains but will raise an error for unresolved circular dependencies.

class J:
    def __init__(self, k: "K"):
        self.k = k

class K:
    def __init__(self, j: J):
        self.j = j

# This will raise a RecursionError
instantiate_with_dependencies(J)

Resolving Multiple Levels of Dependencies

class Inner:
    def __init__(self, x: int):
        self.x = x

class Outer:
    def __init__(self, inner: Inner):
        self.inner = inner

overrides = {"x": 42}
instance = instantiate_with_dependencies(Outer, overrides)
print(instance.inner.x)  # Output: 42

Utilities

pydres also includes helper methods to inspect and manipulate dependency structures.

  • get_first_custom_init(cls: Type[Any]) -> Optional[Callable[..., None]]: Get the first custom __init__ in the inheritance chain.
  • is_builtin_type(param_type: Any) -> bool: Check if a type is a built-in Python type.
  • is_custom_class(param_type: Any) -> bool: Check if a type is a user-defined class.
  • resolve_dependency(...): Resolve a single dependency using overrides or recursive instantiation.

Testing

pydres includes comprehensive tests with pytest.

To run the tests locally:

pytest --cov=pydres --cov-report=html

Contributing

Contributions are welcome! To contribute:

  1. Fork the repository.
  2. Create a feature branch.
  3. Submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Acknowledgments

Special thanks to the open-source community for providing the inspiration and tools necessary to build this project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydres-0.1.2.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydres-0.1.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file pydres-0.1.2.tar.gz.

File metadata

  • Download URL: pydres-0.1.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.5.0-1025-azure

File hashes

Hashes for pydres-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e6db285254242d1585a48e2fd6b27d82d4626f41f2f7217dc1d6b8545401bc5c
MD5 39808ae50ac171d526be5fc14ee3f4a8
BLAKE2b-256 0d41fe545223cdf18c4d7b13d7e23a9f1264e98be010b155756f307d86cbbcfa

See more details on using hashes here.

File details

Details for the file pydres-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pydres-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.5.0-1025-azure

File hashes

Hashes for pydres-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d01d6883741f0f053900cd32c61b2bf4bc9230a127a821b738007eb9a88a8aa5
MD5 c6ed8ea979df221be14d4028284b7488
BLAKE2b-256 60f215cf95f56f8056796b09b61305f8b26cddf789b2d24292b65d6ce69001e9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page