From davidluebeck at gmx.net Sat Jan 23 12:58:37 2016 From: davidluebeck at gmx.net (=?UTF-8?B?RGF2aWQgTMO8YmVjaw==?=) Date: Sat, 23 Jan 2016 18:58:37 +0100 Subject: [python-muc] Test runner checking for absence of 'None' in all functions Message-ID: <56A3BF4D.7060102@gmx.net> Dear fellow Munich Pythonistas, I am currently reading a book [1] that gives the following coding tip: "Prefer Exceptions to Returning None". You can see code examples out of the book further below. As I am teaching myself pytest these days, I would like to write a test runner (which eventually is to be part of a Continuous Integration setup) that fails should it detect any function that returns None. My problem is that I would like to limit the test runner to look *inside* of functions (and/or other structures). I can very well write a parser that checks, per line, if the words "return" and "None" are used. But that would cover the entire file in question, never mind its structure. Do you have any ideas how to test for the 'absence of None' within functions and the like? I am looking forward to your suggestions! Greetings and thanks, David # bad code def divide(a, b): try: return a / b except ZeroDivisionError: return None # good code def divide(a, b): try: return a / b except ZeroDivisionError as e: raise ValueError(?Invalid inputs?) from e [1] Brett Slatkin (2015), Effective Python, Addison-Wesley From h.goebel at goebel-consult.de Sun Jan 24 14:41:25 2016 From: h.goebel at goebel-consult.de (Hartmut Goebel) Date: Sun, 24 Jan 2016 20:41:25 +0100 Subject: [python-muc] Test runner checking for absence of 'None' in all functions In-Reply-To: <56A3BF4D.7060102@gmx.net> References: <56A3BF4D.7060102@gmx.net> Message-ID: <56A528E5.4010202@goebel-consult.de> Am 23.01.2016 um 18:58 schrieb David L?beck: > Do you have any ideas how to test for the 'absence of None' within > functions and the like? Ich habe zwar nicht genau verstanden, aus Du pr?fen willst. Aber du kommst wahrscheinlich nicht umhin, die Module zu parsen. Wenn Du auf bestimmte Code-Muster Aktionen ausf?hren m?chtest, k?nntest Du das evtl. mit den "fixes" aus lib2to3 erschlagen. Wahrscheinlich gibt es noch andere Parser, die Du benutzen k?nntest und deren "Matching" einfacher ist. Z.B. spark - bei dem ich aber nicht wei?, ob es die aktuelle Syntax komplett beherrscht. -- Sch?nen Gru? Hartmut Goebel Dipl.-Informatiker (univ), CISSP, CSSLP Information Security Management, Security Governance, Secure Software Development Goebel Consult, Landshut http://www.goebel-consult.de Blog: http://www.goebel-consult.de/blog/verschlusselte-mailingslisten Kolumne: http://www.cissp-gefluester.de/2012-09-steht-ein-manta-fahrer-vor-der-uni -------------- next part -------------- An HTML attachment was scrubbed... URL: