Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__getitem__ error message hard to understand #50010

Closed
cvrebert mannequin opened this issue Apr 15, 2009 · 3 comments
Closed

__getitem__ error message hard to understand #50010

cvrebert mannequin opened this issue Apr 15, 2009 · 3 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@cvrebert
Copy link
Mannequin

cvrebert mannequin commented Apr 15, 2009

BPO 5760
Nosy @birkenfeld, @terryjreedy

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2009-04-18.08:31:49.499>
created_at = <Date 2009-04-15.06:32:10.854>
labels = ['interpreter-core', 'type-bug']
title = '__getitem__ error message hard to understand'
updated_at = <Date 2009-04-18.08:31:49.188>
user = 'https://bugs.python.org/cvrebert'

bugs.python.org fields:

activity = <Date 2009-04-18.08:31:49.188>
actor = 'georg.brandl'
assignee = 'none'
closed = True
closed_date = <Date 2009-04-18.08:31:49.499>
closer = 'georg.brandl'
components = ['Interpreter Core']
creation = <Date 2009-04-15.06:32:10.854>
creator = 'cvrebert'
dependencies = []
files = []
hgrepos = []
issue_num = 5760
keywords = []
message_count = 3.0
messages = ['85983', '86105', '86110']
nosy_count = 3.0
nosy_names = ['georg.brandl', 'terry.reedy', 'cvrebert']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue5760'
versions = ['Python 3.0', 'Python 3.1', 'Python 2.7']

@cvrebert
Copy link
Mannequin Author

cvrebert mannequin commented Apr 15, 2009

Prompted by
http://mail.python.org/pipermail/python-ideas/2009-April/004048.html

The current error message issued when trying to use the get item ([])
operator on an object that does not define __getitem__ can be hard to
understand:

>>> class A(object): pass
...
>>> A()['a']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'A' object is unsubscriptable

Problems observed:

  • "unsubscriptable" is easily misread in haste as "unscriptable", which
    can be quite confusing, especially to newbies
  • "subscripting" is not frequently used to describe the [] operator,
    making the message more difficult to decipher (again, esp. for newbies)
  • the underlying lack of a __getitem__ method is not mentioned, thus not
    making it obvious how to remedy the error

Suggestion:
Use exception chaining and rephrase the error message to get something like:

AttributeError: class 'A' has no attribute '__getitem__'
The above exception was the direct cause of the following exception:
TypeError: 'A' object does not support the 'get item' operator

Similar changes should be made to __setitem__ & __delitem__.

@cvrebert cvrebert mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Apr 15, 2009
@terryjreedy
Copy link
Member

As I said on the python-ideas discussion, which definitely did *not*
come to consensus, I disagree with this suggestion. To repeat and
expand on what I said there:

  1. 'unsubscriptable' could instead be changed to 'not subscriptable'.

  2. 'subscription' *is* the way Python describes the use of [].
    """A subscription selects an item of a sequence (string, tuple or list)
    or mapping (dictionary) object:

subscription ::= primary "[" expression_list "]"
"""
'array subscript' (from C, for example) is a standard term used for
decades in computer languages. It is appropriate since a[i] is one
standard single-text-line replacement for a<typographic subscript>i when
one cannot actually 'type' a subscript. It *is* a way of representing a
subscript, just as i**j is an in-line way of representing a superscript
exponent, which we still call an exponent in spite of it not being
'raised'. Both constitute visible markup that is part of the compromise
in representing 2D typography with a 1D stream of characters. Using '/'
for division (and fractions) is another.

  1. Python error message generally do not and I think should not mention
    the special method implementation that underlies surface level errors.
    In this particular case, the remedy to mistakenly trying to subscript
    something is to not do that. I think mentioning __getitem__ would be a
    distraction. One generally *cannot* just go around adding it.

@birkenfeld
Copy link
Member

I concur. I changed "unsubscriptable" to "not subscriptable" in r71696.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants