Issue23572
Created on 2015-03-03 12:17 by Sergio Pascual, last changed 2015-09-04 17:23 by lukasz.langa. This issue is now closed.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| singledispatch-test.py | Sergio Pascual, 2015-03-03 12:17 | A class that breaks singledispatch | ||
| issue23572.stoneleaf.01.patch | ethan.furman, 2015-05-16 22:38 | review | ||
| Messages (9) | |||
|---|---|---|---|
| msg237126 - (view) | Author: Sergio Pascual (Sergio Pascual) * | Date: 2015-03-03 12:17 | |
I admit this case is rather convoluted, but I have been debugging a few hours so I think I should share my findings.
I have a metaclass MetaA that provides to the classes constructed with it have a dictionary interface. Of all the functions only __len__ is important. In some cases, the result of __len__ is 0 and that is what triggers the error
class MetaA(type):
def __len__(self):
return 0
class A(metaclass=MetaA):
pass
class AA(A):
pass
Now, I construct a function with single dispatch and register the case of class A but not the class AA
@singledispatch
def fun(a):
print('base case')
@fun.register(A)
def _(a):
print('fun A')
And then, call fun with an object of class AA
fun(AA())
This should call the function for the class up in the hierarchy, A
Instead it raises an exception
RuntimeError: Inconsistent hierarchy
in function functools._c3_merge
because in the line
if not candidate:
raise RuntimeError("Inconsistent hierarchy")
"not candidate" evaluates to True due to __len__ returning 0
This can be avoided by:
* adding __nonzero__ to MetaA
* not adding the map interface to a class in the first place
* changing the code in _c3_merge
I'm not really sure, but instead of:
if not candidate:
raise RuntimeError("Inconsistent hierarchy")
would this work?
if candidate is None:
raise RuntimeError("Inconsistent hierarchy")
I attach a test case
|
|||
| msg242291 - (view) | Author: Łukasz Langa (lukasz.langa) * | Date: 2015-04-30 23:13 | |
Yes, this needs addressing. |
|||
| msg242381 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * | Date: 2015-05-02 06:19 | |
An example is Enum. |
|||
| msg243374 - (view) | Author: Ethan Furman (ethan.furman) * | Date: 2015-05-16 22:38 | |
Attached is patch and test case. |
|||
| msg248685 - (view) | Author: Adam Bartoš (Drekin) * | Date: 2015-08-16 17:46 | |
I was also bitten by this via Enum. Is there any chance this will be fixed in Python 3.5? |
|||
| msg248782 - (view) | Author: Roundup Robot (python-dev) | Date: 2015-08-18 18:23 | |
New changeset 73984e665bf5 by Yury Selivanov in branch '3.5': Issue #23572: Fixed functools.singledispatch on classes with falsy metaclasses. https://hg.python.org/cpython/rev/73984e665bf5 New changeset 94d0c219d46f by Yury Selivanov in branch 'default': Merge 3.5 (issue #23572) https://hg.python.org/cpython/rev/94d0c219d46f |
|||
| msg248783 - (view) | Author: Yury Selivanov (yselivanov) * | Date: 2015-08-18 18:24 | |
> I was also bitten by this via Enum. Is there any chance this will be fixed in Python 3.5? It will be fixed in 3.5.1. Thanks for the patch, Ethan! |
|||
| msg248784 - (view) | Author: Roundup Robot (python-dev) | Date: 2015-08-18 18:41 | |
New changeset 586195685aaf by Yury Selivanov in branch '3.4': Issue #23572: Fixed functools.singledispatch on classes with falsy metaclasses. https://hg.python.org/cpython/rev/586195685aaf |
|||
| msg249792 - (view) | Author: Łukasz Langa (lukasz.langa) * | Date: 2015-09-04 17:23 | |
Thank you for fixing this and sorry for not being responsive sooner! |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2015-09-04 17:23:56 | lukasz.langa | set | messages: + msg249792 |
| 2015-08-18 18:41:00 | python-dev | set | messages: + msg248784 |
| 2015-08-18 18:38:46 | yselivanov | set | versions: + Python 3.6 |
| 2015-08-18 18:24:22 | yselivanov | set | status: open -> closed nosy:
+ yselivanov resolution: fixed |
| 2015-08-18 18:23:23 | python-dev | set | nosy:
+ python-dev messages: + msg248782 |
| 2015-08-16 17:46:10 | Drekin | set | nosy:
+ Drekin messages: + msg248685 |
| 2015-07-21 07:11:45 | ethan.furman | set | nosy:
- ethan.furman |
| 2015-05-16 22:38:14 | ethan.furman | set | files:
+ issue23572.stoneleaf.01.patch keywords: + patch messages: + msg243374 stage: patch review |
| 2015-05-02 06:19:35 | serhiy.storchaka | set | nosy:
+ serhiy.storchaka messages: + msg242381 |
| 2015-05-01 07:19:24 | paul.moore | set | nosy:
+ paul.moore |
| 2015-05-01 03:45:48 | serhiy.storchaka | set | nosy:
+ ethan.furman |
| 2015-04-30 23:13:29 | lukasz.langa | set | assignee: lukasz.langa messages: + msg242291 versions: + Python 3.5 |
| 2015-03-04 02:58:42 | berker.peksag | set | nosy:
+ lukasz.langa |
| 2015-03-03 12:17:09 | Sergio Pascual | create | |