[Python-ideas] where statement in Pyret

Markus Unterwaditzer markus at unterwaditzer.net
Sun Nov 10 11:26:06 CET 2013


I agree that such hints about the tests would be nice for performance, 
but i don't think considerations about performance were ever given a 
high priority during the design of Python.

How about this:

     def sum():
         # implementation

     @sum.test
     def test_sum():
         assert sum([]) == 0

This potentially could do the same things you expected from the 
where-statement, without introducing new keywords. Maybe this could also 
be buried into some stdlib module in order to avoid "polluting" the core 
language if this way of writing tests doesn't gain any traction.

     import unittest  # or whatever
     def sum():
         # implementation

     @unittest.test_func(sum):
     def test_sum():
         assert sum([]) == 0

-- Markus

On 2013-11-10 11:06, Tarek Ziadé wrote:
> Le 11/10/13 11:00 AM, Markus Unterwaditzer a écrit :
> 
>> While it is a nice idea, i don't think this feature deserves its own 
>> syntax. Besides doctests, another way of achieving this in Python 
>> might be:
>> 
>> def sum(l):
>> # implementation of sum
>> 
>> def test_sum():
>> assert sum([]) == 0
>> assert sum([1, 2, 3]) == 6
>> 
>> which IMO is nice enough.
>  yes, that's what we all already do: tests in test_xxx functions. And
> the adopted convention
>  is to have the tests in dedicated tests modules, which defeats the
> benefit of having
>  real isolated unit tests just by the function code.
> 
>  And even if we place the test function just besides the tested
> function, Python will not
>  make any distinction : they are both just functions.
> 
>  Having the ability to distinguish tests and regular code at the 
> language level
>  has benefits like the ability to ignore tests when you run the app
> in production etc.
> 
>  Cheers
>  Tarek
> 
>> -- Markus
>> 
>> "Tarek Ziadé" <tarek at ziade.org> wrote:
>> 
>>> Hey
>>> 
>>> I've read about Pyret on hackernews: http://www.pyret.org/ [2]
>>> 
>>> and found the 'where' statement very compeling. Functions can end 
>>> with a where that contains small unit
>>> tests.
>>> 
>>> From the documentation example:
>>> 
>>> fun sum(l):
>>> cases(List) l:
>>> | empty => 0
>>> | link(first, rest) => first + sum(rest)
>>> end
>>> where:
>>> sum([]) is 0
>>> sum([1, 2, 3]) is 6
>>> end
>>>  
>>> It's quite similar to the doctests ideas I guess - but not intended 
>>> to be documentation like them.
>>> 
>>> I ended up disliking docttests because of this doc+test duality by 
>>> the way: it often ends up as a
>>> not so good documentation and not so good tests.
>>> 
>>> Anyways, having a dedicated keyword to append after a function some 
>>> tests as part of the language
>>> has benefits imho:
>>> 
>>> - the scope is reduced to the function - so it helps making 'real' 
>>> isolated unit tests.
>>> - we do have the unittest conventions, but here it make tests a 
>>> first class citizen in the language.
>>> 
>>> Cheers
>>> Tarek
>>> 
>>> -------------------------
>>> 
>>> Python-ideas mailing list
>>> Python-ideas at python.org
>>> https://mail.python.org/mailman/listinfo/python-ideas [1]
> 
> 
> 
> Links:
> ------
> [1] https://mail.python.org/mailman/listinfo/python-ideas
> [2] http://www.pyret.org/


More information about the Python-ideas mailing list