This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Allow an abc.abstractproperty to be overridden by an instance data attribute
Type: enhancement Stage: needs patch
Components: Library (Lib) Versions: Python 3.3
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: CuriousLearner, cool-RR, daniel.urban, eric.araujo, eric.snow, stutzbach
Priority: normal Keywords:

Created on 2011-05-20 17:15 by cool-RR, last changed 2022-04-11 14:57 by admin.

Messages (11)
msg136388 - (view) Author: Ram Rachum (cool-RR) * Date: 2011-05-20 17:15
When you create an `abc.abstractproperty` on a class, any subclass must override it as an actual property in order to be instantiable. But sometimes you want to override it with a data attribute instead, i.e. `self.x = 5` instead of `x = property(...)`. It would be nice if doing `self.x = 5` would satisfy `abc.abstractproperty` and allow the subclass to be instantiable.
msg136389 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2011-05-20 17:18
SGTM.  I've written code where this would have been useful.

Could you write a patch?
msg136391 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2011-05-20 18:40
Wow!  I was literally working on this problem yesterday.  The abstract check is done at instantiation time (in object.__new__, typeobject.c).  So as it stands __new__ has no way to validate that all your abstract properties have been implemented unless they are actual properties.

The solution I found simplest is to not inherit from an ABC but rather to register your class to it, and make sure that you are compliant, whether through properties or instance names.

If there is a way to check the instance for names that "implement" abstract properties that would be a useful thing in my mind.  However, I am not sure this is doable without some serious work.  Yesterday I put together a list of some solutions that would mostly work that I plan on sending to python-list today.  I am not sure if baking that into the core of Python will work.

In my mind this is a question of if abstract methods (and thereby properties) are a promise that an instance complies with an interface or just that the class of the instance complies.
msg136393 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2011-05-20 18:48
I misread the original request.  I'm +1 on making the following work, if it doesn't work already:

class MySubClass(MyAbstractClass):
    SOME_LIMIT = 5  # Implements abstract property with fixed value

We should be able to check that at instance creation time.

I think abstract methods and properties are to force the class to define methods/properties.  I don't think we should be checking if the instance adds things that are not defined at the class level (i.e., don't try to detect "self.SOME_LIMT = 5").
msg136394 - (view) Author: Ram Rachum (cool-RR) * Date: 2011-05-20 19:00
Daniel, the behavior you describe is already present in Python 3.2.
msg136395 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2011-05-20 19:02
> Daniel, the behavior you describe is already present in Python 3.2.

Awesome. :)

Do you have a compelling use-case for making "self.x = 5" satisfy an abstractproperty requirement?  One of the problems with that approach is that the instance does not satisfy the requirements until the line in __init__ is called that sets self.x.
msg136396 - (view) Author: Ram Rachum (cool-RR) * Date: 2011-05-20 19:04
Eric, do you think that a solution can be made by calling `__init__` inside of `ABCMeta.__new__` and then afterwards checking the instance for attributes?
msg136397 - (view) Author: Ram Rachum (cool-RR) * Date: 2011-05-20 19:07
Ah, I got confused, there's no way we can call `__init__` in `ABCMeta.__new__`.
msg136398 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2011-05-20 19:23
As far as I have found, there isn't a way to make it work implicitly without changing object.__new__.  I just posted some of the approaches I could think of to python-list: 

http://mail.python.org/pipermail/python-list/2011-May/1272604.html

Mostly it is a matter of using decorators for validation.
msg349299 - (view) Author: Sanyam Khurana (CuriousLearner) * (Python triager) Date: 2019-08-09 17:13
I was trying to search the mailing list archives for the URL posted by Eric: http://mail.python.org/pipermail/python-list/2011-May/1272604.html

But I couldn't find it. Also that URL shows 404.

Eric, is it possible for you to post the correct link?
msg351871 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2019-09-11 13:36
I'm guessing it should have been https://mail.python.org/pipermail/python-list/2011-May/604497.html.
History
Date User Action Args
2022-04-11 14:57:17adminsetgithub: 56337
2019-09-11 13:36:25eric.snowsetmessages: + msg351871
2019-08-09 17:13:44CuriousLearnersetnosy: + CuriousLearner
messages: + msg349299
2011-05-23 10:33:14eric.araujosetnosy: + eric.araujo
title: Allow `abc.abstractproperty` to be overridden by a data attribute -> Allow an abc.abstractproperty to be overridden by an instance data attribute

versions: - Python 3.4
2011-05-20 20:27:44daniel.urbansetnosy: + daniel.urban
2011-05-20 19:23:45eric.snowsetmessages: + msg136398
2011-05-20 19:07:59cool-RRsetmessages: + msg136397
2011-05-20 19:04:04cool-RRsetmessages: + msg136396
2011-05-20 19:02:45stutzbachsetmessages: + msg136395
2011-05-20 19:00:12cool-RRsetmessages: + msg136394
2011-05-20 18:48:27stutzbachsetmessages: + msg136393
2011-05-20 18:40:54eric.snowsetnosy: + eric.snow
messages: + msg136391
2011-05-20 17:18:34stutzbachsetnosy: + stutzbach

messages: + msg136389
stage: needs patch
2011-05-20 17:15:02cool-RRcreate