AttributeError when old-style classes fall through to the default

Issue #5 new
Ben Darnell
created an issue

On Python 2, an old-style class that does not match any of the explicit registrations raises an AttributeError instead of falling through to the default:

from singledispatch import singledispatch

@singledispatch
def f(obj):
    pass

class NewStyle(object):
    pass

class OldStyle:
    pass

f(NewStyle())
f(OldStyle())
Traceback (most recent call last):
  File "test.py", line 14, in <module>
    f(OldStyle())
  File ".../lib/python2.7/site-packages/singledispatch.py", line 210, in wrapper
    return dispatch(args[0].__class__)(*args, **kw)
  File ".../lib/python2.7/site-packages/singledispatch.py", line 191, in dispatch
    impl = _find_impl(cls, registry)
  File ".../lib/python2.7/site-packages/singledispatch.py", line 142, in _find_impl
    mro = _compose_mro(cls, registry.keys())
  File ".../lib/python2.7/site-packages/singledispatch.py", line 98, in _compose_mro
    bases = set(cls.__mro__)
AttributeError: class OldStyle has no attribute '__mro__'

Comments (1)

  1. Ben Darnell reporter

    Also, on Python 2.6, it can't be used with old-style classes even if they are explicitly registered (the cache can't make a weak reference to a class object in that version).

  2. Log in to comment