Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions .github/actions/code-style/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ description: Check code style
runs:
using: "composite"
steps:
- name: Isort
- name: Ruff
shell: bash
run: isort ./ --check

- name: Black
shell: bash
run: black ./ --check

- name: Flake8
shell: bash
run: flake8
run: |
ruff format --check
ruff check
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

[![CI](https://github.com/100nm/python-injection/actions/workflows/ci.yml/badge.svg)](https://github.com/100nm/python-injection)
[![PyPI](https://badge.fury.io/py/python-injection.svg)](https://pypi.org/project/python-injection/)
[![Black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

Fast and easy dependency injection framework.

Expand Down
11 changes: 3 additions & 8 deletions before_commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ set -e
title "POETRY"
poetry check

title "ISORT"
isort ./

title "BLACK"
black ./

title "FLAKE"
flake8
title "RUFF"
ruff format
ruff check --fix

title "PYTEST"
pytest --cov=./ --cov-report term-missing:skip-covered
8 changes: 4 additions & 4 deletions documentation/advanced-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,24 @@ The default priority is **`LOW`**.
Apply priority with `use` method:

```python
from injection import Module, ModulePriorities
from injection import Module, ModulePriority

module_1, module_2 = Module(), Module()

module_1.use(module_2, ModulePriorities.HIGH)
module_1.use(module_2, ModulePriority.HIGH)
```

Apply priority with `use_temporarily` method:

```python
with module_1.use_temporarily(module_2, ModulePriorities.HIGH):
with module_1.use_temporarily(module_2, ModulePriority.HIGH):
...
```

Change the priority of a used module:

```python
module_1.change_priority(module_2, ModulePriorities.LOW)
module_1.change_priority(module_2, ModulePriority.LOW)
```

### Understand `ModuleLockError`
Expand Down
4 changes: 2 additions & 2 deletions injection/_pkg.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from .core import Injectable, Module, ModulePriorities
from .core import Injectable, Module, ModulePriority

__all__ = (
"Injectable",
"Module",
"ModulePriorities",
"ModulePriority",
"default_module",
"get_instance",
"get_lazy_instance",
Expand Down
8 changes: 4 additions & 4 deletions injection/_pkg.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class Module:
Example: instance = ~lazy_instance
"""

def use(self, module: Module, priority: ModulePriorities = ...):
def use(self, module: Module, priority: ModulePriority = ...):
"""
Function for using another module. Using another module replaces the module's
dependencies with those of the module used. If the dependency is not found, it
Expand All @@ -143,13 +143,13 @@ class Module:
def use_temporarily(
self,
module: Module,
priority: ModulePriorities = ...,
priority: ModulePriority = ...,
) -> ContextManager | ContextDecorator:
"""
Context manager or decorator for temporary use of a module.
"""

def change_priority(self, module: Module, priority: ModulePriorities):
def change_priority(self, module: Module, priority: ModulePriority):
"""
Function for changing the priority of a module in use.
There are two priority values:
Expand All @@ -164,7 +164,7 @@ class Module:
"""

@final
class ModulePriorities(Enum):
class ModulePriority(Enum):
HIGH = ...
LOW = ...

Expand Down
25 changes: 15 additions & 10 deletions injection/core/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
NoInjectable,
)

__all__ = ("Injectable", "Module", "ModulePriorities")
__all__ = ("Injectable", "Module", "ModulePriority")

_logger = logging.getLogger(__name__)
_thread_lock = RLock()
Expand Down Expand Up @@ -117,7 +117,7 @@ def __str__(self) -> str:
@dataclass(frozen=True, slots=True)
class ModulePriorityUpdated(ModuleEvent):
module_updated: Module
priority: ModulePriorities
priority: ModulePriority

def __str__(self) -> str:
return (
Expand Down Expand Up @@ -269,7 +269,7 @@ def is_locked(self) -> bool:
def __injectables(self) -> frozenset[Injectable]:
return frozenset(self.__data.values())

def update(self, classes: Types, injectable: Injectable, override: bool):
def update(self, classes: Iterable[type], injectable: Injectable, override: bool):
values = MappingProxyType(
{origin: injectable for origin in get_origins(*classes)}
)
Expand Down Expand Up @@ -309,7 +309,7 @@ def __check_if_exists(self, *classes: type):
"""


class ModulePriorities(Enum):
class ModulePriority(Enum):
HIGH = auto()
LOW = auto()

Expand Down Expand Up @@ -437,14 +437,19 @@ def get_instance(self, cls: type[_T], none: bool = True) -> _T | None:
def get_lazy_instance(self, cls: type[_T]) -> Lazy[_T | None]:
return Lazy(lambda: self.get_instance(cls))

def update(self, classes: Types, injectable: Injectable, override: bool = False):
def update(
self,
classes: Iterable[type],
injectable: Injectable,
override: bool = False,
):
self.__container.update(classes, injectable, override)
return self

def use(
self,
module: Module,
priority: ModulePriorities = ModulePriorities.get_default(),
priority: ModulePriority = ModulePriority.get_default(),
):
if module is self:
raise ModuleError("Module can't be used by itself.")
Expand Down Expand Up @@ -475,13 +480,13 @@ def stop_using(self, module: Module):
def use_temporarily(
self,
module: Module,
priority: ModulePriorities = ModulePriorities.get_default(),
priority: ModulePriority = ModulePriority.get_default(),
) -> ContextManager | ContextDecorator:
self.use(module, priority)
yield
self.stop_using(module)

def change_priority(self, module: Module, priority: ModulePriorities):
def change_priority(self, module: Module, priority: ModulePriority):
event = ModulePriorityUpdated(self, module, priority)

with self.notify(event):
Expand Down Expand Up @@ -517,8 +522,8 @@ def __check_locking(self):
if self.is_locked:
raise ModuleLockError(f"`{self}` is locked.")

def __move_module(self, module: Module, priority: ModulePriorities):
last = priority == ModulePriorities.LOW
def __move_module(self, module: Module, priority: ModulePriority):
last = priority == ModulePriority.LOW

try:
self.__modules.move_to_end(module, last=last)
Expand Down
Loading