Skip to content

Subclassed generic (Async)Resource fails py3.8.9 #488

Open
@EdwardBlair

Description

@EdwardBlair

Given python-dependency-injector@4.35.2

# 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

def __getitem__(cls, item):
# Spike for Python 3.6
return cls(item)

What's that for?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions