-
Notifications
You must be signed in to change notification settings - Fork 58
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 #285 from reagento/develop
v1.4
- Loading branch information
Showing
134 changed files
with
4,374 additions
and
1,080 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ jobs: | |
- "3.10" | ||
- "3.11" | ||
- "3.12" | ||
- "3.13.0-rc.1" | ||
- "3.13" | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
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 |
---|---|---|
|
@@ -26,6 +26,7 @@ lint.ignore = [ | |
"UP038", | ||
"TCH001", | ||
"SIM103", | ||
"ISC003", | ||
] | ||
|
||
[lint.per-file-ignores] | ||
|
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Generic types | ||
===================== | ||
|
||
You can use dishka with TypeVars and Generic-classes. | ||
|
||
.. note:: | ||
|
||
Though generics are supported, there are some limitations: | ||
|
||
* You cannot use TypeVar bounded to a Generic type | ||
* Generic-decorators are only applied to concrete factories or factories with more narrow TypeVars | ||
|
||
Creating objects with @provide | ||
************************************ | ||
|
||
You can create generic factories, use ``type[T]`` to access resolved value of ``TypeVar``. Typevar can have bound or constraints, which are checked. | ||
For example, here we have a factory providing instances of generic class ``A``. Note that ``A[int]`` and ``A[bool]`` are different types and cached separately. | ||
|
||
.. literalinclude:: ./generics_examples/provide.py | ||
|
||
Decorating objects with @decorate | ||
*************************************** | ||
|
||
You can also make Generic decorator. Here it is used to decorate any type. | ||
|
||
.. literalinclude:: ./generics_examples/decorate.py |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from collections.abc import Iterator | ||
from typing import TypeVar | ||
|
||
from dishka import make_container, Provider, provide, Scope, decorate | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class MyProvider(Provider): | ||
scope = Scope.APP | ||
|
||
@provide | ||
def make_int(self) -> int: | ||
return 1 | ||
|
||
@provide | ||
def make_str(self) -> str: | ||
return "hello" | ||
|
||
@decorate | ||
def log(self, a: T, t: type[T]) -> Iterator[T]: | ||
print("Requested", t, "with value", a) | ||
yield a | ||
print("Requested release", a) | ||
|
||
|
||
container = make_container(MyProvider()) | ||
container.get(int) # Requested <class 'int'> with value 1 | ||
container.get(str) # Requested <class 'str'> with value hello | ||
container.close() | ||
# Requested release object hello | ||
# Requested release object 1 | ||
|
Oops, something went wrong.