python-dependency-injector icon indicating copy to clipboard operation
python-dependency-injector copied to clipboard

Subclassed generic (Async)Resource fails py3.8.9

Open EdwardBlair opened this issue 3 years ago • 2 comments

Given [email protected]

# my_module.py
from typing import Any
from dependency_injector import resources

class MyDep:
    ...

class MyDepAsyncResource(resources.AsyncResource[MyDep]):
    async def init(self, *args: Any, **kwargs: Any) -> MyDep:
        return MyDep()

    async def shutdown(self, resource: MyDep) -> None:
        ...
# oh_no.py
from my_module import MyDepAsyncResource

Fails to run on python 3.8.9 with

$python oh_no.py
oh_no.py:1: in <module>
    from my_module import MyDepAsyncResource
my_module.py:7: in <module>
    class MyDepAsyncResource(resources.AsyncResource[MyDep]):
../../../Library/Caches/pypoetry/virtualenvs/xxxxxxx-py3.8/lib/python3.8/site-packages/dependency_injector/resources.py:20: in __getitem__
    return cls(item)
../../../.pyenv/versions/3.8.9/lib/python3.8/typing.py:875: in __new__
    obj = super().__new__(cls, *args, **kwds)
E   TypeError: object.__new__() takes exactly one argument (the type to instantiate)

Removing this seems to fix it https://github.com/ets-labs/python-dependency-injector/blob/155f59869922de2c0ae57ab2e77d85655e15f21b/src/dependency_injector/resources.py#L18-L20

What's that for?

EdwardBlair avatar Aug 14 '21 10:08 EdwardBlair

Hey @EdwardBlair. I did have mypy tests for generic typing of these classes, but didn't have runtime testing when generic typing is used. It has been broken for a while. Thanks for reporting the issue.

I fixed it. PR #490 . The fix is in the develop branch. I can not publish it on PyPI right now because project has reached PyPI size limit. I'll go ahead with publishing right after size issue is addressed.

rmk135 avatar Aug 16 '21 14:08 rmk135

PR #490 is published in 4.36.0.

rmk135 avatar Aug 25 '21 17:08 rmk135