[Python-checkins] r53865 - sandbox/trunk/pep362/pep362.py sandbox/trunk/pep362/test_pep362.py

brett.cannon python-checkins at python.org
Fri Feb 23 15:41:43 CET 2007


Author: brett.cannon
Date: Fri Feb 23 15:41:40 2007
New Revision: 53865

Modified:
   sandbox/trunk/pep362/pep362.py
   sandbox/trunk/pep362/test_pep362.py
Log:
Add keyword-only and annotation support to Parameter.__init__.


Modified: sandbox/trunk/pep362/pep362.py
==============================================================================
--- sandbox/trunk/pep362/pep362.py	(original)
+++ sandbox/trunk/pep362/pep362.py	Fri Feb 23 15:41:40 2007
@@ -9,20 +9,46 @@
 
 class Parameter(object):
 
-    """Represent a parameter in a function signature."""
+    """Represent a parameter in a function signature.
 
-    def __init__(self, name, position, has_default, *args):
+    Each parameter has the following attributes:
+    * name
+        The name of the parameter.
+    * position
+        The position in the parameter list for the argument, no including any
+        variable position argument.
+    * keyword_only
+        True if the parameter is keyword-only.
+    * has_annotation
+        True if the parameter has an annotation.  If it does, the 'annotation'
+        attribute will store the annotation.
+    * has_default
+        True if the parameter has a default value.  If it does, the
+        'default_value' attribute will store the default value.
+
+    """
+
+    def __init__(self, name, position, has_default=False, default_value=None,
+                 keyword_only=False, has_annotation=False, annotation=None):
+        """Initialize a Parameter instance.
+
+        For has_* arguments, if they are False then the corresponding *
+        parameter is ignored.
+
+        """
         self.name = name
         self.position = position
         if not has_default:
             self.has_default = False
         else:
             self.has_default = True
-            if len(args) != 1:
-                raise ValueError("Parameter requires a default value to be "
-                                    "specified when has_default has been set "
-                                    "to True")
-            self.default_value = args[0]
+            self.default_value = default_value
+        self.keyword_only = keyword_only
+        if not has_annotation:
+            self.has_annotation = False
+        else:
+            self.has_annotation = True
+            self.annotation = annotation
 
     @classmethod
     def __tuple2param(self, tuple_):
@@ -46,16 +72,6 @@
             result+= "=" + str(self.default_value)
         return result
 
-    def __repr__(self):
-        """Return the string required to create an equivalent instance of this
-        parameter."""
-        result = "%s(%r, %r, %r" % (self.__class__.__name__,
-                                    self.name, self.position, self.has_default)
-        if self.has_default:
-            result +=", %r" % self.default_value
-        result += ")"
-        return result
-
 
 class Signature(object):
 

Modified: sandbox/trunk/pep362/test_pep362.py
==============================================================================
--- sandbox/trunk/pep362/test_pep362.py	(original)
+++ sandbox/trunk/pep362/test_pep362.py	Fri Feb 23 15:41:40 2007
@@ -12,42 +12,45 @@
         # Test that 'name' attribute works.
         # Must test both using a string and a tuple of strings.
         name = "test"
-        param = pep362.Parameter(name, 0, False)
+        param = pep362.Parameter(name, 0)
         self.failUnlessEqual(param.name, name)
         name = ('a', ('b',))
-        param = pep362.Parameter(name, 0, False)
+        param = pep362.Parameter(name, 0)
         self.failUnlessEqual(param.name, name)
 
     def test_position(self):
         # Test the 'position' attribute.
         pos = 42
-        param = pep362.Parameter("_", pos, False)
+        param = pep362.Parameter("_", pos)
         self.failUnlessEqual(param.position, pos)
 
-    def test_has_default(self):
+    def test_default_values(self):
         # Test the 'has_default' attribute.
         # Testing that 'default_value' is not set is handled in the testing of
         # that attribute.
-        param = pep362.Parameter('_', 0, True, None)
+        default_value = 42
+        param = pep362.Parameter('_', 0, True, default_value)
         self.failUnlessEqual(param.has_default, True)
+        self.failUnlessEqual(param.default_value, default_value)
         param = pep362.Parameter('_', 0, False)
         self.failUnlessEqual(param.has_default, False)
-        self.failUnlessRaises(TypeError, pep362.Parameter,
-                                ('_', 0, False, 'extra arg'))
-        self.failUnlessRaises(TypeError, pep362.Parameter,
-                                ('_', 0, True))
-        self.failUnlessRaises(TypeError, pep362.Parameter,
-                                ('_', 0, True, 'default', 'extra'))
-        
-    def test_default_value(self):
-        # Test the 'default_value' attribute.
-        # Make sure that if has_default is set to False that default_value is
-        # not defined.
-        param = pep362.Parameter('_', 0, False)
-        self.failUnless(not hasattr(param, 'default_value'))
-        default = 42
-        param = pep362.Parameter('_', 0, True, default)
-        self.failUnlessEqual(param.default_value, default)
+
+    def test_keyword_only(self):
+        # Setting the value for keyword_only should create an attribute.
+        for value in (True, False):
+            param = pep362.Parameter('_', 0, keyword_only=value)
+            self.failUnlessEqual(param.keyword_only, value)
+
+    def test_annotations(self):
+        # If has_annotation is False then 'annotation' should not exist.
+        param = pep362.Parameter('_', 0, has_annotation=False)
+        self.failUnlessEqual(param.has_annotation, False)
+        self.failUnless(not hasattr(param, 'annotation'))
+        annotation = 42
+        param = pep362.Parameter('_', 0, has_annotation=True,
+                                    annotation=annotation)
+        self.failUnlessEqual(param.has_annotation, True)
+        self.failUnlessEqual(param.annotation, annotation)
 
     def test_str(self):
         # Test __str__().
@@ -58,20 +61,6 @@
         param = pep362.Parameter(name, 0, True, default_value)
         self.failUnlessEqual("%s=%s" % (name, default_value), str(param))
 
-    def test_repr(self):
-        # Test __repr__().
-        name = "X"
-        pos = 0
-        param = pep362.Parameter(name, pos, False)
-        self.failUnlessEqual("Parameter(%r, %r, False)" % (name, pos),
-                                repr(param))
-        default_value = 42
-        param = pep362.Parameter(name, pos, True, default_value)
-        self.failUnlessEqual("Parameter(%r, %r, True, %r)" %
-                                (name, pos, default_value),
-                             repr(param))
-
-
 class SignatureObjectTests(unittest.TestCase):
 
     def test_no_args(self):


More information about the Python-checkins mailing list