`obj[key]` is sugar for `obj.__getitem__(key)`. The first step is to look up
the `__getitem__` method, and the second is to call it with the key. While it's
possible that the code that raises the `TypeError` has access to all three
objects, it can already be raised just by attempting `getattr(
Noted, yes this was more just a QOL improvement suggestion by me as it strikes
me as a useful breadcrumb that could be left to help to the developer get to
the bottom of where it's come from especially when debugging bad code like this.
The faster they can find where the bad code is and refact
[quote="James Campbell, post:11, topic:67898, full:true, username:jcampbell05"]
**getattr** already includes the attribute name in the Exception, is there a
reason that **getitem** could not do the same ?
[/quote]
If you look closely at the exception here, where `None` comes from is
underlined
Ahh, I see what you mean now. Yes, that should be possible.
---
[Visit
Topic](https://discuss.python.org/t/small-suggestion-regarding-type-errors-and-subscription/67898/12)
or reply to this email to respond.
You are receiving this because you enabled mailing list mode.
To unsubscribe fro
__getattr__ already includes the attribute name in the Exception, is there a
reason that __getitem__ could not do the same ?
---
[Visit
Topic](https://discuss.python.org/t/small-suggestion-regarding-type-errors-and-subscription/67898/11)
or reply to this email to respond.
You are receivi
I think that the *should issues involving None be made more self explanatory*
question is unanswerable until you figure out *how* you'd do it. In this case,
how is `NoneType.__getattr__()` or `NoneType.__getitem__()` supposed to know
what the `None` placeholder-ing for or what assumption faile
Of course but there is always a big gap between the code you should write
And the code you’ve been given to debug 😀
---
[Visit
Topic](https://discuss.python.org/t/small-suggestion-regarding-type-errors-and-subscription/67898/9)
or reply to this email to respond.
You are receiving this b
If that line of code is broken up across two lines, the error will be more
specific, and the bug simple to fix.
Python one liners are superficially great, but only when they work. Debugging
them and trying to understand them, not so much.
---
[Visit
Topic](https://discuss.python.org/t/s
That makes much more sense. I was sure I had seen it before 3.13, but it must
have been in a notebook or somewhere else where it displayed properly. I don't
use the base REPL very often.
---
[Visit
Topic](https://discuss.python.org/t/small-suggestion-regarding-type-errors-and-subscription
[quote="James Webber, post:2, topic:67898, username:jamestwebber"]
confirmed it’s new in 3.13
[/quote]
This should be available since 3.11
https://docs.python.org/3/whatsnew/3.11.html#pep-657-fine-grained-error-locations-in-tracebacks
But it seems the old REPL didn't show them properly.
So I th
https://peps.python.org/pep-0657/
---
[Visit
Topic](https://discuss.python.org/t/small-suggestion-regarding-type-errors-and-subscription/67898/5)
or reply to this email to respond.
You are receiving this because you enabled mailing list mode.
To unsubscribe from these emails, [click
her
No, it will give the error message @jamestwebber showed. Try it out in a python
shell! That is the easiest way to figure out exactly how a feature behaves.
---
[Visit
Topic](https://discuss.python.org/t/small-suggestion-regarding-type-errors-and-subscription/67898/4)
or reply to this emai
Nice so it will give an error like this now ? If so, nice!
```
TypeError: Key “name” cannot be used with “NoneType” because it’s not
subscripts or
```
---
[Visit
Topic](https://discuss.python.org/t/small-suggestion-regarding-type-errors-and-subscription/67898/3)
or reply to this email to
Do you mean like this?
```
$ python -c 'print({"profile":None}["profile"]["name"])'
Traceback (most recent call last):
File "", line 1, in
print({"profile":None}["profile"]["name"])
~~~
TypeError: 'NoneType' object is not subscriptable
```
This i
If this provides you an error message on the specific key that’s missing
```
class User:
profile = None
u = User()
u.profile.name # AttributeError: None does not have attribute name
```
Then may I suggest that this has the same ?
```
user = {
“profile”: None
}
user[“profile”][“name”] # Ty
15 matches
Mail list logo