[pypy-dev] improve error message when missing 'self' in method definition

Carl Friedrich Bolz cfbolz at gmx.de
Tue Sep 27 13:19:21 EDT 2016

Hi all,

I read this paper today about common mistakes that Python beginners


The most common one by far is forgetting the "self" parameter in the
method definition (which also still happens to me regularly). The error
message is not particularly enlightening, if you don't quite understand
the explicit self in Python.

So I wonder whether we should print a better error message, something
like this:

$ cat m.py
class A(object):
    def f(x):
        return self.x

$ pypy m.py
Traceback (application-level):
  File "m.py", line 4 in <module>
TypeError: f() takes exactly 1 argument (2 given). Did you forget 'self'
in the function definition?

It's a bit the question how clever we would like this to be to reduce
false positives, see the attached patch for a very simple approach.

Anyone have opinions?


Carl Friedrich
-------------- next part --------------
A non-text attachment was scrubbed...
Name: selfmsg.patch
Type: text/x-patch
Size: 3960 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20160927/fb74f0f0/attachment.bin>

More information about the pypy-dev mailing list