Title: confusing error msg from random.randint
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.2
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: ajaksu2, eric.araujo, georg.brandl, petraszd, phr, rhettinger
Priority: low Keywords: easy, patch

Created on 2006-09-17 06:22 by phr, last changed 2010-11-21 22:33 by rhettinger. This issue is now closed.

File name Uploaded Description Edit
pseudo-fix-1560032.patch petraszd, 2010-07-24 10:51 Patch that hides _inst calls inside proxy methods. review
Messages (10)
msg29852 - (view) Author: paul rubin (phr) Date: 2006-09-17 06:22
See the following output.  The reason for the confusing
message is that random.randint is actually a bound
method to a Random instance inside the module, i.e.
someone got a little bit too clever.  It should be an
ordinary function that calls that instance method instead.

>>> import random
>>> random.randint(10000)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: randint() takes exactly 3 arguments (2 given)

msg29853 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-10-12 10:51
Logged In: YES 

Note that it is documented that the random functions are
actually bound methods.
msg84474 - (view) Author: Daniel Diniz (ajaksu2) (Python triager) Date: 2009-03-30 03:08
I'll close this as won't fix unless someone wants to fix this.
msg84513 - (view) Author: paul rubin (phr) Date: 2009-03-30 05:54
ajaksu2, I don't understand why you want to close this bug if it isn't
fixed.  I can accept that it's not the highest priority issue in the
world, but it's something that trips up users from time to time, and it
ix obviously fixable.  Closing bugs like this is demoralizing to those
of us who take the trouble to submit them in the hope of helping improve
Python.  I've noticed that any number of times over the years, and
recently saw a post about Ubuntu bug triage that expressed the sentiment
better than I could:

(And while trying to locate that several week old post through web
searches, I came across several more like it...).  

Anyway, the best thing to do with bugs like this is fix them.  If there
is other work with higher priority, the obvious thing is to just leave
the bug open so someone gets around to it sooner or later.  Closing the
bug without a fix comes across as destructive.
msg84517 - (view) Author: Daniel Diniz (ajaksu2) (Python triager) Date: 2009-03-30 06:20
I meant no disrespect, but it seemed to me not even you wanted it fixed
(as you never replied to Georg's comment). I stated I'd close this issue
unless someone wanted it fixed.

I'm reopening it, as you do want it fixed :)

FWIW and IMHO, I don't see much improvement from a message saying
"randint() takes exactly 2 arguments (1 given)". So I think it's
possible it can be considered invalid, even after all this time.

Anyway, if you can provide a patch (or even a specification) that makes
the message clear without introducing too much code complication, I'm
sure getting this fixed this will be much more likely.

(nice link BTW, I read it back then and I think I'm following his advice
where needed)
msg84701 - (view) Author: paul rubin (phr) Date: 2009-03-30 22:57
Daniel, thanks--I didn't mean to jump on you, so I'm sorry if it came
across that way.  Maybe I'm a little oversensitized to this issue due to
some unrelated incidents with other programs.

I'll try to write a more detailed reply and maybe include a patch later
(I can't do it right now).


msg86271 - (view) Author: Daniel Diniz (ajaksu2) (Python triager) Date: 2009-04-22 05:08
Don't worry :)

Tagging as a Bug Day candidate.
msg111451 - (view) Author: Petras Zdanavičius (petraszd) * Date: 2010-07-24 10:51
I have written a patch thats makes these strange error messages go away.
What actually I have done was something like this:

>>> randint = _inst.randint

Was replaced with:

>>> def randint(a, b):
        return _inst.randint(a, b)

But I do not think it is worth to add additional stack frame just for error messages be a little more better.
msg122003 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-11-21 22:20
This is related to the more general issue of how to report a wrong number of arguments in a method call; I don’t remember if it was a python-dev discussion, a python-ideas one or a bug report.

random is not the only module where instance methods are used as module functions; I don’t know what is more important between slightly better error messages and performance.
msg122006 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-11-21 22:33
Sorry, it's time to close this one.  The code is not buggy, it is documented as being a bound method and that is a perfectly acceptable python coding style to use bound methods as callables.  It's also been around for a *very* long without causing issues beyond Python's usual irritation with the number of arguments messages (that happened when Python became object oriented and that artifact still survives).

When it comes to the random module, people are performance sensitive and it would be a total waste to wrap these bound methods in another layer just to get different error message reporting.  Anyone whose Monte Carlo simulation run time doubles because of this would be rightfully irritated.
Date User Action Args
2010-11-21 22:33:03rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg122006

assignee: rhettinger
resolution: not a bug
2010-11-21 22:20:20eric.araujosetnosy: + eric.araujo
messages: + msg122003
2010-08-24 20:36:13BreamoreBoysetstage: test needed -> patch review
versions: + Python 3.2, - Python 3.1, Python 2.7
2010-07-24 10:51:16petraszdsetfiles: + pseudo-fix-1560032.patch

nosy: + petraszd
messages: + msg111451

keywords: + patch
2009-04-22 05:08:32ajaksu2setkeywords: + easy

messages: + msg86271
2009-03-30 22:57:04phrsetmessages: + msg84701
2009-03-30 06:21:00ajaksu2setstatus: pending -> open

messages: + msg84517
stage: test needed
2009-03-30 05:54:30phrsetmessages: + msg84513
2009-03-30 03:08:56ajaksu2setstatus: open -> pending
versions: + Python 3.1, Python 2.7, - Python 2.4
nosy: + ajaksu2

messages: + msg84474

type: enhancement
2006-09-17 06:22:07phrcreate