This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: assert return values
Type: enhancement Stage:
Components: None Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, jhylton, nnorwitz, rhettinger, tal197, tim.peters
Priority: normal Keywords:

Created on 2002-07-24 13:28 by tal197, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Messages (6)
msg53578 - (view) Author: Thomas Leonard (tal197) Date: 2002-07-24 13:28
It would be nice to write:

def root(x):
  assert x >= 0
  assert return >= 0
  ...

or

def get_path(foo):
  if foo is None: return None
  assert return is not None
  ...

and have the 'return' assertion checked when the function
exits. 'return' is already a reserved keyword so the
syntax doesn't introduce any incompatibility.

This might make functions more self-documenting, since
asserts at the end / in the middle tend to get lost.

Even if the assert goes at the end, it's still clearer, eg:

def foo():
  ...
  assert min <= return <= max
  return bar(min, max)

vs

def foo():
  ...
  tmp = bar(min, max)
  assert min <= tmp <= max
  return tmp

Thanks!
msg53579 - (view) Author: Jeremy Hylton (jhylton) (Python triager) Date: 2002-07-24 15:06
Logged In: YES 
user_id=31392

I don't think this is a great feature.  It seems like
control flow via yield or return  could easily be overlooked
if it's inside an assert statement.

Regardless of whether I like the idea, the SF bug tracker
isn't the right place to make this suggestion.  The right
thing to do is write a PEP describing the feature in detail,
then come up with a patch to implement it.  If that's too
much work, but you really like the idea, you could try to
drum up interest on comp.lang.python.
msg53580 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2002-07-24 15:42
Logged In: YES 
user_id=31435

Re-opened but moved to the Feature Request tracker (it's 
certainly not "a bug", but there's no rule against asking for 
new features either, provided they're in the right place).

I don't believe there's any control flow in the suggestion, just 
that the name "return" be taken as being bound, in assert 
statements, to the possibly anonyous return expression.  
Eiffel does something very much like this for the benefit of 
expressing post-conditions.

I certainly agree this way of spelling it in Python has 
problems, though, and a PEP would be in order.
msg53581 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2003-05-13 02:32
Logged In: YES 
user_id=357491

Wouldn't this require turning 'return' into an expression instead of a 
statement?
msg53582 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-07-01 06:41
Logged In: YES 
user_id=80475

Yuck!
msg53583 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2007-03-16 05:48
Raymond pretty much summed up my opinion. :-)  This could only happen with a PEP and still doesn't seem likely.
History
Date User Action Args
2022-04-10 16:05:31adminsetgithub: 36923
2002-07-24 13:28:42tal197create