[Python-checkins] r83807 - in python/branches/release31-maint: Doc/library/functools.rst Lib/functools.py Lib/test/test_functools.py Misc/ACKS Misc/NEWS

raymond.hettinger python-checkins at python.org
Sun Aug 8 02:56:52 CEST 2010


Author: raymond.hettinger
Date: Sun Aug  8 02:56:52 2010
New Revision: 83807

Log:
Issue 8814: functools.wraps() did not copy __annotations__.

Modified:
   python/branches/release31-maint/Doc/library/functools.rst
   python/branches/release31-maint/Lib/functools.py
   python/branches/release31-maint/Lib/test/test_functools.py
   python/branches/release31-maint/Misc/ACKS
   python/branches/release31-maint/Misc/NEWS

Modified: python/branches/release31-maint/Doc/library/functools.rst
==============================================================================
--- python/branches/release31-maint/Doc/library/functools.rst	(original)
+++ python/branches/release31-maint/Doc/library/functools.rst	Sun Aug  8 02:56:52 2010
@@ -66,9 +66,9 @@
    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

Modified: python/branches/release31-maint/Lib/functools.py
==============================================================================
--- python/branches/release31-maint/Lib/functools.py	(original)
+++ python/branches/release31-maint/Lib/functools.py	Sun Aug  8 02:56:52 2010
@@ -12,7 +12,7 @@
 # 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,7 +30,8 @@
        function (defaults to functools.WRAPPER_UPDATES)
     """
     for attr in assigned:
-        setattr(wrapper, attr, getattr(wrapped, attr))
+        if hasattr(wrapped, attr):
+            setattr(wrapper, attr, getattr(wrapped, attr))
     for attr in updated:
         getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
     # Return the wrapper so this can be used as a decorator via partial()

Modified: python/branches/release31-maint/Lib/test/test_functools.py
==============================================================================
--- python/branches/release31-maint/Lib/test/test_functools.py	(original)
+++ python/branches/release31-maint/Lib/test/test_functools.py	Sun Aug  8 02:56:52 2010
@@ -181,17 +181,19 @@
                 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 @@
         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 @@
         functools.update_wrapper(wrapper, max)
         self.assertEqual(wrapper.__name__, 'max')
         self.assertTrue(wrapper.__doc__.startswith('max('))
+        self.assertEqual(wrapper.__annotations__, {})
 
 class TestWraps(TestUpdateWrapper):
 

Modified: python/branches/release31-maint/Misc/ACKS
==============================================================================
--- python/branches/release31-maint/Misc/ACKS	(original)
+++ python/branches/release31-maint/Misc/ACKS	Sun Aug  8 02:56:52 2010
@@ -144,6 +144,7 @@
 Nick Coghlan
 Josh Cogliati
 Dave Cole
+Terrence Cole
 Benjamin Collar
 Jeffery Collins
 Paul Colomiets

Modified: python/branches/release31-maint/Misc/NEWS
==============================================================================
--- python/branches/release31-maint/Misc/NEWS	(original)
+++ python/branches/release31-maint/Misc/NEWS	Sun Aug  8 02:56:52 2010
@@ -12,6 +12,10 @@
 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


More information about the Python-checkins mailing list