Message280884
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. |
|
Date |
User |
Action |
Args |
2016-11-15 20:04:43 | gward | set | recipients:
+ gward |
2016-11-15 20:04:43 | gward | set | messageid: <1479240283.12.0.750407106609.issue28702@psf.upfronthosting.co.za> |
2016-11-15 20:04:43 | gward | link | issue28702 messages |
2016-11-15 20:04:42 | gward | create | |
|