Skip to content

Commit 4e9ec29

Browse files
authored
Merge pull request #6 from runemalm/feature/next-version
Release v1.0.0-alpha.8
2 parents 0d3e5b2 + 2f2f153 commit 4e9ec29

File tree

15 files changed

+450
-419
lines changed

15 files changed

+450
-419
lines changed

Makefile

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ HOME := $(shell echo ~)
1010
PWD := $(shell pwd)
1111
SRC := $(PWD)/src
1212
TESTS := $(PWD)/tests
13+
DOCS := $(PWD)/docs
1314

1415
# Load env file
1516
include env.make
@@ -29,7 +30,7 @@ help:
2930

3031
.PHONY: test
3132
test: ## run test suite
32-
PYTHONPATH=./src:./tests pipenv run pytest -n 1 $(TESTS)
33+
PYTHONPATH=$(SRC):$(TESTS) pipenv run pytest $(TESTS)
3334

3435
################################################################################
3536
# RELEASE
@@ -42,8 +43,10 @@ build: ## build the python package
4243
.PHONY: clean
4344
clean: ## clean the build
4445
python setup.py clean
45-
rm -rf build dist py_dependency_injection.egg-info
46-
find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -delete
46+
rm -rf build dist
47+
find . -type f -name '*.py[co]' -delete
48+
find . -type d -name __pycache__ -exec rm -rf {} +
49+
find . -type d -name '*.egg-info' -exec rm -rf {} +
4750

4851
.PHONY: bump_version
4952
bump_version: ## Bump the version
@@ -67,23 +70,25 @@ sphinx-html: ## build the sphinx html
6770

6871
.PHONY: sphinx-rebuild
6972
sphinx-rebuild: ## re-build the sphinx docs
70-
make -C docs clean && make -C docs html
73+
cd $(DOCS) && \
74+
pipenv run make clean && pipenv run make html
7175

7276
.PHONY: sphinx-autobuild
7377
sphinx-autobuild: ## activate autobuild of docs
74-
pipenv run sphinx-autobuild docs docs/_build/html --watch $(SRC)
78+
cd $(DOCS) && \
79+
pipenv run sphinx-autobuild . _build/html --watch $(SRC)
7580

7681
################################################################################
77-
# FORMAT & LINT
82+
# PRE-COMMIT HOOKS
7883
################################################################################
7984

8085
.PHONY: black
8186
black: ## run black auto-formatting
82-
pipenv run black $(SRC) $(TESTS) --line-length=88
87+
pipenv run black $(SRC) $(TESTS)
8388

8489
.PHONY: black-check
8590
black-check: ## check code don't violate black formatting rules
86-
pipenv run black --check $(SRC) --line-length=88
91+
pipenv run black --check $(SRC)
8792

8893
.PHONY: flake
8994
flake: ## lint code with flake
@@ -105,12 +110,12 @@ pre-commit-run: ## run the pre-commit hooks
105110
pipenv-install: ## setup the virtual environment
106111
pipenv install --dev
107112

108-
.PHONY: pipenv-packages-install
109-
pipenv-packages-install: ## install a package (uses PACKAGE)
113+
.PHONY: pipenv-install-package
114+
pipenv-install-package: ## install a package (uses PACKAGE)
110115
pipenv install $(PACKAGE)
111116

112-
.PHONY: pipenv-packages-install-dev
113-
pipenv-packages-install-dev: ## install a dev package (uses PACKAGE)
117+
.PHONY: pipenv-install-package-dev
118+
pipenv-install-package-dev: ## install a dev package (uses PACKAGE)
114119
pipenv install --dev $(PACKAGE)
115120

116121
.PHONY: pipenv-packages-graph
@@ -121,12 +126,12 @@ pipenv-packages-graph: ## Check installed packages
121126
pipenv-requirements-generate: ## Check a requirements.txt
122127
pipenv lock -r > requirements.txt
123128

124-
.PHONY: pipenv-venv-activate
125-
pipenv-venv-activate: ## Activate the virtual environment
129+
.PHONY: pipenv-shell
130+
pipenv-shell: ## Activate the virtual environment
126131
pipenv shell
127132

128-
.PHONY: pipenv-venv-path
129-
pipenv-venv-path: ## Show the path to the venv
133+
.PHONY: pipenv-venv
134+
pipenv-venv: ## Show the path to the venv
130135
pipenv --venv
131136

132137
.PHONY: pipenv-lock-and-install

README.md

Lines changed: 45 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@
44

55
# py-dependency-injection
66

7-
A dependency injection library for Python.
7+
A prototypical dependency injection library for Python.
88

99
## Features
1010

11-
- **Dependency Container:** Manage and resolve object dependencies with a flexible and easy-to-use container.
12-
- **Dependency Scopes:** Define different scopes for dependencies, allowing for fine-grained control over their lifecycle.
13-
- **Constructor Injection:** Inject dependencies into constructors, promoting cleaner and more modular code.
14-
- **Method Injection:** Inject dependencies into methods, enabling more flexible dependency management within class instances.
15-
- **Tags:** Register and resolve dependencies using tags, facilitating flexible and dynamic dependency management.
16-
- **Factory Registration:** Register dependencies using factory functions for dynamic instantiation.
17-
- **Instance Registration:** Register existing instances as dependencies, providing more control over object creation.
18-
- **Python Compatibility:** Compatible with Python versions 3.7 to 3.12, ensuring broad compatibility with existing and future Python projects.
11+
- **Scoped Registrations:** Define the lifetime of your dependencies as transient, scoped, or singleton.
12+
- **Constructor Injection:** Automatically resolve and inject dependencies when creating instances.
13+
- **Method Injection:** Inject dependencies into methods using a simple decorator.
14+
- **Factory Functions:** Register factory functions, classes, or lambdas to create dependencies.
15+
- **Instance Registration:** Register existing instances as dependencies.
16+
- **Tag-Based Registration and Resolution:** Organize and resolve dependencies based on tags.
17+
- **Multiple Containers:** Support for using multiple dependency containers.
1918

2019
## Compatibility
2120

22-
This library is compatible with the following Python versions:
21+
The library is compatible with the following Python versions:
2322

2423
- 3.7, 3.8, 3.9, 3.10, 3.11, 3.12
2524

@@ -29,138 +28,70 @@ This library is compatible with the following Python versions:
2928
$ pip install py-dependency-injection
3029
```
3130

32-
## Basic Usage
31+
## Quick Start
3332

34-
The following examples demonstrates how to use the library.
35-
36-
### Creating a Dependency Container
33+
Here's a quick example to get you started:
3734

3835
```python
39-
# Get the default dependency container
40-
dependency_container = DependencyContainer.get_instance()
41-
42-
# Create additional named containers if needed
43-
another_container = DependencyContainer.get_instance(name="another_container")
44-
```
36+
from dependency_injection.container import DependencyContainer
4537

46-
### Registering Dependencies with Scopes
47-
48-
```python
49-
# Register a transient dependency (a new instance every time)
50-
dependency_container.register_transient(Connection, PostgresConnection)
38+
# Define an abstract Connection
39+
class Connection:
40+
pass
5141

52-
# Register a scoped dependency (a new instance per scope)
53-
dependency_container.register_scoped(Connection, PostgresConnection, scope_name="http_request")
42+
# Define a specific implementation of the Connection
43+
class PostgresConnection(Connection):
44+
def connect(self):
45+
print("Connecting to PostgreSQL database...")
5446

55-
# Register a singleton dependency (a single instance for the container's lifetime)
56-
dependency_container.register_singleton(Connection, PostgresConnection)
57-
```
58-
59-
### Using Constructor Arguments
60-
61-
```python
62-
# Register a dependency with constructor arguments
63-
dependency_container.register_transient(
64-
Connection,
65-
PostgresConnection,
66-
constructor_args={"host": "localhost", "port": 5432}
67-
)
68-
```
69-
70-
### Using Factory Functions
71-
72-
```python
73-
# Define a factory function
74-
def create_connection(host: str, port: int) -> Connection:
75-
return PostgresConnection(host=host, port=port)
47+
# Define a repository that depends on some type of Connection
48+
class UserRepository:
49+
def __init__(self, connection: Connection):
50+
self._connection = connection
7651

77-
# Register the factory function
78-
dependency_container.register_factory(Connection, create_connection, factory_args={"host": "localhost", "port": 5432})
79-
```
52+
def fetch_users(self):
53+
self._connection.connect()
54+
print("Fetching users from the database...")
8055

81-
Besides functions, you can also use lambdas and class functions. Read more in the [documentation](https://py-dependency-injection.readthedocs.io/en/latest/userguide.html#using-factory-functions).
56+
# Get an instance of the (default) DependencyContainer
57+
container = DependencyContainer.get_instance()
8258

83-
### Registering and Using Instances
59+
# Register the specific connection type as a singleton instance
60+
container.register_singleton(Connection, PostgresConnection)
8461

85-
```python
86-
# Create an instance
87-
my_connection = PostgresConnection(host="localhost", port=5432)
62+
# Register UserRepository as a transient (new instance every time)
63+
container.register_transient(UserRepository)
8864

89-
# Register the instance
90-
dependency_container.register_instance(Connection, my_connection)
65+
# Resolve an instance of UserRepository, automatically injecting the required Connection
66+
user_repository = container.resolve(UserRepository)
9167

92-
# Resolve the instance
93-
resolved_connection = dependency_container.resolve(Connection)
94-
print(resolved_connection.host) # Output: localhost
68+
# Use the resolved user_repository to perform an operation
69+
user_repository.find_all()
9570
```
9671

97-
### Registering and Resolving with Tags
98-
99-
```python
100-
# Register dependencies with tags
101-
dependency_container.register_transient(Connection, PostgresConnection, tags={"Querying", "Startable"})
102-
dependency_container.register_scoped(BusConnection, KafkaBusConnection, tags={"Publishing", "Startable"})
103-
104-
# Resolve dependencies by tags
105-
startable_dependencies = dependency_container.resolve_all(tags={"Startable"})
106-
for dependency in startable_dependencies:
107-
dependency.start()
108-
```
109-
110-
### Using Constructor Injection
111-
112-
```python
113-
class OrderRepository:
114-
def __init__(self, connection: Connection):
115-
self.connection = connection
116-
117-
# Register dependencies
118-
dependency_container.register_transient(OrderRepository)
119-
dependency_container.register_singleton(Connection, PostgresConnection)
120-
121-
# Resolve the OrderRepository with injected dependencies
122-
repository = dependency_container.resolve(OrderRepository)
123-
print(repository.connection.__class__.__name__) # Output: PostgresConnection
124-
```
72+
## Documentation
12573

126-
### Using Method Injection
74+
For more advanced usage and examples, please visit our [readthedocs](https://py-dependency-injection.readthedocs.io/en/latest/) page.
12775

128-
```python
129-
class OrderController:
130-
@staticmethod
131-
@inject()
132-
def place_order(order: Order, repository: OrderRepository):
133-
order.status = "placed"
134-
repository.save(order)
135-
136-
# Register the dependency
137-
dependency_container.register_transient(OrderRepository)
138-
dependency_container.register_singleton(Connection, PostgresConnection)
139-
140-
# Use method injection to inject the dependency
141-
my_order = Order.create()
142-
OrderController.place_order(order=my_order) # The repository instance will be automatically injected
143-
```
76+
## License
14477

145-
You can also specify container and scope using the decorator arguments `container_name` and `scope_name`.
78+
`py-dependency-injection` is released under the GPL 3 license. See [LICENSE](LICENSE) for more details.
14679

147-
## Documentation
80+
## Source Code
14881

149-
For the latest documentation, visit [readthedocs](https://py-dependency-injection.readthedocs.io/en/latest/).
82+
You can find the source code for `py-dependency-injection` on [GitHub](https://github.com/runemalm/py-dependency-injection).
15083

151-
## Contribution
84+
## Release Notes
15285

153-
To contribute, create a pull request on the develop branch following the [git flow](https://nvie.com/posts/a-successful-git-branching-model/) branching model.
86+
### [1.0.0-alpha.8](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.8) (2024-06-07)
15487

155-
## Release Notes
88+
- Bug Fix: Fixed an issue in the dependency resolution logic where registered constructor arguments were not properly merged with automatically injected dependencies. This ensures that constructor arguments specified during registration are correctly combined with dependencies resolved by the container.
15689

15790
### [1.0.0-alpha.7](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.7) (2024-03-24)
15891

15992
- Documentation Update: Updated the documentation to provide clearer instructions and more comprehensive examples.
16093
- Preparing for Beta Release: Made necessary adjustments and refinements in preparation for the upcoming first beta release.
16194

162-
This release focuses on enhancing the documentation and making final preparations for the transition to the beta phase. If you have any more updates or need further assistance, feel free to reach out!
163-
16495
### [1.0.0-alpha.6](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.6) (2024-03-23)
16596

16697
- Factory Registration: Added support for registering dependencies using factory functions for dynamic instantiation.

docs/concepts.rst

Lines changed: 0 additions & 67 deletions
This file was deleted.

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
version = "1.0"
3636

3737
# The full version, including alpha/beta/rc tags
38-
release = "1.0.0-alpha.7"
38+
release = "1.0.0-alpha.8"
3939

4040

4141
# -- General configuration ---------------------------------------------------
@@ -84,6 +84,6 @@
8484
html_static_path = ["_static"]
8585

8686
intersphinx_mapping = {
87-
"python": ("https://docs.python.org/", None),
87+
"python": ("https://docs.python.org/3", None),
8888
"sqlalchemy": ("http://docs.sqlalchemy.org/en/latest/", None),
8989
}

0 commit comments

Comments
 (0)