pytype icon indicating copy to clipboard operation
pytype copied to clipboard

Generic, ABC / ABCMeta and __getitem__ can't work together

Open bbtfr opened this issue 5 years ago • 2 comments

I ran into a problem

from abc import ABC, abstractmethod
from typing import Generic, TypeVar

T = TypeVar('T')


class A(Generic[T], ABC):

    @abstractmethod
    def __getitem__(self, index: int) -> T:
        ...
    
class B(A[T]):

    pass

$ python3 --version
Python 3.6.8
$ pytype --version
2020.03.19

$ pytype test.py                                                                                                                                 1
Computing dependencies
Analyzing 1 sources with 0 local dependencies
ninja: Entering directory `/home/[===]/.pytype_output'
[1/1] check test
FAILED: /home/[===]/.pytype_output/pyi/test.pyi
/usr/bin/python3 -m pytype.single --disable pyi-error --imports_info /home/[===]/.pytype_output/imports/test.imports --module-name test -V 3.6 -o /home/[===]/.pytype_output/pyi/test.pyi --analyze-annotated --nofail --quick /home/[===]/test.py
File "/home/[===]/test.py", line 15, in <module>: Invalid TypeVar: cannot pass a TypeVar to a function [invalid-typevar]
File "/home/[===]/test.py", line 15, in <module>: Missing parameter 'index' in call to function A.__getitem__ [missing-parameter]
         Expected: (self, index)
  Actually passed: (self)

For more details, see https://google.github.io/pytype/errors.html.
ninja: build stopped: cannot make progress due to previous errors.

Seems Generic, ABC / ABCMeta and __getitem__ can't work together If I remove any one of them, the problem will disappear

bbtfr avatar Mar 21 '20 05:03 bbtfr

Looks like if there is a customized metaclass for class A, it won't be treated as Generic?

bbtfr avatar Mar 21 '20 05:03 bbtfr

Thanks for the report!

rchen152 avatar Mar 22 '20 20:03 rchen152