New submission from Greg Ward: Python's error message when you let None accidentally sneak into an expression where it doesn't belong could be better. The canonical example is attribute lookup:
>>> a = None >>> a.foo Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'NoneType' object has no attribute 'foo' This assumes that the programmer knows there is only one object of type NoneType, and it is None. That's a lot to assume of a beginner, whether they are coming from another programming language ("null has a type? that's crazy talk!") or are completely new to programming ("null? none? nil? wat...??"). There are plenty of other places this use of NoneType in error messages comes up: >>> a + 1 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' >>> 1 + a Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' >>> len(a) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: object of type 'NoneType' has no len() >>> a < 1 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '<' not supported between instances of 'NoneType' and 'int' I think we can do better than this. For example, here is an proposed improvement to user experience for getting and setting attributes on None: >>> a.foo Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: attempt to access attribute 'foo' of None, but None has no attributes >>> a.foo = 42 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: attempt to set attribute 'foo' on None, but None is read-only Let the bikeshedding commence. I have a working patch, but need to write tests. Will upload it here when that is done. ---------- assignee: gward components: Interpreter Core messages: 280884 nosy: gward priority: normal severity: normal status: open title: Confusing error message when None used in expressions, eg. "'NoneType' object has no attribute 'foo'" type: enhancement versions: Python 3.6 _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue28702> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com