[Py] [Ideas] Small suggestion regarding type errors and subscription

2024-10-15 Thread Chris Markiewicz via Discussions on Python.org


`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(obj, 
'__getitem__')`, so I would not assume that this is a trivial thing to do at 
the level that the exception is being raised.

The traceback generator, on the other hand, has access to both the exception 
and the code objects that created it, which I suspect is why you are able to 
get the helpful underlines at that point.

Also, note that reporting the key is also probably non-trivial. When it's a 
string or integer literal, that's one thing, but it could be an arbitrary 
expression that has up to that point not been evaluated. Attempting to execute 
it could trigger another exception, and attempting to report it could be quite 
long.

Anyway, this is not a part of the code I've spent much time in, but I suspect 
it's not an easy QOL win.





---
[Visit 
Topic](https://discuss.python.org/t/small-suggestion-regarding-type-errors-and-subscription/67898/15)
 or reply to this email to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click 
here](https://discuss.python.org/email/unsubscribe/866906ba4030eeeba815a41997c217aa221ab9a14462d1bfd05ef48b2b4eef81).


[Py] [Ideas] Small suggestion regarding type errors and subscription

2024-10-15 Thread James Campbell via Discussions on Python.org


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 refactor it as you suggested 
above, the better.  And if it's a trivial change, all the better.

Obviously coding around the Exception's error message would be madness but 
making it a bit more explicit about the specific error is always useful 
especially as not all tools such as Sentry will support that new REPL 
underlining indicator out of the box.





---
[Visit 
Topic](https://discuss.python.org/t/small-suggestion-regarding-type-errors-and-subscription/67898/14)
 or reply to this email to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click 
here](https://discuss.python.org/email/unsubscribe/3ecf8c023d73ce46d492f84d3a16f32739ae3fee8beefb9e183511524ad095b0).


[Py] [Ideas] Small suggestion regarding type errors and subscription

2024-10-15 Thread Michael H via Discussions on Python.org


[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 with a different underline from the attempt at subscription, which 
directly indicates which one failed.

While it may be possible to improve that message, just pointing that out for 
things you have to deal with now (and it should be faster to fix places you 
have to deal with this than wait for a new python version to do even more than 
this)

(And you should not rely on stability of exception messages, they aren't 
guaranteed, so I hope you don't mean to programmatically handle TypeError 
exceptions like this rather than split things and handle each possible error 
properly)

[quote="James Webber, post:2, topic:67898, username:jamestwebber"]
```
$ 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
```
[/quote]





---
[Visit 
Topic](https://discuss.python.org/t/small-suggestion-regarding-type-errors-and-subscription/67898/13)
 or reply to this email to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click 
here](https://discuss.python.org/email/unsubscribe/32a784333c39341d7f5436141c966442e2c0ab2bf30499f18a8a339d3a65a0b1).


[Py] [Ideas] Small suggestion regarding type errors and subscription

2024-10-15 Thread Brénainn Woodsend via Discussions on Python . org


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 from these emails, [click 
here](https://discuss.python.org/email/unsubscribe/0bc971ac5d8fcb99c1cdee38d9426b6e5fb213f5dbf12d269d9bb888cc0b6739).


[Py] [Ideas] Small suggestion regarding type errors and subscription

2024-10-15 Thread James Campbell via Discussions on Python.org


__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 receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click 
here](https://discuss.python.org/email/unsubscribe/a39ec8679d7568f21e2c01f1de9fbd9a25cba2b723361f7c736c6d40f7d2156f).


[Py] [Ideas] Small suggestion regarding type errors and subscription

2024-10-15 Thread Brénainn Woodsend via Discussions on Python . org


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 failed to require the 
placeholder to be used in the first place?





---
[Visit 
Topic](https://discuss.python.org/t/small-suggestion-regarding-type-errors-and-subscription/67898/10)
 or reply to this email to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click 
here](https://discuss.python.org/email/unsubscribe/f0516db887e46b862f2cac9cbb3292fbce981fa50a82d19c86301b44d0b41742).


[Py] [Ideas] Small suggestion regarding type errors and subscription

2024-10-15 Thread James Campbell via Discussions on Python.org


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 because you enabled mailing list mode.

To unsubscribe from these emails, [click 
here](https://discuss.python.org/email/unsubscribe/4b3484a694afd887029a57017099ae3b03295eb63ff3b8176cf81f5fc26a2a50).


[Py] [Ideas] Small suggestion regarding type errors and subscription

2024-10-15 Thread James Parrott via Discussions on Python.org


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/small-suggestion-regarding-type-errors-and-subscription/67898/8)
 or reply to this email to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click 
here](https://discuss.python.org/email/unsubscribe/d70535195e9c29a4820e41a7bcfa79b7deade460218a94a5633e19f1ee87c5ab).


[Py] [Ideas] Small suggestion regarding type errors and subscription

2024-10-14 Thread James Webber via Discussions on Python.org


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/67898/7)
 or reply to this email to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click 
here](https://discuss.python.org/email/unsubscribe/a41e3ab83c0b65a0324d12b0d910af941dd3c3010bcfa5d2bc3a2e4192a79bff).


[Py] [Ideas] Small suggestion regarding type errors and subscription

2024-10-14 Thread Saaket Prakash via Discussions on Python.org


[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 think what's new in 3.13 is the new REPL, which shows the tracebacks 
properly.





---
[Visit 
Topic](https://discuss.python.org/t/small-suggestion-regarding-type-errors-and-subscription/67898/6)
 or reply to this email to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click 
here](https://discuss.python.org/email/unsubscribe/4400e63246266fef9abccc1787caeff0ebe7b49fe7295e75b74c02e7d1ad2537).


[Py] [Ideas] Small suggestion regarding type errors and subscription

2024-10-14 Thread Elis Byberi via Discussions on Python.org


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 
here](https://discuss.python.org/email/unsubscribe/c112d43d3708fe501f62adafaf1cbb8d91ae8d45f8013b63ffdf7c718c8498eb).


[Py] [Ideas] Small suggestion regarding type errors and subscription

2024-10-14 Thread Cornelius Krupp via Discussions on Python.org


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 email to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click 
here](https://discuss.python.org/email/unsubscribe/0ca9ca72b5ddc0b62fde348b73f132cf40478689a8a2b8f4dbfdbb2a769bb4c1).


[Py] [Ideas] Small suggestion regarding type errors and subscription

2024-10-14 Thread James Campbell via Discussions on Python.org


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 respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click 
here](https://discuss.python.org/email/unsubscribe/4e5860f7b5cc89ac2a9d744921b4b88c5c7e5a76f766fcefb7ce388da375d4d5).


[Py] [Ideas] Small suggestion regarding type errors and subscription

2024-10-14 Thread James Webber via Discussions on Python.org


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 is what python 3.13 does. ~~Maybe 3.12 as well?~~ edit: confirmed it's new 
in 3.13





---
[Visit 
Topic](https://discuss.python.org/t/small-suggestion-regarding-type-errors-and-subscription/67898/2)
 or reply to this email to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click 
here](https://discuss.python.org/email/unsubscribe/69bf5dc6fceeaa7b4cb306d1112b0f4a9d5fe352e24a14a716a642fc8369944e).


[Py] [Ideas] Small suggestion regarding type errors and subscription

2024-10-14 Thread James Campbell via Discussions on Python.org


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”] # TypeError: None is not subscripable
```

Currently doesn’t indicate which key triggered this which isn’t helpful in 
software that will send the high level exception message to you.

There has been recent discussion in the null operator thread that statements 
like this should always make clear which part failed

But python even today doesn’t always do this





---
[Visit 
Topic](https://discuss.python.org/t/small-suggestion-regarding-type-errors-and-subscription/67898/1)
 or reply to this email to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click 
here](https://discuss.python.org/email/unsubscribe/525c7e6daa421f0446e4e4e62f7c525b204d8fda7ca4adf1b9f49f03c634711b).