Improve the exception message when trying to subclass

The code

import enum

class a(enum):
    Foo = 1
    Bar = 1

will raise the exception TypeError: module() takes at most 2 arguments (3 given).

This doesn’t tell me what I did wrong. Instead, it tells something about a callable module() which doesn’t seem to exist.
Instead, I suggest that this should raise an exception like Type Error: Cannot subclass module because that tells people accidentally writing enum instead of enum.Enum what they did wrong so that they can find their mistake easily.

2 Likes

The tricky bit with this is that information is useful to folks implementing metaclasses. So you would need to chain the exceptions as well as make sure you don’t raise it incorrectly (e.g. it might legitimately be a coding error and not because the object is not a class).