classification
Title: Warning required when calling register() on an ABCMeta subclass
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.0
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: christian.heimes Nosy List: christian.heimes, gvanrossum, mark
Priority: normal Keywords: patch

Created on 2007-09-05 15:22 by mark, last changed 2008-01-06 22:29 by admin. This issue is now closed.

Messages (7)
msg55663 - (view) Author: Mark Summerfield (mark) Date: 2007-09-05 15:22
GvR asked me to add this to the bug tracker.

If you do this:

    class A(ABCMeta): pass

    A.register(list)

you get this error message:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: maximum recursion depth exceeded in __instancecheck__

This is not very helpful.

You probably meant to write:

    class A(metaclass=ABCMeta): pass

Perhaps a warning message like this would be better:

"register() should not be called on an ABCMeta subclass; maybe you
forgot the metaclass keyword argument when declaring the class?"
msg57742 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-11-21 22:47
This would be easier to fix if we didn't have unbound methods.  I'm
going to ask the py3k list if anybody really cares about having those.
msg57980 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-11-30 13:43
Are you fine with the error message or do you have a better one?

Index: Lib/abc.py
===================================================================
--- Lib/abc.py  (Revision 59228)
+++ Lib/abc.py  (Arbeitskopie)
@@ -137,8 +137,12 @@
         cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
         return cls

-    def register(cls, subclass):
+    def register(cls, subclass=None):
         """Register a virtual subclass of an ABC."""
+        if subclass is None:
+            raise TypeError("register() cannot be called on an ABCMeta "
+                "subclass. Maybe you forgot the metaclass keyword
argument "
+                "when declaring the class?")
         if not isinstance(cls, type):
             raise TypeError("Can only register classes")
         if issubclass(subclass, cls):
msg57983 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-11-30 14:33
Fixed in r59233. Please adjust the error message if you don't like it.

TypeError: register() cannot be called on an ABCMeta subclass, use class
Example(metaclass=abc.ABCMeta) instead.
msg57985 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-11-30 15:07
Please roll this back.  The error message you added is inappropriate
when the parameter to a legitimate register() call is omitted, e.g.

collections.Sequence.register()

Since we got rid of unbound methods, the infinite recursion is gone;
that's a good enough fix, there are tons of other situations where
confusion between class and instance (or between metaclass and class)
causes confusing error messages.
msg57986 - (view) Author: Mark Summerfield (mark) Date: 2007-11-30 15:29
On 2007-11-30, Christian Heimes wrote:
> Christian Heimes added the comment:
>
> Fixed in r59233. Please adjust the error message if you don't like it.
>
> TypeError: register() cannot be called on an ABCMeta subclass, use class
> Example(metaclass=abc.ABCMeta) instead.

I think it is fine---but seems that GvR doesn't want it!
msg57987 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-11-30 15:32
I've reverted the changes.
History
Date User Action Args
2008-01-06 22:29:44adminsetkeywords: - py3k
versions: Python 3.0
2007-11-30 15:32:52christian.heimessetstatus: open -> closed
messages: + msg57987
2007-11-30 15:29:49marksetmessages: + msg57986
2007-11-30 15:07:48gvanrossumsetstatus: closed -> open
assignee: gvanrossum -> christian.heimes
messages: + msg57985
2007-11-30 14:33:35christian.heimessetstatus: open -> closed
resolution: fixed
messages: + msg57983
2007-11-30 13:43:22christian.heimessetkeywords: + py3k, patch
nosy: + christian.heimes
messages: + msg57980
components: + Library (Lib), - Interpreter Core
2007-11-21 22:47:55gvanrossumsetmessages: + msg57742
2007-09-17 08:11:24jafosetpriority: normal
assignee: gvanrossum
nosy: + gvanrossum
2007-09-05 15:22:18markcreate