◐ Shell
clean mode source ↗

gh-124342: Wrap __annotate__ functions in functools.update_wrapper by JelleZijlstra · Pull Request #124346 · python/cpython

Another surprising behavior. I'm discovering __annotate__() so it's maybe by design, I don't know :-)

When I update annotations of the wrapped function, __annotate__() of the wrapped function doesn't change, whereas __annotate__() of the wrapper is updated.

$ ./python
>>> import functools
>>> def f(x: int = 1): return x
>>> def wrapper(*args): pass
>>> functools.update_wrapper(wrapper, f)
<function f at 0x7f19f44d3e90>

>>> f.__annotations__['x']
<class 'int'>
>>> import annotationlib
>>> f.__annotate__(annotationlib.Format.VALUE)
{'x': <class 'int'>}
>>> wrapper.__annotate__(annotationlib.Format.VALUE)
{'x': <class 'int'>}

>>> f.__annotations__['x'] = float
>>> f.__annotate__(annotationlib.Format.VALUE)  # <=== HERE
{'x': <class 'int'>}
>>> wrapper.__annotate__(annotationlib.Format.VALUE)  # <=== HERE
{'x': <class 'float'>}

I would expect getting the same value for both cases. Either <class 'int'> or <class 'float'>.