Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic subclass. ABCMeta object is not subscriptable #5264

Closed
cburgdorf opened this issue Jun 22, 2018 · 3 comments
Closed

Generic subclass. ABCMeta object is not subscriptable #5264

cburgdorf opened this issue Jun 22, 2018 · 3 comments

Comments

@cburgdorf
Copy link

cburgdorf commented Jun 22, 2018

Please provide more information to help us understand the issue:

I'm trying to write the following

class ServiceContext(UserList[BaseService]):
...

However, that leaves me with

TypeError: 'ABCMeta' object is not subscriptable

I know that for simple type annotations I can simply define the type in strings in cases like this. However, that clearly doesn't apply in the inheritance case.

The only solution that I found to that seems very hacky.

Since I want to enable --disallow-any-generics I need to specify the type parameter for UserList.

I'm wondering if there's any other alternative to that?

@ilevkivskyi
Copy link
Member

This is a known limitation of Python standard library classes. There is a slightly less hacky solution:

if TYPE_CHECKING:
    QueueBase = queue.Queue
else
    class _Queue:
        def __getitem__(*args):
            return queue.Queue
    QueueBase = _Queue()

In addition, for your particular use case (non-generic subclass) you can simply write:

if TYPE_CHECKING:
    Base = UserList[BaseService]
else:
    Base = UserList

But the latter solution will work only if you don't have type variables.

@cburgdorf
Copy link
Author

Ah, that's it! Thanks for clarifying. I think this should be part of the docs (or maybe at least reference to this answer or something).

Closing the issue.

adithyabsk added a commit to robinhood-unofficial/pyrh that referenced this issue Apr 12, 2020
* ABCMeta typing issue with requests package
  * python/mypy#5264 (comment)
* Run pytest and remove broken test as functionality was removed.
adithyabsk added a commit to robinhood-unofficial/pyrh that referenced this issue Apr 12, 2020
* ABCMeta typing issue with requests package
  * python/mypy#5264 (comment)
* Run pytest and remove broken test as functionality was removed.
@sultaniman
Copy link

sultaniman commented Jan 17, 2024

I ended up doing sth similar to what @ilevkivskyi did, found this in mypy docs

T = TypeVar("T")

if TYPE_CHECKING:
    class _Queue(Queue[T]):
        pass
else:
    class _Queue(Generic[T], Queue):
        pass


class BaseQueue(_Queue[T]):
    pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants