Class 'type' does not define '__getitem__' when using generic class

已回答

In PyCharm 2017.2.4, I'm seeing an unexpected warning when I declare a generic class that inherits from another generic but does not explicitly inherit from `Generic[T]`. In the example below, I would expect that Container2 can be used without a warning, but there is a warning (Class 'type' does define '__getitem__') unless I declare it as in Container1.

 

 

```
from typing import TypeVar, Generic, Container

T = TypeVar('T')


class Container1(Container[T], Generic[T]):
    def __contains__(self, x: object) -> bool:
        return False


class Container2(Container[T]):
    def __contains__(self, x: object) -> bool:
        return False


a: Container1[int] = Container1() # OK
b: Container2[int] = Container2() # Warning: Class 'type' does define '__getitem__'...
```

(Sorry if the formatting doesn't work right; I wasn't sure how to do code blocks.)

Container2 should be fine according to PEP484: "The Generic[T] base class is redundant in simple cases where you subclass some other generic class and specify type variables for its parameters:".

 

0

Corresponding issue in bug tracker: PY-27102.

0

请先登录再写评论。