Skip to content

Commit

Permalink
Fix inject parameters, update changelog, bump version
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Karkit <karkucik@gmail.com>
  • Loading branch information
ekiro committed Jul 27, 2018
1 parent 07b93ee commit 87753c7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,23 @@ Install `requirements.test.txt` and run `py.test` in main directory.

# Changelog


## 1.1.1 (2018-07-27)
- Fix bug with optional arguments for functions decorated with :code:`@inject`

## 1.1.0 (2018-07-26)
- Add configuration module
- Add application class and runner
- Add profiles
- Minor fixes

## 1.0.5 (2018-07-12)
@egg decorator can be used without function invocation
- `@egg` decorator can be used without function invocation

## 1.0.4 (2018-06-30)
Add support for python 3.7
Fix autodiscover sample
- Add support for python 3.7
- Fix autodiscover sample

## 1.0.0 (2018-06-15)

First stable release
- First stable release

4 changes: 4 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Contribute
Changelog
---------

1.1.1 (2018-07-27)
.....................
* Fix bug with optional arguments for functions decorated with :code:`@inject`

1.1.0 (2018-07-26)
.....................
* Add configuration module
Expand Down
9 changes: 3 additions & 6 deletions haps/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing import Any, Callable, Dict, List, Optional, Type, TypeVar, Union

from haps.config import Configuration
from haps.exceptions import (AlreadyConfigured, CallError, ConfigurationError,
from haps.exceptions import (AlreadyConfigured, ConfigurationError,
NotConfigured, UnknownDependency, UnknownScope)
from haps.scopes import Scope
from haps.scopes.instance import InstanceScope
Expand Down Expand Up @@ -105,7 +105,6 @@ def configure(config: List[Egg], subclass: 'Container' = None) -> None:
profiles = Configuration().get_var(PROFILES, tuple)
assert isinstance(profiles, (list, tuple))
profiles = tuple(profiles) + (None,)
print(profiles)

seen = set()
registered = set()
Expand Down Expand Up @@ -304,18 +303,16 @@ def __init__(self, my_dep: DepType) -> None:
injectables: Dict[str, Any] = {}
for name, param in sig.parameters.items():
type_ = param.annotation
if type_ is Signature.empty:
if name == 'self':
continue
else:
injectables[name] = type_

@wraps(fun)
def _inner(*args, **kwargs):
if len(args) > 1:
raise CallError('Cannot call this method with arguments ')
container = Container()
for n, t in injectables.items():
if name not in kwargs:
if n not in kwargs:
kwargs[n] = container.get_object(t)

return fun(*args, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def readme():

setup(
name='haps',
version='1.1.0',
version='1.1.1',
packages=find_packages(),
url='https://github.com/ekiro/haps',
license='MIT License',
Expand Down
21 changes: 21 additions & 0 deletions tests/test_di_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ def __init__(self, some_class_instance: some_class):
assert isinstance(some_instance.some_class_instance, some_class)


def test_inject_into_function_with_optional_args(some_class):
haps.Container.configure([
haps.Egg(some_class, some_class, None, some_class)
])

class NotRegistered:
pass

@haps.inject
def func(some_class_instance: some_class, no_annotation,
not_registered: NotRegistered):
return some_class_instance, no_annotation, not_registered

not_reg = NotRegistered()
sci, na, nr = func(no_annotation=5, not_registered=not_reg)

assert isinstance(sci, some_class)
assert na == 5
assert nr is not_reg


def test_not_existing_scope():
@haps.scope('custom')
class CustomScopedCls:
Expand Down

0 comments on commit 87753c7

Please sign in to comment.