[Python-checkins] peps: One last edit on PEP 362; I think Guido's ready to give this the green light.

larry.hastings python-checkins at python.org
Fri Jun 22 22:13:23 CEST 2012


http://hg.python.org/peps/rev/a1622b1bafe0
changeset:   4474:a1622b1bafe0
user:        Larry Hastings <larry at hastings.org>
date:        Fri Jun 22 13:12:59 2012 -0700
summary:
  One last edit on PEP 362; I think Guido's ready to give this the green light.

files:
  pep-0362.txt |  73 ++++++++++++++++++++-------------------
  1 files changed, 37 insertions(+), 36 deletions(-)


diff --git a/pep-0362.txt b/pep-0362.txt
--- a/pep-0362.txt
+++ b/pep-0362.txt
@@ -43,7 +43,8 @@
 
 * return_annotation : object
     The "return" annotation for the function. If the function
-    has no "return" annotation, this attribute is not set.
+    has no "return" annotation, this attribute is set to
+    ``Signature.empty``.
 
 * parameters : OrderedDict
     An ordered mapping of parameters' names to the corresponding
@@ -60,7 +61,7 @@
     behavior.)  Raises a ``TypeError`` if the passed arguments do
     not match the signature.
 
-* replace(parameters, \*, return_annotation) -> Signature
+* replace(parameters=<optional>, \*, return_annotation=<optional>) -> Signature
     Creates a new Signature instance based on the instance
     ``replace`` was invoked on.  It is possible to pass different
     ``parameters`` and/or ``return_annotation`` to override the
@@ -68,6 +69,9 @@
     ``return_annotation`` from the copied ``Signature``, pass in
     ``Signature.empty``.
 
+    Note that the '=<optional>' notation, means that the argument is
+    optional.  This notation applies to the rest of this PEP.
+
 Signature objects are immutable.  Use ``Signature.replace()`` to
 make a modified copy:
 ::
@@ -90,7 +94,7 @@
 
 There are two ways to instantiate a Signature class:
 
-* Signature(parameters, \*, return_annotation)
+* Signature(parameters=<optional>, \*, return_annotation=Signature.empty)
     Default Signature constructor.  Accepts an optional sequence
     of ``Parameter`` objects, and an optional ``return_annotation``.
     Parameters sequence is validated to check that there are no
@@ -136,11 +140,11 @@
 
 * default : object
     The default value for the parameter.  If the parameter has no
-    default value, this attribute is not set.
+    default value, this attribute is set to ``Parameter.empty``.
 
 * annotation : object
     The annotation for the parameter.  If the parameter has no
-    annotation, this attribute is not set.
+    annotation, this attribute is set to ``Parameter.empty``.
 
 * kind
     Describes how argument values are bound to the parameter.
@@ -176,7 +180,7 @@
     Always use ``Parameter.*`` constants for setting and checking
     value of the ``kind`` attribute.
 
-* replace(\*, name, kind, default, annotation) -> Parameter
+* replace(\*, name=<optional>, kind=<optional>, default=<optional>, annotation=<optional>) -> Parameter
     Creates a new Parameter instance based on the instance
     ``replaced`` was invoked on.  To override a Parameter
     attribute, pass the corresponding argument.  To remove
@@ -185,7 +189,7 @@
 
 Parameter constructor:
 
-* Parameter(name, kind, \*, annotation, default)
+* Parameter(name, kind, \*, annotation=Parameter.empty, default=Parameter.empty)
    Instantiates a Parameter object. ``name`` and ``kind`` are required,
    while ``annotation`` and ``default`` are optional.
 
@@ -277,9 +281,11 @@
     - If the object is a an instance of ``FunctionType`` construct
       and return a new ``Signature`` for it
 
-    - If the object is a method, construct and return a new ``Signature``
+    - If the object is a bound method, construct and return a new ``Signature``
       object, with its first parameter (usually ``self`` or ``cls``)
-      removed
+      removed.  (``classmethod`` and ``staticmethod`` are supported
+      too.  Since both are descriptors, the former returns a bound method,
+      and the latter returns its wrapped function.)
 
     - If the object is an instance of ``functools.partial``, construct
       a new ``Signature`` from its ``partial.func`` attribute, and
@@ -472,27 +478,23 @@
         for param in sig.parameters.values():
             # Iterate through function's parameters and build the list of
             # arguments types
+            type_ = param.annotation
+            if type_ is param.empty or not inspect.isclass(type_):
+                # Missing annotation or not a type, skip it
+                continue
+
+            types[param.name] = type_
+
+            # If the argument has a type specified, let's check that its
+            # default value (if present) conforms with the type.
             try:
-                type_ = param.annotation
+                default = param.default
             except AttributeError:
                 continue
             else:
-                if not inspect.isclass(type_):
-                    # Not a type, skip it
-                    continue
-
-                types[param.name] = type_
-
-                # If the argument has a type specified, let's check that its
-                # default value (if present) conforms with the type.
-                try:
-                    default = param.default
-                except AttributeError:
-                    continue
-                else:
-                    if not isinstance(default, type_):
-                        raise ValueError("{func}: wrong type of a default value for {arg!r}". \
-                                         format(func=func.__qualname__, arg=param.name))
+                if not isinstance(default, type_):
+                    raise ValueError("{func}: wrong type of a default value for {arg!r}". \
+                                     format(func=func.__qualname__, arg=param.name))
 
         def check_type(sig, arg_name, arg_type, arg_value):
             # Internal function that encapsulates arguments type checking
@@ -530,17 +532,16 @@
                         check_type(sig, arg_name, type_, arg)
 
             result = func(*ba.args, **ba.kwargs)
+
             # The last bit - let's check that the result is correct
-            try:
-                return_type = sig.return_annotation
-            except AttributeError:
-                # Looks like we don't have any restriction on the return type
-                pass
-            else:
-                if isinstance(return_type, type) and not isinstance(result, return_type):
-                    raise ValueError('{func}: wrong return type, {exp} expected, got {got}'. \
-                                     format(func=func.__qualname__, exp=return_type.__name__,
-                                            got=type(result).__name__))
+            return_type = sig.return_annotation
+            if (return_type is not sig._empty and
+                    isinstance(return_type, type) and
+                    not isinstance(result, return_type)):
+
+                raise ValueError('{func}: wrong return type, {exp} expected, got {got}'. \
+                                 format(func=func.__qualname__, exp=return_type.__name__,
+                                        got=type(result).__name__))
             return result
 
         return wrapper

-- 
Repository URL: http://hg.python.org/peps


More information about the Python-checkins mailing list