Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ethan Furman Newsgroups: comp.lang.python Subject: Re: How much sanity checking is required for function inputs? Date: Tue, 19 Apr 2016 23:09:39 -0700 Lines: 39 Message-ID: References: <5713E52D.3060407@icloud.com> <1461131475.4042173.584042297.03DAB00E@webmail.messagingengine.com> <57171D23.40709@stoneleaf.us> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de wIILX5hfvdnfOSYrLb5F8QjjhQUSNtlULgR0VRkYzgxg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'sanity': 0.07; 'subject:How': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'meaningful': 0.09; 'message-id:@stoneleaf.us': 0.09; 'yeah,': 0.09; 'exception': 0.13; 'appropriate': 0.14; 'weird': 0.15; '(tm)': 0.16; '10:26': 0.16; 'also:': 0.16; 'exceptions.': 0.16; 'fuzzy': 0.16; 'keyerror': 0.16; 'programmers.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:required': 0.16; 'wrote:': 0.16; 'library': 0.20; 'meant': 0.22; "aren't": 0.22; 'exceptions': 0.22; 'stephen': 0.22; 'users,': 0.22; '(or': 0.23; 'errors': 0.23; 'needed.': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'error': 0.27; '(such': 0.27; '~ethan~': 0.29; 'raise': 0.29; 'code': 0.30; 'push': 0.30; "i'd": 0.31; 'getting': 0.33; 'point': 0.33; 'largely': 0.33; 'clear': 0.35; 'attempt': 0.35; 'question,': 0.35; 'something': 0.35; 'level': 0.35; 'but': 0.36; 'to:addr :python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'agree': 0.37; 'things': 0.38; 'wrong': 0.38; 'mean': 0.38; 'means': 0.39; 'goes': 0.39; 'data': 0.39; 'sure': 0.39; 'rather': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'close': 0.61; 'charset:windows-1252': 0.62; 'more': 0.63; 'answer.': 0.72; 'informative,': 0.84; 'odd,': 0.84; 'reasons:': 0.84; 'ultimately,': 0.84; 'ethan': 0.91; 'furman': 0.91; 'subject:much': 0.91 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 In-Reply-To: <1461131475.4042173.584042297.03DAB00E@webmail.messagingengine.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <57171D23.40709@stoneleaf.us> X-Mailman-Original-References: <5713E52D.3060407@icloud.com> <1461131475.4042173.584042297.03DAB00E@webmail.messagingengine.com> Xref: csiph.com comp.lang.python:107396 On 04/19/2016 10:51 PM, Stephen Hansen wrote: > But that's a fuzzy question, there's no solid and clear answer. Did you > see Ethan's response? I largely agree with his trinity: > > On Sun, Apr 17, 2016, at 10:26 PM, Ethan Furman wrote: >> I sanity check for three reasons: >> >> 1) raise a nicer error message >> >> 2) keep the point of failure close to the error >> >> 3) the consequences of bad data are Bad Bad Things (tm) > > With a 4)th that exceptions aren't for users, only programmers. But > ultimately, I'd rather a user see an exception if something weird goes > wrong because they can send it to me and I can diagnose it and push an > update. So I also: > > 4) Attempt to make sure all user errors result in user error messages, > not exceptions. > > Note, 1) doesn't mean I always raise a nicer message, it means if > "KeyError" is ambiguious or odd, I raise a better and more informative > one. But you're getting nothing swapping out KeyError for > Exception(lotsofwords). > > I use 1) more to be less 'nicer' and more, er, 'more specific'. Since I > don't like exceptions to rise to the user level where niceness is > needed. Yeah, that's a better phrasing for (1); I meant more appropriate or informative, such as swapping an internal error (such as KeyError) for a more meaningful FieldNotFound error (or whatever) -- largely library code concerns. -- ~Ethan~