-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
151 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
..highlight:: rst | ||
.. _injectable: | ||
injectable | ||
========== | ||
|
||
:decorator:`injectable` enables exposing injectable arguments in | ||
function parameters without worrying to initialize these dependencies | ||
latter if the caller didn't inject them. | ||
|
||
.. _usage: | ||
Usage | ||
----- | ||
|
||
Just annotate a function with :decorator:`injectable` and worry no more | ||
about initializing it's injectable dependencies when the caller do not | ||
pass them explicitly: | ||
|
||
.. code:: python | ||
class Printer: | ||
def print_something(self): | ||
print("Something") | ||
@injectable() | ||
def foo(*, printer: Printer): | ||
printer.print_something() | ||
foo() | ||
# Something | ||
.. _how-works: | ||
How does this work? | ||
~~~~~~~~~~~~~~~~~~~ | ||
:decorator:`injectable` uses type annotations to decide whether or not | ||
to inject the dependency. Some conditions may be observed: | ||
|
||
* Only Keyword-Only arguments can be injected: | ||
.. code:: python | ||
@injectable() | ||
def foo(not_injectable: MyClass, not_injectable_either: MyClass = None, | ||
*, injectable_kwarg: MyClass): | ||
... | ||
* If a default value is provided, the argument will **not** be injected: | ||
.. code:: python | ||
@injectable() | ||
def foo(*, injectable_kwarg: MyClass, not_injectable_kwarg: MyClass = None): | ||
... | ||
* The class must have a default constructor without arguments: | ||
.. code:: python | ||
class OkForInjection: | ||
def __init__(self, optional_arg=42): | ||
... | ||
class NotSuitableForInjection: | ||
def __init__(self, mandatory_arg): | ||
... | ||
Attempting to use a not suitable class for injection will result in a | ||
TypeError raised during initialization of the annotated function. | ||
|
||
.. _specify-injectables: | ||
Cherry picking arguments for injection | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
If no parameters are passed into :decorator:`injectable`, then it will consider every | ||
keyword-only argument that does not have a default value to be an injectable | ||
argument. This can be undesired, because situations like this can happen: | ||
|
||
.. code:: python | ||
@injectable() | ||
def foo(*, injectable_dependency: MyClass, not_injectable: ClassWithoutNoArgsContructor): | ||
... | ||
# This will raise a TypeError as parameter `not_injectable` cannot be injected | ||
This is solved by naming which arguments shall be injected: | ||
|
||
.. code:: python | ||
@injectable(['injectable_dependency']) | ||
def foo(*, injectable_dependency: MyClass, not_injectable: ClassWithoutNoArgsContructor): | ||
... | ||
# This will run just fine and only `injectable_dependecy` will be injected |
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,5 @@ | ||
from injectable.injectable import injectable | ||
|
||
__all__ = [ | ||
injectable, | ||
] |
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,2 @@ | ||
[metadata] | ||
description-file = README.rst |
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,13 @@ | ||
from setuptools import setup | ||
|
||
setup( | ||
name='injectable', | ||
version='0.1.0', | ||
packages=['tests', 'injectable'], | ||
url='https://github.com/allrod5/injectable', | ||
license='MIT', | ||
author='rodrigo', | ||
author_email='[email protected]', | ||
description='Cleanly expose injectable arguments in Python 3 functions', | ||
test_requires=['testfixtures', 'pytest'] | ||
) |
Empty file.
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,8 @@ | ||
import pytest | ||
from testfixtures import LogCapture | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def log_capture(): | ||
with LogCapture() as capture: | ||
yield capture |
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