New submission from David Caro <cascaran...@gmail.com>:

In version 3.2, bpo-8814 introduced copying the __annotations__ property from 
the wrapped function to the wrapper by default.

That would be the desired behavior when your wrapper function has the same 
signature than the function it wraps, but in some cases (for example, with the 
contextlib.asynccontextmanager function) the return value is different, and 
then the __annotations__ property will have invalid information:

In [2]: from contextlib import asynccontextmanager                              
                                                                              

In [3]: @asynccontextmanager 
   ...: async def mytest() -> int: 
   ...:     return 1 
   ...:                                                                         
                                                                              

In [4]: mytest.__annotations__                                                  
                                                                              
Out[4]: {'return': int}


I propose changing the behavior of wraps, to only assign the __annotations__ by 
default if there's no __annotations__ already in the wrapper function, that 
would fit most default cases, but would allow to preserve the __annotations__ 
of the wrapper function when the types are explicitly specified, allowing now 
to change the contextlib.asynccontextmanager function with the proper types 
(returning now an AsyncContextManager) and keep the __annotation__ valid.

I'll try to get a POC and attach to the issue, but please comment with your 
ideas too.

----------
components: Library (Lib)
messages: 373233
nosy: David Caro
priority: normal
severity: normal
status: open
title: Type annotations lost when using wraps by default
type: behavior
versions: Python 3.10, Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 
3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41231>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to