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.

Title: Number ABC can be instantiated
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: Claudiu.Popa, CuriousLearner, belopolsky, berker.peksag, eric.araujo, gvanrossum, ncoghlan, rhettinger
Priority: normal Keywords: patch

Created on 2013-01-01 23:26 by belopolsky, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
issue16837.diff belopolsky, 2013-01-02 17:37 review
Messages (9)
msg178785 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2013-01-01 23:26
According to the reference manual, "None of the types defined in [numbers] module can be instantiated."  However,

>>> from numbers import *
>>> Number()
<numbers.Number object at 0x1005e5080>

This can probably be fixed by making Number.__hash__ an abstract method.
msg178827 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2013-01-02 17:37
Attached patch fixes the issue and passes regression tests, so most likely the ability to instantiate Number is not an intentional feature.

"pass" should probably be replaced with a meaningful docstring, but I would like to hear from others on whether the solution is conceptually correct before making it commit-ready.
msg227732 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-09-27 21:09
I'd say that this should be fixed, it breaks the expectancy imposed by the documentation, so +1 from me.
msg232111 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2014-12-04 08:24
I left a couple of comments on Rietveld.
msg349301 - (view) Author: Sanyam Khurana (CuriousLearner) * (Python triager) Date: 2019-08-09 17:23
Hi Alexander,

Can you please address the reviews and turn this patch into a GitHub PR, please?
msg349302 - (view) Author: Sanyam Khurana (CuriousLearner) * (Python triager) Date: 2019-08-09 17:25
I'm changing the versions to Python 3.8 and 3.9 for this bug.
msg349325 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-08-10 02:36
Usually, setting hash to None is the proper way to turn-off hashability.  And it's possible that existing code has subclassed Number and provided other methods but not __hash__, so this could potentially break their code.  On the other hand, it would be desirable to prevent instantiation, perhaps using some other mechanism would be better.  I'll take a deeper look at this when I get a chance.
msg349331 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-08-10 05:48
I looked into this a bit more and don't see another non-invasive way of blocking instantiation.

Marking for Guido to take a look at.  He's the expert on ABCs and typing and is the originator of the __hash__ = None approach to turning off hashability.

Guido, would you like number subclasses to require an override of __hash__ or is the accidental instantiation of Number() something we even care about (I think this unlikely to ever arise in practice)?  My suggestion is to deem it harmless and leave it as is.
msg349368 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-08-11 00:42
Let's not do this. We've survived this long.
Date User Action Args
2022-04-11 14:57:40adminsetgithub: 61041
2019-08-11 06:08:41rhettingersetstatus: open -> closed
resolution: wont fix
stage: patch review -> resolved
2019-08-11 00:42:43gvanrossumsetmessages: + msg349368
2019-08-10 05:48:38rhettingersetversions: + Python 2.7, - Python 3.8, Python 3.9
nosy: + gvanrossum

messages: + msg349331

assignee: rhettinger -> gvanrossum
2019-08-10 02:36:54rhettingersetassignee: rhettinger
messages: + msg349325
2019-08-09 17:25:46CuriousLearnersetmessages: + msg349302
versions: + Python 3.8, Python 3.9, - Python 3.4, Python 3.5
2019-08-09 17:23:35CuriousLearnersetnosy: + CuriousLearner
messages: + msg349301
2014-12-04 08:24:47berker.peksagsetnosy: + berker.peksag
messages: + msg232111
2014-09-27 21:09:49Claudiu.Popasetnosy: + Claudiu.Popa
messages: + msg227732
2014-09-27 21:05:51Claudiu.Popasetcomponents: + Library (Lib)
versions: + Python 3.5, - Python 3.3
2013-01-02 17:37:37belopolskysetfiles: + issue16837.diff

nosy: + rhettinger, ncoghlan, eric.araujo
messages: + msg178827

keywords: + patch
stage: patch review
2013-01-01 23:26:03belopolskycreate