|
11 | 11 | """Functional tests for the "Injector" dependency injection framework.""" |
12 | 12 |
|
13 | 13 | from contextlib import contextmanager |
| 14 | +from dataclasses import dataclass |
14 | 15 | from typing import Any, NewType, Optional, Union |
15 | 16 | import abc |
16 | 17 | import sys |
@@ -1754,3 +1755,53 @@ def configure(binder): |
1754 | 1755 | injector = Injector([configure]) |
1755 | 1756 | assert injector.get(foo) == 123 |
1756 | 1757 | assert injector.get(bar) == 456 |
| 1758 | + |
| 1759 | + |
| 1760 | +def test_annotated_integration_with_annotated(): |
| 1761 | + UserID = Annotated[int, 'user_id'] |
| 1762 | + |
| 1763 | + @inject |
| 1764 | + class TestClass: |
| 1765 | + def __init__(self, user_id: UserID): |
| 1766 | + self.user_id = user_id |
| 1767 | + |
| 1768 | + def configure(binder): |
| 1769 | + binder.bind(UserID, to=123) |
| 1770 | + |
| 1771 | + injector = Injector([configure]) |
| 1772 | + |
| 1773 | + test_class = injector.get(TestClass) |
| 1774 | + assert test_class.user_id == 123 |
| 1775 | + |
| 1776 | + |
| 1777 | +def test_newtype_integration_with_annotated(): |
| 1778 | + UserID = NewType('UserID', int) |
| 1779 | + |
| 1780 | + @inject |
| 1781 | + class TestClass: |
| 1782 | + def __init__(self, user_id: UserID): |
| 1783 | + self.user_id = user_id |
| 1784 | + |
| 1785 | + def configure(binder): |
| 1786 | + binder.bind(UserID, to=123) |
| 1787 | + |
| 1788 | + injector = Injector([configure]) |
| 1789 | + |
| 1790 | + test_class = injector.get(TestClass) |
| 1791 | + assert test_class.user_id == 123 |
| 1792 | + |
| 1793 | + |
| 1794 | +def test_dataclass_annotated_parameter(): |
| 1795 | + Foo = Annotated[int, object()] |
| 1796 | + |
| 1797 | + def configure(binder): |
| 1798 | + binder.bind(Foo, to=123) |
| 1799 | + |
| 1800 | + @inject |
| 1801 | + @dataclass |
| 1802 | + class MyClass: |
| 1803 | + foo: Foo |
| 1804 | + |
| 1805 | + injector = Injector([configure]) |
| 1806 | + instance = injector.get(MyClass) |
| 1807 | + assert instance.foo == 123 |
0 commit comments