Issue5996
Created on 2009-05-11 14:06 by thet, last changed 2022-04-11 14:56 by admin.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| abc.patch | eltoder, 2011-04-10 02:28 | review | ||
| Messages (16) | |||
|---|---|---|---|
| msg87574 - (view) | Author: Johannes Raggam (thet) | Date: 2009-05-11 14:06 | |
when declaring a abstract base class with an abstract property or method and subclassing from dict, the class is instantiable (instanceable?). >>> import abc >>> class A(object): ... __metaclass__ = abc.ABCMeta ... @abc.abstractproperty ... def abstract(self): return True ... >>> a = A() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Can't instantiate abstract class A with abstract methods abstract >>> >>> class A2(dict): ... __metaclass__ = abc.ABCMeta ... @abc.abstractproperty ... def abstract(self): return True ... >>> a2 = A2() >>> although, when using the dict definition from __builtin__.pi directly, the abc behaves like expected. but this may be a bug in the c-implementation from dict. platform: Python 2.6.2 (r262:71600, Apr 25 2009, 21:56:41) [GCC 4.3.2] on linux2 |
|||
| msg87576 - (view) | Author: Antoine Pitrou (pitrou) * ![]() |
Date: 2009-05-11 14:56 | |
Also happens with other builtin types such as tuple. It's probably related to the way subclasses of those types are instantiated, bypassing the normal metaclass mechanism. |
|||
| msg87854 - (view) | Author: Terry J. Reedy (terry.reedy) * ![]() |
Date: 2009-05-16 03:47 | |
I presume you claim the dict example to be a bug in relation to "A class that has a metaclass derived from ABCMeta cannot be instantiated unless all of its abstract methods and properties are overridden." There is the same difference with @abstractproperty Windows, 3.0.1 class C(metaclass=abc.ABCMeta): @abc.abstractmethod def f(self): return True class C2(dict,metaclass=abc.ABCMeta): @abc.abstractmethod def f(self): return True c2=C2() print(c2.f()) c=C() # prints True ... TypeError: Can't instantiate abstract class C with abstract methods f |
|||
| msg113698 - (view) | Author: Daniel Urban (daniel.urban) * ![]() |
Date: 2010-08-12 18:52 | |
I think, that the reason is that, object.__new__ checks, if the class is instantiable (object_new in Objects/typeobject.c ). dict.__new__ (and tuple.__new__, and I guess the __new__ method of other built-in types) doesn't call object.__new__, but user defined types typically either doesn't have a __new__, or call object.__new__ from it (directly or with super). |
|||
| msg133440 - (view) | Author: Eugene Toder (eltoder) * | Date: 2011-04-10 02:28 | |
This patch fixes the problem by moving the check from object_new to PyType_GenericAlloc. The check is very cheap, so this should not be an issue. |
|||
| msg138411 - (view) | Author: Eugene Toder (eltoder) * | Date: 2011-06-16 02:59 | |
Anyone has any thoughts on this? |
|||
| msg140108 - (view) | Author: Raymond Hettinger (rhettinger) * ![]() |
Date: 2011-07-11 11:28 | |
IIRC, classes weren't supposed to be able inherit from two classes that had different metaclasses (since differing metaclasses don't necessarily play well with one another). |
|||
| msg140114 - (view) | Author: Eugene Toder (eltoder) * | Date: 2011-07-11 12:35 | |
They are, when there's a most specific metaclass -- the one which is a subclass of all others (described here http://www.python.org/download/releases/2.2.3/descrintro/#metaclasses, implemented here http://hg.python.org/cpython/file/ab162f925761/Objects/typeobject.c#l1956). Since ABCMeta is a subclass of type this holds. Also, in the original example there's no multiple inheritance at all. |
|||
| msg266704 - (view) | Author: Xiang Zhang (xiang.zhang) * ![]() |
Date: 2016-05-30 16:21 | |
I think subclassing builtin types and making it abstract is rare. And when there is a need, we can mimic this in application level (this may also apply to types having custom __new__): In [2]: class CustomDict(dict, metaclass=abc.ABCMeta): ...: def __new__(cls, *args, **kwargs): ...: if getattr(cls, '__abstractmethods__', None): ...: raise TypeError ...: return super().__new__(cls, *args, **kwargs) ...: @abc.abstractmethod ...: def f(self): ...: pass Adding the abstract class checking in tp_alloc or builtin types' tp_new maybe degrade performance. Is it necessary to add this support? |
|||
| msg279479 - (view) | Author: Guido van Rossum (gvanrossum) * ![]() |
Date: 2016-10-26 01:45 | |
Honestly let's just forget about this. |
|||
| msg279481 - (view) | Author: Nathaniel Manista (Nathaniel Manista) | Date: 2016-10-26 02:00 | |
Wait, really? My report came out of a real bug that I had in my system and shipped to my users; it wasn't academic or contrived at all. |
|||
| msg279483 - (view) | Author: Guido van Rossum (gvanrossum) * ![]() |
Date: 2016-10-26 02:14 | |
Where did you report that? I don't see your name on this bug -- it has a patch that's been unapplied for 5 years, so I doubt it's very important. |
|||
| msg279484 - (view) | Author: Guido van Rossum (gvanrossum) * ![]() |
Date: 2016-10-26 02:17 | |
Oh sorry. I received the emails in a strange order. I guess it can stay open. |
|||
| msg279499 - (view) | Author: R. David Murray (r.david.murray) * ![]() |
Date: 2016-10-26 13:44 | |
My apologies, I added Nathaniel to nosy here when I closed the duplicate, but forgot to add a link to the closed issue: issue 28537. |
|||
| msg299815 - (view) | Author: R. David Murray (r.david.murray) * ![]() |
Date: 2017-08-06 21:40 | |
Closed issue 31127 as a duplicate of this one. |
|||
| msg335337 - (view) | Author: Josh Rosenberg (josh.r) * ![]() |
Date: 2019-02-12 17:26 | |
Closed #35958 as a duplicate of this issue (and updated the title, since clearly the problem is not specific to dict). Patch probably needs to be rebased/rewritten against latest trunk (given it dates from Mercurial days). |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:56:48 | admin | set | github: 50246 |
| 2020-10-30 23:49:27 | iritkatriel | set | versions: + Python 3.9, Python 3.10, - Python 3.1, Python 2.7, Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 3.6, Python 3.7 |
| 2020-05-01 18:27:37 | Jim Fasarakis-Hilliard | set | nosy:
+ Jim Fasarakis-Hilliard |
| 2020-02-23 13:08:03 | mark.dickinson | set | nosy:
- mark.dickinson |
| 2020-02-13 16:51:32 | 2xB | set | nosy:
+ 2xB |
| 2019-02-25 23:32:20 | cheryl.sabella | link | issue24235 superseder |
| 2019-02-12 18:02:57 | gvanrossum | set | nosy:
- gvanrossum |
| 2019-02-12 17:30:26 | remi.lapeyre | set | nosy:
+ remi.lapeyre |
| 2019-02-12 17:26:58 | josh.r | set | nosy:
+ josh.r, Jon McMahon title: abstract class instantiable when subclassing dict -> abstract class instantiable when subclassing built-in types messages: + msg335337 versions:
+ Python 3.5, Python 3.6, Python 3.7, Python 3.8 |
| 2019-02-12 17:23:38 | josh.r | link | issue35958 superseder |
| 2017-08-06 21:40:29 | r.david.murray | set | nosy:
+ Kevin Shweh messages: + msg299815 |
| 2017-08-06 21:39:14 | r.david.murray | link | issue31127 superseder |
| 2017-02-26 08:06:38 | serhiy.storchaka | unlink | issue29650 dependencies |
| 2017-02-26 08:06:38 | serhiy.storchaka | link | issue29650 superseder |
| 2017-02-26 07:49:41 | xiang.zhang | link | issue29650 dependencies |
| 2016-10-26 13:44:05 | r.david.murray | set | nosy:
+ r.david.murray messages: + msg279499 |
| 2016-10-26 02:17:05 | gvanrossum | set | status: closed -> open resolution: wont fix -> messages: + msg279484 |
| 2016-10-26 02:14:51 | gvanrossum | set | messages: + msg279483 |
| 2016-10-26 02:00:19 | Nathaniel Manista | set | messages: + msg279481 |
| 2016-10-26 01:45:41 | gvanrossum | set | status: open -> closed resolution: wont fix messages: + msg279479 |
| 2016-10-26 00:45:23 | r.david.murray | set | nosy:
+ aleax, cvrebert, Nathaniel Manista |
| 2016-10-26 00:44:33 | r.david.murray | link | issue28537 superseder |
| 2016-06-01 02:40:48 | luiz.poleto | set | nosy:
+ luiz.poleto |
| 2016-05-30 16:21:46 | xiang.zhang | set | nosy:
+ gvanrossum messages: + msg266704 |
| 2016-05-26 15:36:31 | rhettinger | set | assignee: rhettinger -> |
| 2016-05-26 14:01:32 | xiang.zhang | set | nosy:
+ xiang.zhang |
| 2016-05-25 13:21:58 | maciej.szulik | set | nosy:
+ maciej.szulik |
| 2016-05-25 13:19:26 | r.david.murray | link | issue26306 superseder |
| 2013-12-10 12:19:04 | jwilk | set | nosy:
+ jwilk |
| 2013-12-07 15:03:45 | mark.dickinson | set | versions: + Python 3.4 |
| 2013-12-07 15:00:20 | mark.dickinson | set | nosy:
+ mark.dickinson |
| 2011-07-11 13:04:39 | rhettinger | set | priority: normal -> low assignee: rhettinger |
| 2011-07-11 12:35:43 | eltoder | set | messages: + msg140114 |
| 2011-07-11 11:28:59 | rhettinger | set | nosy:
+ rhettinger messages: + msg140108 |
| 2011-07-09 20:52:20 | eric.snow | set | nosy:
+ eric.snow |
| 2011-06-16 02:59:50 | eltoder | set | messages: + msg138411 |
| 2011-04-10 02:28:38 | eltoder | set | files:
+ abc.patch nosy:
+ eltoder keywords: + patch |
| 2011-04-08 15:27:03 | eric.araujo | set | nosy:
+ eric.araujo versions: + Python 3.3, - Python 2.6 |
| 2011-04-08 08:33:43 | nadeem.vawda | set | nosy:
+ nadeem.vawda |
| 2010-08-14 22:00:18 | pitrou | set | nosy:
+ benjamin.peterson |
| 2010-08-12 18:52:21 | daniel.urban | set | nosy:
+ daniel.urban messages: + msg113698 |
| 2009-05-16 03:47:20 | terry.reedy | set | nosy:
+ terry.reedy messages: + msg87854 |
| 2009-05-11 14:56:50 | pitrou | set | priority: normal versions: + Python 3.1, Python 2.7, Python 3.2 nosy: + pitrou messages: + msg87576 |
| 2009-05-11 14:06:01 | thet | create | |

