pytype icon indicating copy to clipboard operation
pytype copied to clipboard

Support for ABCMeta.register

Open PhilippWendler opened this issue 4 years ago • 2 comments

Classes that use abc.ABCMeta as meta class have a method named register that can be used to register arbitrary types as subclasses of the given class (as class decorator or by calling it manually): documentation.

So the assertion (if commented in) in the following program holds, but pytype 2021.04.26 complains about a bad return type in return SubTest() line because it does not recognize that SubTest is a subclass of Test:

import abc

class Test(object, metaclass=abc.ABCMeta):
    pass

@Test.register
class SubTest:
    pass

Test.register(SubTest)

def f() -> Test:
    #assert issubclass(SubTest, Test)  # holds!
    return SubTest()

It would be nice if pytype could get support for this feature. We use it to register certain classes (e.g., transparent proxies or adapters) as subclasses without actually inheriting any method.

PhilippWendler avatar May 03 '21 14:05 PhilippWendler

Thanks for the report! We also had an internal feature request opened for this a few weeks ago. This is the comment I made at the time after looking briefly into what this would take:

Supporting ABCMeta.register() is hard. pytype typically assumes that once a class is created, its definition can't be modified, and register() goes against that assumption. (I suspect we'll end up having to special-case virtual subclasses everywhere we do type matching.

rchen152 avatar May 04 '21 23:05 rchen152

I can understand that. For our use case it would actually be enough if the class decorator would be supported, which might be easier given that it is right there at class creation?

PhilippWendler avatar May 05 '21 05:05 PhilippWendler