On Thu, Oct 21, 2021 at 10:49:35AM +0200, Baptiste Carvello wrote:

> > Versions of this that rely on catching AttributeError are simply wrong 
> > and are an anti-pattern. They catch too much and silently turn 
> > errors into silent wrong behaviour.
> >  
> > PEP 505 does not fall into that trap.
> 
> This is not true as a general rule: the PEP 505 examples with
> `dict.get()` do catch too much.

The problem there is not the None-aware operators, but the use of 
dict.get. That's a good reason to re-think None-aware subscripting.

    dict?['key']

will still raise if you mistype the key, while dict.get does not.

Even if we limit ourselves to dict.get:

    return obj?.get('spam')?.get('eggs')

doesn't protect against mispellings of the keys, *due to dict.get*, but 
it does protect against mispelling "get" (unlikely). More importantly it 
also protects against type errors:

    obj['spam'] = {'eggs', value}  # oops, a set, not a dict

or equivalent. (I don't mean to limit this to just typos.)

Now if we write:

    obj?.get('spam')?.get('eggs')

the first attribute lookup will return a set and the second will fail 
because sets don't have a get method.

Where as if we use exceptions:

    try:
        obj['spam]['eggs']
    except (TypeError, KeyError):
        return None

the error is silently suppressed and we get None when we should get a 
TypeError.



-- 
Steve
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/WYEIZ3IC6P6KN4L2MROV4SBCZ5XOQUV6/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to