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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydres-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 bb14efd4c3dd067e15b2b428df909317452a48aa696f7428c7bc4c6976566178
MD5 58537cf6b676faeaa70b662bf7c84ea4
BLAKE2b-256 14f44eaa739946756e759842eacf9f040cdfc69fcf9fd7bcdeaea9742981f10c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydres-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 13d9ead57cb37047ffb76ae53e65f28b1eeb57bfe24c0fa46176eecd2371140b
MD5 aac4a43db9d52f6b3c0669024809ebc3
BLAKE2b-256 ff4f41f0ddce20082cc5f65e94986a7c721320b291d0123fbdf706043eb10bc8

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