[Python-ideas] PEP 484: Generating test inputs from type hints

Guido van Rossum guido at python.org
Tue Mar 31 04:18:24 CEST 2015


On Mon, Mar 30, 2015 at 5:41 PM, Terry Reedy <tjreedy at udel.edu> wrote:

> PEP 484 proposes "a standard syntax for type annotations", principally for
> static analysis (and other static uses), with runtime checking or
> optimization mentioned as a possibility.
>
> Another (real) runtime use is generating unit test inputs.  Today on
> python-list, David MacIver announced Hypothesis, a testing library that
> does just that. (The thread title is "Hypothesis 1.0: A production quality
> property-based testing library for Python".)  Here is an example from
> http://hypothesis.readthedocs.org/en/latest/quickstart.html
>
> from hypothesis import given
> @given([int])
> def test_reversing_twice_gives_same_list(xs):
>     assert xs == list(reversed(reversed(xs)))
>
> When a test runner such as pytest calls the wrapper, it calls the real
> function with a variety of int lists (including  [], but details not
> important here). @given() also works with unittest TestCase methods.
>
> When I mentioned PEP 484 and the alternative syntax List(int), he replied
> "Having something like that as standard would be great for Hypothesis and I
> intend to support it once it becomes available."
>
> I presume 'support' could and would include a version of given() that
> accessed .__annotations__.  Of course, if every 'test_xyz' were to be
> decorated with the same decorator, then the decorator could be omitted and
> the fuzzing moved into the test runner.


This sounds cool. Note that you shouldn't access __annotations__ directly
-- it may contain string literals. The typing module exports a functio
get_type_hints() that takes a function object and returns a dict like
__annotations__ but with the string literals evaluated. It also honors the
@no_type_check decorator. For details see here:
https://github.com/ambv/typehinting/blob/master/prototyping/typing.py#L1143

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150330/509e8bac/attachment.html>


More information about the Python-ideas mailing list