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.3.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.3-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydres-0.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 6af78a8b70998aa98e0583168a5dd06357738e6b2fa0931b032ef58d481d2c79
MD5 3da2c658a7e4865126697393316e14f0
BLAKE2b-256 4df9846731489aefca9a139b7a2752c4c19dbfd2115f5421ffec4458be29cade

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydres-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 11fe83fcfb5c7da5f83b88fda6a12e12738f47a2c619ce813eb29e75d0920914
MD5 0527cae00dde55651b1cd86cbb7b2bf1
BLAKE2b-256 91bf43c2d6937d2333e192c8e22e22eed463092171b8c668fc1f338769b35c22

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