[Python-ideas] Proposed PEP 484 addition: describe a way of annotating decorated declarations

Guido van Rossum guido at python.org
Mon May 1 10:44:30 EDT 2017


On Mon, May 1, 2017 at 12:28 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:

> On 1 May 2017 at 03:07, Guido van Rossum <guido at python.org> wrote:
> > There's a PR to the peps proposal here:
> > https://github.com/python/peps/pull/242
> >
> > The full text of the current proposal is below. The motivation for this
> is
> > that for complex decorators, even if the type checker can figure out
> what's
> > going on (by taking the signature of the decorator into account), it's
> > sometimes helpful to the human reader of the code to be reminded of the
> type
> > after applying the decorators (or a stack thereof). Much discussion can
> be
> > found in the PR. Note that we ended up having `Callable` in the type
> because
> > there's no rule that says a decorator returns a function type (e.g.
> > `property` doesn't).
>
> So a rigorous typechecker that understood the full decorator stack
> would be able to check whether or not the argument to `decorated_type`
> was correct, while all typecheckers (and human readers) would be able
> to just believe the argument rather than having to run through all the
> decorator transformations?
>

Yes. In fact the intention is that the checker should check the declared
type with the inferred and complain if they don't fit. In some cases the
inferred type would have `Any` where the declared type would have a
specific type and then the declared type would "win" (for uses of the
decorated function) -- this is an example of where "erosion" in type
inference can be counteracted by explicit declarations.

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


More information about the Python-ideas mailing list