Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
c6d80c1b
Commit
c6d80c1b
authored
Aug 08, 2010
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue 8814: functools.wraps() did not copy __annotations__.
parent
f56c9cd3
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
17 additions
and
7 deletions
+17
-7
Doc/library/functools.rst
Doc/library/functools.rst
+3
-3
Lib/functools.py
Lib/functools.py
+3
-2
Lib/test/test_functools.py
Lib/test/test_functools.py
+6
-2
Misc/ACKS
Misc/ACKS
+1
-0
Misc/NEWS
Misc/NEWS
+4
-0
No files found.
Doc/library/functools.rst
View file @
c6d80c1b
...
...
@@ -66,9 +66,9 @@ The :mod:`functools` module defines the following functions:
attributes of the wrapper function are updated with the corresponding attributes
from the original function. The default values for these arguments are the
module level constants *WRAPPER_ASSIGNMENTS* (which assigns to the wrapper
function's *__name__*, *__module__*
and *__doc__*, the documentation string) and
*WRAPPER_UPDATES* (which updates the wrapper function's *__dict__*, i.e. the
instance dictionary).
function's *__name__*, *__module__*
, *__annotations__* and *__doc__*, the
documentation string) and *WRAPPER_UPDATES* (which updates the wrapper
function's *__dict__*, i.e. the
instance dictionary).
The main intended use for this function is in :term:`decorator` functions which
wrap the decorated function and return the wrapper. If the wrapper function is
...
...
Lib/functools.py
View file @
c6d80c1b
...
...
@@ -12,7 +12,7 @@ from _functools import partial, reduce
# update_wrapper() and wraps() are tools to help write
# wrapper functions that can handle naive introspection
WRAPPER_ASSIGNMENTS
=
(
'__module__'
,
'__name__'
,
'__doc__'
)
WRAPPER_ASSIGNMENTS
=
(
'__module__'
,
'__name__'
,
'__doc__'
,
'__annotations__'
)
WRAPPER_UPDATES
=
(
'__dict__'
,)
def
update_wrapper
(
wrapper
,
wrapped
,
...
...
@@ -30,6 +30,7 @@ def update_wrapper(wrapper,
function (defaults to functools.WRAPPER_UPDATES)
"""
for
attr
in
assigned
:
if
hasattr
(
wrapped
,
attr
):
setattr
(
wrapper
,
attr
,
getattr
(
wrapped
,
attr
))
for
attr
in
updated
:
getattr
(
wrapper
,
attr
).
update
(
getattr
(
wrapped
,
attr
,
{}))
...
...
Lib/test/test_functools.py
View file @
c6d80c1b
...
...
@@ -181,17 +181,19 @@ class TestUpdateWrapper(unittest.TestCase):
self
.
assertTrue
(
wrapped_attr
[
key
]
is
wrapper_attr
[
key
])
def
test_default_update
(
self
):
def
f
():
def
f
(
a
:
'This is a new annotation'
):
"""This is a test"""
pass
f
.
attr
=
'This is also a test'
def
wrapper
():
def
wrapper
(
b
:
'This is the prior annotation'
):
pass
functools
.
update_wrapper
(
wrapper
,
f
)
self
.
check_wrapper
(
wrapper
,
f
)
self
.
assertEqual
(
wrapper
.
__name__
,
'f'
)
self
.
assertEqual
(
wrapper
.
__doc__
,
'This is a test'
)
self
.
assertEqual
(
wrapper
.
attr
,
'This is also a test'
)
self
.
assertEqual
(
wrapper
.
__annotations__
[
'a'
],
'This is a new annotation'
)
self
.
assertNotIn
(
'b'
,
wrapper
.
__annotations__
)
def
test_no_update
(
self
):
def
f
():
...
...
@@ -204,6 +206,7 @@ class TestUpdateWrapper(unittest.TestCase):
self
.
check_wrapper
(
wrapper
,
f
,
(),
())
self
.
assertEqual
(
wrapper
.
__name__
,
'wrapper'
)
self
.
assertEqual
(
wrapper
.
__doc__
,
None
)
self
.
assertEqual
(
wrapper
.
__annotations__
,
{})
self
.
assertFalse
(
hasattr
(
wrapper
,
'attr'
))
def
test_selective_update
(
self
):
...
...
@@ -230,6 +233,7 @@ class TestUpdateWrapper(unittest.TestCase):
functools
.
update_wrapper
(
wrapper
,
max
)
self
.
assertEqual
(
wrapper
.
__name__
,
'max'
)
self
.
assertTrue
(
wrapper
.
__doc__
.
startswith
(
'max('
))
self
.
assertEqual
(
wrapper
.
__annotations__
,
{})
class
TestWraps
(
TestUpdateWrapper
):
...
...
Misc/ACKS
View file @
c6d80c1b
...
...
@@ -144,6 +144,7 @@ Steve Clift
Nick Coghlan
Josh Cogliati
Dave Cole
Terrence Cole
Benjamin Collar
Jeffery Collins
Paul Colomiets
...
...
Misc/NEWS
View file @
c6d80c1b
...
...
@@ -12,6 +12,10 @@ What's New in Python 3.1.3?
Core and Builtins
-----------------
- Issue #8814: function annotations (the ``__annotations__`` attribute)
are now included in the set of attributes copied by default by
functools.wraps and functools.update_wrapper. Patch by Terrence Cole.
- Issue #83755: Implicit set-to-frozenset conversion was not thread-safe.
- Issue #9416: Fix some issues with complex formatting where the
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment