Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into furo-theme
Browse files Browse the repository at this point in the history
  • Loading branch information
jstasiak committed Jul 7, 2024
2 parents 2df9a48 + 8e61d47 commit ad625d5
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 26 deletions.
13 changes: 8 additions & 5 deletions docs/terminology.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,25 @@ Injection is the process of providing an instance of a type, to a method that us

Here is an example of injection on a module provider method, and on the constructor of a normal class::

from injector import inject
from typing import NewType

from injector import Binder, Module, inject, provider

Name = NewType("Name", str)
Description = NewType("Description", str)

class User:
@inject
def __init__(self, name: Name, description: Description):
self.name = name
self.description = description


class UserModule(Module):
def configure(self, binder):
def configure(self, binder: Binder):
binder.bind(User)


class UserAttributeModule(Module):
def configure(self, binder):
def configure(self, binder: Binder):
binder.bind(Name, to='Sherlock')

@provider
Expand Down
2 changes: 2 additions & 0 deletions injector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,8 @@ def _punch_through_alias(type_: Any) -> type:
and type(type_).__name__ == 'NewType'
):
return type_.__supertype__
elif isinstance(type_, _AnnotatedAlias) and getattr(type_, '__metadata__', None) is not None:
return type_.__origin__
else:
return type_

Expand Down
72 changes: 72 additions & 0 deletions injector_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
import traceback
import warnings

if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

from typing import Dict, List, NewType

import pytest
Expand Down Expand Up @@ -1682,3 +1687,70 @@ def function1(a: int | str) -> None:
pass

assert get_bindings(function1) == {'a': Union[int, str]}


# test for https://github.com/python-injector/injector/issues/217
def test_annotated_instance_integration_works():
UserID = Annotated[int, "user_id"]

def configure(binder):
binder.bind(UserID, to=123)

injector = Injector([configure])
assert injector.get(UserID) == 123


def test_annotated_class_integration_works():
class Shape(abc.ABC):
pass

class Circle(Shape):
pass

first = Annotated[Shape, "first"]

def configure(binder):
binder.bind(first, to=Circle)

injector = Injector([configure])
assert isinstance(injector.get(first), Circle)


def test_annotated_meta_separate_bindings():
first = Annotated[int, "first"]
second = Annotated[int, "second"]

def configure(binder):
binder.bind(first, to=123)
binder.bind(second, to=456)

injector = Injector([configure])
assert injector.get(first) == 123
assert injector.get(second) == 456
assert injector.get(first) != injector.get(second)


def test_annotated_origin_separate_bindings():
UserID = Annotated[int, "user_id"]

def configure(binder):
binder.bind(UserID, to=123)
binder.bind(int, to=456)

injector = Injector([configure])
assert injector.get(UserID) == 123
assert injector.get(int) == 456
assert injector.get(UserID) != injector.get(int)


def test_annotated_non_comparable_types():
foo = Annotated[int, float("nan")]
bar = Annotated[int, object()]

def configure(binder):
binder.bind(foo, to=123)
binder.bind(bar, to=456)

injector = Injector([configure])
assert injector.get(foo) == 123
assert injector.get(bar) == 456
16 changes: 4 additions & 12 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
aiohttp==3.9.1
aiosignal==1.3.1
async-timeout==4.0.3
attrs==23.1.0
black==23.3.0;implementation_name=="cpython"
black==24.3.0 ; implementation_name == "cpython"
build==1.0.3
check-manifest==0.49
click==8.1.7
coverage==7.3.2
coverage[toml]==7.3.2
exceptiongroup==1.2.0
frozenlist==1.4.0
idna==3.6
importlib-metadata==7.0.0
iniconfig==2.0.0
multidict==6.0.4
mypy==1.7.1;implementation_name=="cpython"
mypy==1.7.1 ; implementation_name == "cpython"
mypy-extensions==1.0.0
packaging==23.2
pathspec==0.12.1
Expand All @@ -23,6 +16,5 @@ pyproject-hooks==1.0.0
pytest==7.4.3
pytest-cov==4.1.0
tomli==2.0.1
typing-extensions==4.9.0
yarl==1.9.4
typing-extensions==4.9.0 ; python_version < "3.9"
zipp==3.17.0
18 changes: 9 additions & 9 deletions requirements-docs.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
alabaster==0.7.13
Babel==2.14.0
beautifulsoup4==4.12.2
certifi==2023.11.17
beautifulsoup4==4.12.3
certifi==2024.7.4
charset-normalizer==3.3.2
docutils==0.20.1
furo==2023.9.10
idna==3.6
furo==2024.5.6
idna==3.7
imagesize==1.4.1
importlib-metadata==7.0.0
Jinja2==3.1.2
Jinja2==3.1.4
MarkupSafe==2.1.3
packaging==23.2
pygments==2.17.2
Pygments==2.17.2
pytz==2023.3.post1
requests==2.31.0
requests==2.32.2
snowballstemmer==2.2.0
soupsieve==2.5
sphinx==7.1.2
Sphinx==7.1.2
sphinx-basic-ng==1.0.0b2
sphinxcontrib-applehelp==1.0.4
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==2.0.1
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
urllib3==2.1.0
urllib3==2.2.2
zipp==3.17.0

0 comments on commit ad625d5

Please sign in to comment.