PyCharm not detecting errors raised by metaclasses

Answered
# I have roughly the following code:
import abc


class Metaclass(abc.ABCMeta):
def __new__(mcs, *args, **kwargs):
new = super().__new__(mcs, *args, **kwargs)
if not hasattr(new, "foo"):
raise AttributeError("Descendents of 'Meta' must contain a 'foo'")
return new


class Class(metaclass=Metaclass):
# This class will always raise an AttributeError
# when the interpreter reaches the declaration
pass


if __name__ == "__main__":
test = Class()
# This is guaranteed behavior, however PyCharm doesn't
# detect this as it would with code such as the following:
import abc


class Interface(abc.ABC):
@abc.abstractmethod
def foo(self):
pass


class Class(Interface):
# 'Class' is presently highlighted in yellow with the error 'class Class must implement all abstract methods'
pass


if __name__ == "__main__":
test = Class()


# I would hope that behavior such as the first program
# could be implemented, as to eliminate bugs.

0
1 comment

Hello,

Thank you for the report!

It's a known issue. Please vote for it on our tracker to get notifications when there are any updates.

0

Please sign in to leave a comment.