|
18 | 18 | import traceback
|
19 | 19 | import warnings
|
20 | 20 |
|
| 21 | +if sys.version_info >= (3, 9): |
| 22 | + from typing import Annotated |
| 23 | +else: |
| 24 | + from typing_extensions import Annotated |
| 25 | + |
21 | 26 | from typing import Dict, List, NewType
|
22 | 27 |
|
23 | 28 | import pytest
|
@@ -1682,3 +1687,70 @@ def function1(a: int | str) -> None:
|
1682 | 1687 | pass
|
1683 | 1688 |
|
1684 | 1689 | assert get_bindings(function1) == {'a': Union[int, str]}
|
| 1690 | + |
| 1691 | + |
| 1692 | +# test for https://github.com/python-injector/injector/issues/217 |
| 1693 | +def test_annotated_instance_integration_works(): |
| 1694 | + UserID = Annotated[int, "user_id"] |
| 1695 | + |
| 1696 | + def configure(binder): |
| 1697 | + binder.bind(UserID, to=123) |
| 1698 | + |
| 1699 | + injector = Injector([configure]) |
| 1700 | + assert injector.get(UserID) == 123 |
| 1701 | + |
| 1702 | + |
| 1703 | +def test_annotated_class_integration_works(): |
| 1704 | + class Shape(abc.ABC): |
| 1705 | + pass |
| 1706 | + |
| 1707 | + class Circle(Shape): |
| 1708 | + pass |
| 1709 | + |
| 1710 | + first = Annotated[Shape, "first"] |
| 1711 | + |
| 1712 | + def configure(binder): |
| 1713 | + binder.bind(first, to=Circle) |
| 1714 | + |
| 1715 | + injector = Injector([configure]) |
| 1716 | + assert isinstance(injector.get(first), Circle) |
| 1717 | + |
| 1718 | + |
| 1719 | +def test_annotated_meta_separate_bindings(): |
| 1720 | + first = Annotated[int, "first"] |
| 1721 | + second = Annotated[int, "second"] |
| 1722 | + |
| 1723 | + def configure(binder): |
| 1724 | + binder.bind(first, to=123) |
| 1725 | + binder.bind(second, to=456) |
| 1726 | + |
| 1727 | + injector = Injector([configure]) |
| 1728 | + assert injector.get(first) == 123 |
| 1729 | + assert injector.get(second) == 456 |
| 1730 | + assert injector.get(first) != injector.get(second) |
| 1731 | + |
| 1732 | + |
| 1733 | +def test_annotated_origin_separate_bindings(): |
| 1734 | + UserID = Annotated[int, "user_id"] |
| 1735 | + |
| 1736 | + def configure(binder): |
| 1737 | + binder.bind(UserID, to=123) |
| 1738 | + binder.bind(int, to=456) |
| 1739 | + |
| 1740 | + injector = Injector([configure]) |
| 1741 | + assert injector.get(UserID) == 123 |
| 1742 | + assert injector.get(int) == 456 |
| 1743 | + assert injector.get(UserID) != injector.get(int) |
| 1744 | + |
| 1745 | + |
| 1746 | +def test_annotated_non_comparable_types(): |
| 1747 | + foo = Annotated[int, float("nan")] |
| 1748 | + bar = Annotated[int, object()] |
| 1749 | + |
| 1750 | + def configure(binder): |
| 1751 | + binder.bind(foo, to=123) |
| 1752 | + binder.bind(bar, to=456) |
| 1753 | + |
| 1754 | + injector = Injector([configure]) |
| 1755 | + assert injector.get(foo) == 123 |
| 1756 | + assert injector.get(bar) == 456 |
0 commit comments