Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: AlTosterino/Lidi
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.1.0
Choose a base ref
...
head repository: AlTosterino/Lidi
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref

Commits on Jun 1, 2023

  1. 💬 Update README.md

    AlTosterino committed Jun 1, 2023
    Copy the full SHA
    8fa4e67 View commit details
  2. 👷 Add coveralls

    AlTosterino committed Jun 1, 2023
    Copy the full SHA
    6114274 View commit details
  3. Copy the full SHA
    aa02c72 View commit details
  4. 💚 Fix test-ci command

    AlTosterino committed Jun 1, 2023
    Copy the full SHA
    19dbfbf View commit details
  5. 💚 Move to poetry add

    AlTosterino committed Jun 1, 2023
    Copy the full SHA
    708ac92 View commit details
  6. 💚 Fix resolving

    AlTosterino committed Jun 1, 2023
    Copy the full SHA
    65e2470 View commit details
  7. Copy the full SHA
    e30ad07 View commit details
  8. 🔖 Version 0.1.1

    AlTosterino committed Jun 1, 2023
    Copy the full SHA
    39c5a4e View commit details
  9. 💬 Add badges

    AlTosterino committed Jun 1, 2023
    Copy the full SHA
    a71e3b8 View commit details

Commits on Jun 16, 2023

  1. Copy the full SHA
    55ffb79 View commit details

Commits on Mar 29, 2024

  1. ⬆️ Upgrade deps

    AlTosterino committed Mar 29, 2024
    Copy the full SHA
    bbb40b1 View commit details
  2. Copy the full SHA
    cdda81a View commit details

Commits on Apr 5, 2024

  1. Copy the full SHA
    a395e2a View commit details
  2. 🔖 Version 0.2.0

    AlTosterino committed Apr 5, 2024
    Copy the full SHA
    0ffd949 View commit details
  3. Merge pull request #4 from AlTosterino/feature/minor-stuff

    Small refactor of existing code
    AlTosterino authored Apr 5, 2024
    Copy the full SHA
    bfa672e View commit details
  4. Copy the full SHA
    a50c4d5 View commit details
  5. Merge pull request #5 from AlTosterino/feature/make-sure-its-thread-safe

    Add tests for thread safety
    AlTosterino authored Apr 5, 2024
    Copy the full SHA
    0806fc9 View commit details

Commits on Jun 10, 2024

  1. ✨ Add resolve_attr

    AlTosterino committed Jun 10, 2024
    Copy the full SHA
    939525c View commit details

Commits on Jun 11, 2024

  1. 📝 Update README.md

    AlTosterino committed Jun 11, 2024
    Copy the full SHA
    b9016f2 View commit details
  2. Copy the full SHA
    18d10e8 View commit details
  3. Copy the full SHA
    3a2f60a View commit details
  4. 🎨 Reformat

    AlTosterino committed Jun 11, 2024
    Copy the full SHA
    4a268b0 View commit details
  5. 🎨 Reformat

    AlTosterino committed Jun 11, 2024
    Copy the full SHA
    a1501ba View commit details
  6. Copy the full SHA
    3672cd7 View commit details
  7. 📝 Add CHANGELOG.md

    AlTosterino committed Jun 11, 2024
    Copy the full SHA
    83021ac View commit details

Commits on Nov 18, 2024

  1. 🏷️ Add py.typed

    AlTosterino committed Nov 18, 2024
    Copy the full SHA
    93c7a4a View commit details
  2. Merge pull request #8 from AlTosterino/provide-type-info

    🏷️ Add `py.typed`
    AlTosterino authored Nov 18, 2024
    Copy the full SHA
    0682889 View commit details
  3. 🔖 Version 0.3.1

    AlTosterino committed Nov 18, 2024
    Copy the full SHA
    30d6f35 View commit details
11 changes: 8 additions & 3 deletions .github/workflows/push-test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test and publish ADR-Py
name: Test and publish lidipy
on: push
permissions:
contents: write
@@ -20,13 +20,18 @@ jobs:
cache: 'poetry'

- name: Install dependencies
run: poetry install
run: poetry install && poetry add coveralls -G dev

- name: Run lint
run: make lint-ci

- name: Run tests
run: make test
run: make test-ci

- name: Upload coverage
run: poetry run coveralls --service=github
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
publish:
name: Publish
needs: test
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.3.1] - 2024-11-18
### Added
- `py.typed` thanks to [@aardjon](https://github.com/aardjon)

## [0.3.0] - 2024-06-10
### Added
- Resolve dependencies as class attributes using: `resolve_attr`

### Changed
- Supported Python versions changed from `>3.11` to `>=3.11,<3.13`
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -5,6 +5,12 @@ LINT_PATHS = \
$(SRC_PATH) \
$(TESTS_PATH)

sync-deps:
poetry install

update-deps:
poetry update

lint:
poetry run black $(LINT_PATHS)
poetry run ruff check $(LINT_PATHS) --fix
@@ -16,4 +22,7 @@ lint-ci:
poetry run mypy $(LINT_PATHS)

test:
poetry run pytest
poetry run pytest

test-ci:
poetry run coverage run -m --source=lidipy pytest
90 changes: 73 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
[![Build status](https://github.com/altosterino/lidi/actions/workflows/push-test.yml/badge.svg)](https://github.com/altosterino/lidi/)
[![Coverage Status](https://coveralls.io/repos/github/AlTosterino/Lidi/badge.svg?branch=main)](https://coveralls.io/github/AlTosterino/Lidi?branch=main)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json)](https://github.com/charliermarsh/ruff)
[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)
[![Checked with mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](https://mypy-lang.org/)

# Lidi (LIghtweight Dependency Injector)

Lidi is a lightweight dependency injector designed to simplify dependency management in your Python projects.
@@ -29,20 +36,20 @@ class Child:
pass


lidipy = Lidi()
lidi = Lidi()
# bind instance
lidipy.bind(Parent, Child())
lidi.bind(Parent, Child())

# or bind a callable
lidipy.bind(Parent, Child)
lidi.bind(Parent, Child)
```

### Singleton Binding

If you want to bind a class as a singleton, you can pass the `singleton` parameter as `True` when calling the `bind()` method:

```python
lidipy.bind(Parent, Child(), singleton=True)
lidi.bind(Parent, Child(), singleton=True)
```

With singleton binding, the same instance of the class will be returned every time it is resolved.
@@ -52,21 +59,30 @@ With singleton binding, the same instance of the class will be returned every ti
To resolve a class and its dependencies, you can use the `resolve()` method of the Lidi class:

```python
instance = lidipy.resolve(Parent) # instance is Child()
instance = lidi.resolve(Parent) # instance is Child()
```

If the class was bound as a singleton, the same instance will be returned each time it is resolved.

### Deferred Resolution

Lidi also supports deferred resolution using the resolve_defer() method. This method returns a callable that, when invoked, resolves the class:
Lidi also supports deferred resolution using the `resolve_defer()` method. This method returns a callable that, when invoked, resolves the class:

```python
deferred_resolve = lidipy.resolve_defer(Parent)
deferred_resolve = lidi.resolve_defer(Parent)
instance = deferred_resolve()
```

Deferred resolution can be useful when you want to defer the instantiation of a class until it is actually needed.
### Class Attribute Resolution

Class attributes are often services or repositories. Lidi supports resolution of bindings using the `resolve_attr` method.

```python
lidi.bind(Parent, Child)

class Repository:
service: Parent = lidi.resolve_attr(Parent) # instance is Child()
```

### Handling Missing Bindings

@@ -77,7 +93,7 @@ You can handle this exception and provide appropriate error handling in your app
from lidipy import BindingMissing

try:
instance = lidipy.resolve(Mother)
instance = lidi.resolve(Mother)
except BindingMissing as e:
print(e) # Outputs: "Binding missing for type: Mother"
```
@@ -90,31 +106,71 @@ Lidi can be used seamlessly with Python's dataclasses. Here's an example of how
from dataclasses import dataclass
from lidipy import Lidi

lidipy = Lidi()
lidi = Lidi()


@dataclass
@dataclass(frozen=True)
class Config:
db_url: str


# Bind Config
lidi.bind(Config, Config(db_url="example.com:5432"))

@dataclass
class Database:
config: Config = lidipy.resolve(Config)
config: Config = lidi.resolve(Config) # or lidi.resolve_defer(Config)

def connect(self):
print(f"Connecting to database at {self.config.db_url}")


# Bind the dependencies
lidipy.bind(Config, Config(db_url="example.com:5432"))
lidipy.bind(Database, Database)
# Bind Database
lidi.bind(Database, Database)

# Resolve the dataclass with dependencies
database = lidipy.resolve(Database)
database = lidi.resolve(Database)
database.connect() # Output: Connecting to database at example.com:5432
```

## Dynamic binds on runtime

Lidi supports bindings change on runtime, here's an example:

```python
from dataclasses import dataclass, field
from lidipy import Lidi

lidi = Lidi()


@dataclass
class Config:
db_url: str


@dataclass
class Database:
config: Config = field(default_factory=lidi.resolve_defer(Config))

def connect(self):
print(f"Connecting to database at {self.config.db_url}")


# Bind the initial dependencies
lidi.bind(Config, Config(db_url="example.com:5432"))
lidi.bind(Database, Database)

# Initial resolve
database = lidi.resolve(Database)
database.connect() # Output: Connecting to database at example.com:5432

# Dynamically change binding
lidi.bind(Config, Config(db_url="other-example.com:5432"))

# Second resolve
database = lidi.resolve(Database)
database.connect() # Output: Connecting to database at other-example.com:5432
```
## Contributing

Contributions are welcome! If you find a bug or want to suggest an improvement, please open an issue or submit a pull request on the GitHub repository.
Loading