-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from jarey/fix/support-protocols
Support protocols
- Loading branch information
Showing
7 changed files
with
433 additions
and
439 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
[tool:pytest] | ||
testpaths = tests | ||
timeout = 10 | ||
|
||
[black] | ||
line_length=120 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,42 @@ | ||
from abc import ABC | ||
from typing import Protocol, List | ||
|
||
from kink import inject, di | ||
|
||
class Repository: | ||
pass | ||
|
||
class Repository(Protocol): | ||
def __init__(self) -> None: | ||
pass | ||
|
||
|
||
@inject(alias=Repository, use_factory=True) | ||
class PerInstanceRepository(Repository): | ||
pass | ||
pass | ||
|
||
|
||
@inject(alias=Repository) | ||
class Repository1(Repository): | ||
pass | ||
|
||
|
||
@inject(alias=Repository) | ||
class Repository2(Repository): | ||
repo: Repository1 = di[Repository1] | ||
|
||
|
||
@inject | ||
class Service: | ||
def __init__(self, repository: Repository): | ||
pass | ||
|
||
def __init__(self, repositories: List[Repository]): | ||
self._repositories = repositories | ||
|
||
|
||
def test_can_inject_aliased_factory_services(): | ||
di[Service] | ||
service: Service = di[Service] | ||
assert service is not None | ||
assert service._repositories is not None | ||
assert len(service._repositories) == 3 | ||
repository: Repository = di[Repository2] | ||
assert repository.repo is not None | ||
repositories: List[Repository] = di[List[Repository]] | ||
assert repositories is not None | ||
assert len(repositories) == 3 |