[Python-Dev] PEP 484 update proposal: annotating decorated declarations

Guido van Rossum guido at python.org
Fri Jun 2 18:16:10 EDT 2017


On Fri, Jun 2, 2017 at 1:07 PM, Koos Zevenhoven <k7hoven at gmail.com> wrote:

> [...]
> I suppose it is, especially because there seems to be nothing that
> prevents you from getting runtime annotations in the enclosing class/module
> ​:
>
>
> number: int
>
> @call
> def number():
>     return 42
>

Well mypy actually gives an error for that, "Name 'number' already defined".


>
> But for functions one could have (
> ​using
>  the context manager example):
>
>
> def session(url: str) -> ContextManager[DatabaseSession]: ...
>
> @predeclared
> @contextmanager
> def session(url: str) -> Iterator[DatabaseSession]:
>     s = DatabaseSession(url)
>     try:
>         yield s
>     finally:
>         s.close()
>
>
> This makes it clear that the function is declared elsewhere. But the
> `predeclared` decorator would need tricks like sys._getframe(1) to set
> session.__annotations__ according to the predeclaration.
>

I'm not excited about that.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20170602/8f983606/attachment.html>


More information about the Python-Dev mailing list