[Python-Dev] PEP 484 update proposal: annotating decorated declarations (original) (raw)

Koos Zevenhoven k7hoven at gmail.com
Fri Jun 2 16:07:01 EDT 2017


On Fri, Jun 2, 2017 at 8:57 PM, Guido van Rossum <guido at python.org> wrote:

On Fri, Jun 2, 2017 at 9:41 AM, Koos Zevenhoven <k7hoven at gmail.com> wrote:

I still don't understand what would happen with annotations. If the decorator returns a non-function, one would expect the annotations to be in the annotations attribute of the enclosing class or module. If it returns a function, they would be in the annotations attribute of the function. And I'm talking about the runtime behavior in Python as explained in PEP484 and PEP526. I would expect these declarations to behave according to the same principles as other ways to annotate variables/functions. If there is no runtime behavior, a comment-based syntax might be more appropriate. Or have I missed something? So when returning a function, the runtime version of the decorator can easily update the function's annotations. But when returning a non-function, the decorator would have a hard time updating annotations of the containing class/module without "cheating" (e.g. sys.getframe()). I think the latter is similar to e.g. attributes defined with @property -- those don't end up in annotations either. I think this is an acceptable deficiency.

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

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.

-- Koos

--



More information about the Python-Dev mailing list