Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Cameron Simpson Newsgroups: comp.lang.python Subject: Re: raise None Date: Thu, 31 Dec 2015 15:03:20 +1100 Lines: 94 Message-ID: References: <5684842a$0$1596$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed X-Trace: news.uni-berlin.de Pesc4T2o2AvrA8s0f2Oewwn0QD0+M/qaYjKELfL7pPZw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'none,': 0.05; 'bug.': 0.07; 'cc:addr:python-list': 0.09; 'abort': 0.09; 'none"': 0.09; 'none.': 0.09; 'other)': 0.09; 'subject:None': 0.09; 'bug': 0.10; 'exception': 0.13; 'do,': 0.15; 'argument': 0.15; 'thu,': 0.15; '...]': 0.16; '39,': 0.16; '>in': 0.16; '>on': 0.16; 'argument.': 0.16; 'b):': 0.16; 'bug).': 0.16; 'bug,': 0.16; 'foo()': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'no-op,': 0.16; 'other:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'simpson': 0.16; 'succinct': 0.16; 'text?': 0.16; 'to:addr:pearwood.info': 0.16; "to:name:steven d'aprano": 0.16; 'traceback.': 0.16; 'workings': 0.16; 'wrote:': 0.16; 'tells': 0.18; '(in': 0.18; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'exceptions': 0.22; 'see:': 0.22; 'cheers,': 0.22; 'pass': 0.22; 'cc:no real name:2**0': 0.22; 'am,': 0.23; 'bit': 0.23; 'dec': 0.23; 'this:': 0.23; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'chris': 0.26; 'point.': 0.27; 'error': 0.27; 'checking': 0.27; 'compare': 0.27; 'function': 0.28; 'idea': 0.28; 'raise': 0.29; "i'm": 0.30; 'supposed': 0.31; 'point': 0.33; 'useful': 0.33; 'problem': 0.33; 'common': 0.33; "d'aprano": 0.33; 'raised': 0.33; 'raising': 0.33; 'steven': 0.33; 'though.': 0.33; 'traceback': 0.33; 'open': 0.33; 'file': 0.34; 'skip:> 10': 0.35; "isn't": 0.35; 'but': 0.36; 'instead': 0.36; 'mode': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'say': 0.37; 'charset:us- ascii': 0.37; 'things': 0.38; 'itself': 0.38; 'received:localdomain': 0.38; 'several': 0.38; 'end': 0.39; 'why': 0.39; 'test': 0.39; 'does': 0.39; 'where': 0.40; 'still': 0.40; 'save': 0.60; 'your': 0.60; 'skip:u 10': 0.61; 'spam.': 0.61; 'today,': 0.62; 'more': 0.63; 'cameron': 0.66; 'natural': 0.67; 'therefore': 0.67; 'sounds': 0.76; '(is': 0.84; '>if': 0.84; 'confusing': 0.84; 'failures.': 0.84; 'harmful': 0.84 Content-Disposition: inline In-Reply-To: <5684842a$0$1596$c3e8da3$5496439d@news.astraweb.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101038 On 31Dec2015 12:26, Steven D'Aprano wrote: >On Thu, 31 Dec 2015 11:38 am, Chris Angelico wrote: >>> [... functions calling common _validate function ...] >>> But when the argument checking fails, the traceback shows the error >>> occurring in _validate, not eggs or spam. (Naturally, since that is where >>> the exception is raised.) That makes the traceback more confusing than it >>> need be. >> >> If the validation really is the same in all of them, then is it a >> problem to see the validation function in the traceback? Its purpose >> isn't simply "raise an exception", but "validate a specific set of >> inputs". That sounds like a perfectly reasonable traceback line to me >> (imagine if your validation function has a bug). > >Right -- that's *exactly* why it is harmful that the _validate function >shows up in the traceback. I think I'm still disagreeing, but only on this point of distinguishing _validate bug exceptions from _validate test failures. >If _validate itself has a bug, then it will raise, and you will see the >traceback: > >Traceback (most recent call last): > File "spam", line 19, in this > File "spam", line 29, in that > File "spam", line 39, in other > File "spam", line 5, in _validate >ThingyError: ... > >which tells you that _validate raised an exception and therefore has a bug. Ok.... >Whereas if _validate does what it is supposed to do, and is working >correctly, you will see: > >Traceback (most recent call last): > File "spam", line 19, in this > File "spam", line 29, in that > File "spam", line 39, in other > File "spam", line 5, in _validate >ThingyError: ... > >and the reader has to understand the internal workings of _validate >sufficiently to infer that this exception is not a bug in _validate but an >expected failure mode of other when you pass a bad argument. Would it not be useful then to name the including function in the exception text? >In the case where _validate *returns* the exception instead of raising it, >and the calling function (in this case other) raises, you see this in the >case of a bug in _validate: > >Traceback (most recent call last): > File "spam", line 19, in this > File "spam", line 29, in that > File "spam", line 39, in other > File "spam", line 5, in _validate >ThingyError: ... > >and this is the case of a bad argument to other: > >Traceback (most recent call last): > File "spam", line 19, in this > File "spam", line 29, in that > File "spam", line 39, in other >ThingyError: ... I confess that when I want to check several things I would like to return several failure indications. So thing on that line, how about this: for blam in _validate(a, b): raise blam which leaves you open to gatheroing them all up instead of aborting on the first complaint. >I think this is a win for debuggability. (Is that a word?) But it's a bit >annoying to do it today, since you have to save the return result and >explicitly compare it to None. If "raise None" was a no-op, it would feel >more natural to just say raise _validate() and trust that if _validate >falls out the end and returns None, the raise will be a no-op. This is a nice idea though. Succinct and expressive, though people would have to learn that: raise foo() does not unconditionally abort at this point. Cheers, Cameron Simpson