mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-05-22 21:46:17 +03:00
Remove generic meta class from resource and async resource classes
This commit is contained in:
parent
48df949cd5
commit
006734d136
|
@ -3,5 +3,8 @@ source = src/dependency_injector
|
||||||
omit = tests/unit
|
omit = tests/unit
|
||||||
plugins = Cython.Coverage
|
plugins = Cython.Coverage
|
||||||
|
|
||||||
|
[report]
|
||||||
|
show_missing = true
|
||||||
|
|
||||||
[html]
|
[html]
|
||||||
directory=reports/unittests/
|
directory=reports/unittests/
|
||||||
|
|
|
@ -1,26 +1,13 @@
|
||||||
"""Resources module."""
|
"""Resources module."""
|
||||||
|
|
||||||
import abc
|
import abc
|
||||||
import sys
|
|
||||||
from typing import TypeVar, Generic
|
from typing import TypeVar, Generic
|
||||||
|
|
||||||
if sys.version_info < (3, 7):
|
|
||||||
from typing import GenericMeta
|
|
||||||
else:
|
|
||||||
class GenericMeta(type):
|
|
||||||
...
|
|
||||||
|
|
||||||
|
|
||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
|
|
||||||
|
|
||||||
class ResourceMeta(GenericMeta, abc.ABCMeta):
|
class Resource(Generic[T]):
|
||||||
def __getitem__(cls, item):
|
|
||||||
# Spike for Python 3.6
|
|
||||||
return cls(item)
|
|
||||||
|
|
||||||
|
|
||||||
class Resource(Generic[T], metaclass=ResourceMeta):
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def init(self, *args, **kwargs) -> T:
|
def init(self, *args, **kwargs) -> T:
|
||||||
|
@ -31,7 +18,7 @@ class Resource(Generic[T], metaclass=ResourceMeta):
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
class AsyncResource(Generic[T], metaclass=ResourceMeta):
|
class AsyncResource(Generic[T]):
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
async def init(self, *args, **kwargs) -> T:
|
async def init(self, *args, **kwargs) -> T:
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
"""Dependency injector resource provider unit tests."""
|
"""Dependency injector resource provider unit tests."""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from dependency_injector import containers, providers, resources, errors
|
from dependency_injector import containers, providers, resources, errors
|
||||||
|
|
||||||
|
@ -603,3 +603,30 @@ class AsyncResourceTest(AsyncTestCase):
|
||||||
|
|
||||||
self.assertIs(result2, resource)
|
self.assertIs(result2, resource)
|
||||||
self.assertEqual(_init.counter, 1)
|
self.assertEqual(_init.counter, 1)
|
||||||
|
|
||||||
|
|
||||||
|
class ResourceTypingTest(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_sync_generic_type(self):
|
||||||
|
class MyDependency:
|
||||||
|
...
|
||||||
|
|
||||||
|
class MyResource(resources.Resource[MyDependency]):
|
||||||
|
def init(self, *args: Any, **kwargs: Any) -> MyDependency:
|
||||||
|
return MyDependency()
|
||||||
|
|
||||||
|
def shutdown(self, resource: MyDependency) -> None: ...
|
||||||
|
|
||||||
|
self.assertTrue(issubclass(MyResource, resources.Resource))
|
||||||
|
|
||||||
|
def test_async_generic_type(self):
|
||||||
|
class MyDependency:
|
||||||
|
...
|
||||||
|
|
||||||
|
class MyAsyncResource(resources.AsyncResource[MyDependency]):
|
||||||
|
async def init(self, *args: Any, **kwargs: Any) -> MyDependency:
|
||||||
|
return MyDependency()
|
||||||
|
|
||||||
|
async def shutdown(self, resource: MyDependency) -> None: ...
|
||||||
|
|
||||||
|
self.assertTrue(issubclass(MyAsyncResource, resources.AsyncResource))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user