<br><br><div class="gmail_quote">On Wed, Mar 23, 2011 at 21:38, Eric Snow <span dir="ltr"><<a href="mailto:ericsnowcurrently@gmail.com">ericsnowcurrently@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Shouldn't the Signature class also support building from args and not just a function object? Then I could use a Signature object when building a function object, or vice versa. Having the ability to validate args/kwargs against a Signature object, sort of like provided by the bind method, will be helpful, particularly with the decorator use case.</blockquote>
<div><br>I will probably try to add that functionality, but one thing at a time. =)<br><br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
What does the tuple form of Parameter.name represent?</blockquote><div><br>What tuple form? If you mean tuple parameters those no longer exist.<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Did you come to any conclusion on the open issues in the PEP? </blockquote><div><br>Nope. You are getting ahead of me. =)<br><br>-Brett<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div>
<div><br></div><div>With the exec_closure stuff I have been looking pretty closely at function-related C-API capability that might be worth exposing better in python, and Signature objects would help facilitate in a much clearer way. Not only that, but even though CPython won't use Signature objects behind the scenes, they can help to make the internal operations of Python more clear by exposing the underlying mechanisms in pure python. For example, with a validate method you could express how args and kwargs are mapped onto the different parts of the code object which correspond to the parts of the Signature object.</div>
<div><br></div><div>I am looking forward to the inclusion of the function signature stuff in 3.3.<br><div><font color="#888888"><div><br></div><div>-eric</div></font><div><div></div><div class="h5"><div><br><br><div class="gmail_quote">
On Wed, Mar 23, 2011 at 3:39 PM, brett.cannon <span dir="ltr"><<a href="mailto:python-checkins@python.org" target="_blank">python-checkins@python.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: brett.cannon<br>
Date: Wed Mar 23 22:39:32 2011<br>
New Revision: 88796<br>
<br>
Log:<br>
Update code for future inclusion in Python 3.3 (when I get around to it).<br>
<br>
Added:<br>
sandbox/trunk/pep362/test_pep362.py<br>
- copied, changed from r88415, /sandbox/trunk/pep362/tests/test_pep362.py<br>
Removed:<br>
sandbox/trunk/pep362/setup.py<br>
sandbox/trunk/pep362/tests/<br>
Modified:<br>
sandbox/trunk/pep362/ (props changed)<br>
sandbox/trunk/pep362/pep362.py<br>
<br>
Modified: sandbox/trunk/pep362/pep362.py<br>
==============================================================================<br>
--- sandbox/trunk/pep362/pep362.py (original)<br>
+++ sandbox/trunk/pep362/pep362.py Wed Mar 23 22:39:32 2011<br>
@@ -8,7 +8,7 @@<br>
pass<br>
<br>
<br>
-class Parameter(object):<br>
+class Parameter:<br>
<br>
"""Represent a parameter in a function signature.<br>
<br>
@@ -31,8 +31,9 @@<br>
<br>
"""<br>
<br>
- def __init__(self, name, position, has_default=False, default_value=None,<br>
- keyword_only=False, has_annotation=False, annotation=None):<br>
+ def __init__(self, name, position, *, has_default=False,<br>
+ default=None, keyword_only=False, has_annotation=False,<br>
+ annotation=None):<br>
"""Initialize a Parameter instance.<br>
<br>
For has_* arguments, if they are False then the corresponding *<br>
@@ -42,13 +43,13 @@<br>
<a href="http://self.name" target="_blank">self.name</a> = name<br>
self.position = position<br>
if has_default:<br>
- self.default_value = default_value<br>
+ self.default = default<br>
self.keyword_only = keyword_only<br>
if has_annotation:<br>
self.annotation = annotation<br>
<br>
<br>
-class Signature(object):<br>
+class Signature:<br>
<br>
"""Object to represent the signature of a function/method.<br>
<br>
@@ -74,39 +75,19 @@<br>
<br>
def __init__(self, func):<br>
"""Initialize from a function or method object."""<br>
- if hasattr(func, 'im_func'):<br>
- func = func.im_func<br>
- try:<br>
- func_code = func.__code__<br>
- except AttributeError:<br>
- # Compatibility for versions < 2.6.<br>
- func_code = func.func_code<br>
-<br>
+ func_code = func.__code__<br>
<a href="http://self.name" target="_blank">self.name</a> = func.__name__<br>
-<br>
- try:<br>
- # Unneeded once 2.x support is removed; can easily get info the<br>
- # "hard" way.<br>
- argspec = inspect.getfullargspec(func)[:4]<br>
- except AttributeError:<br>
- # Needed only for tuple parameters.<br>
- argspec = inspect.getargspec(func)<br>
+ # XXX replace with own implementation<br>
+ argspec = inspect.getfullargspec(func)[:4]<br>
parameters = {}<br>
<br>
# Parameter information.<br>
pos_count = func_code.co_argcount<br>
- if hasattr(func_code, 'co_kwonlyargcount'):<br>
- keyword_only_count = func_code.co_kwonlyargcount<br>
- else:<br>
- keyword_only_count = 0<br>
+ keyword_only_count = func_code.co_kwonlyargcount<br>
positional = argspec[0]<br>
keyword_only = func_code.co_varnames[pos_count:<br>
pos_count+keyword_only_count]<br>
- try:<br>
- fxn_defaults = func.__defaults__<br>
- except AttributeError:<br>
- # Deal with old names prior to 2.6.<br>
- fxn_defaults = func.func_defaults<br>
+ fxn_defaults = func.__defaults__<br>
if fxn_defaults:<br>
pos_default_count = len(fxn_defaults)<br>
else:<br>
@@ -126,7 +107,7 @@<br>
has_annotation, annotation = self._find_annotation(func, name)<br>
default_value = fxn_defaults[offset]<br>
param = Parameter(name, offset+non_default_count,<br>
- has_default=True, default_value=default_value,<br>
+ has_default=True, default=default_value,<br>
has_annotation=has_annotation,<br>
annotation=annotation)<br>
parameters[name] = param<br>
@@ -134,16 +115,14 @@<br>
for offset, name in enumerate(keyword_only):<br>
has_annotation, annotation = self._find_annotation(func, name)<br>
has_default, default_value = False, None<br>
- # hasattr check only needed for versions < 2.6.<br>
- if (hasattr(func, '__kwdefaults__') and func.__kwdefaults__ and<br>
- name in func.__kwdefaults__):<br>
+ if func.__kwdefaults__ and name in func.__kwdefaults__:<br>
has_default = True<br>
default_value = func.__kwdefaults__[name]<br>
param = Parameter(name, offset+pos_count, keyword_only=True,<br>
- has_default=has_default,<br>
- default_value=default_value,<br>
- has_annotation=has_annotation,<br>
- annotation=annotation)<br>
+ has_default=has_default,<br>
+ default=default_value,<br>
+ has_annotation=has_annotation,<br>
+ annotation=annotation)<br>
parameters[name] = param<br>
# Variable parameters.<br>
index = pos_count + keyword_only_count<br>
@@ -172,9 +151,8 @@<br>
self._parameters = parameters<br>
<br>
# Return annotation.<br>
- if hasattr(func, '__annotations__'):<br>
- if 'return' in func.__annotations__:<br>
- self.return_annotation = func.__annotations__['return']<br>
+ if 'return' in func.__annotations__:<br>
+ self.return_annotation = func.__annotations__['return']<br>
<br>
def __getitem__(self, key):<br>
return self._parameters[key]<br>
@@ -186,10 +164,9 @@<br>
"""Return True if an annotation exists for the named parameter along<br>
with its annotation, else return False and None."""<br>
has_annotation, annotation = False, None<br>
- if hasattr(func, '__annotations__'):<br>
- if name in func.__annotations__:<br>
- has_annotation = True<br>
- annotation = func.__annotations__[name]<br>
+ if name in func.__annotations__:<br>
+ has_annotation = True<br>
+ annotation = func.__annotations__[name]<br>
return has_annotation, annotation<br>
<br>
def _convert_name(self, name):<br>
@@ -234,7 +211,7 @@<br>
break<br>
else:<br>
raise BindError("too many positional arguments")<br>
- self._tuple_bind(bindings, <a href="http://param.name" target="_blank">param.name</a>, position_arg)<br>
+ bindings[<a href="http://param.name" target="_blank">param.name</a>] = position_arg<br>
args = args[1:]<br>
# Keyword arguments & default values.<br>
else:<br>
@@ -247,9 +224,8 @@<br>
except KeyError:<br>
raise BindError("%r unbound" % param_name)<br>
else:<br>
- if hasattr(positional_param, 'default_value'):<br>
- self._tuple_bind(bindings, param_name,<br>
- positional_param.default_value)<br>
+ if hasattr(positional_param, 'default'):<br>
+ bindings[param_name] = positional_param.default<br>
else:<br>
raise BindError("%r parameter lacking default value" %<br>
param_name)<br>
@@ -276,39 +252,14 @@<br>
# Keyword-only default values.<br>
else:<br>
for name, param in keyword_only.items():<br>
- if hasattr(param, 'default_value'):<br>
- bindings[name] = param.default_value<br>
+ if hasattr(param, 'default'):<br>
+ bindings[name] = param.default<br>
else:<br>
raise BindError("%s parameter lacking a default value" %<br>
name)<br>
<br>
return bindings<br>
<br>
- def _tuple_bind(self, bindings, possible_tuple, value):<br>
- """Where a tuple could be a parameter, handle binding the values to the<br>
- tuple and storing into the bindings mapping."""<br>
- if not isinstance(possible_tuple, tuple):<br>
- bindings[possible_tuple] = value<br>
- else:<br>
- # Need to make sure that value is as long as the parameter, but not<br>
- # vice-versa.<br>
- error_msg = "not enough values to unpack for %r"<br>
- tuple_iter = iter(possible_tuple)<br>
- try:<br>
- value_iter = iter(value)<br>
- except TypeError:<br>
- raise BindError(error_msg % possible_tuple)<br>
- while True:<br>
- try:<br>
- sub_param = tuple_iter.next()<br>
- except StopIteration:<br>
- break<br>
- try:<br>
- sub_value = value_iter.next()<br>
- except StopIteration:<br>
- raise BindError(error_msg % possible_tuple)<br>
- self._tuple_bind(bindings, sub_param, sub_value)<br>
-<br>
<br>
def signature(func):<br>
"""Return a Signature object for the function or method.<br>
@@ -318,15 +269,9 @@<br>
attribute if possible (but is not required).<br>
<br>
"""<br>
- if hasattr(func, 'im_func'):<br>
- func = func.im_func<br>
- sig = Signature(func)<br>
- if not hasattr(func, '__signature__'):<br>
- try:<br>
- func.__signature__ = sig<br>
- except AttributeError:<br>
- pass<br>
- else:<br>
- sig = func.__signature__<br>
-<br>
- return sig<br>
+ try:<br>
+ return func.__signature__<br>
+ except AttributeError:<br>
+ sig = Signature(func)<br>
+ func.__signature__ = sig<br>
+ return sig<br>
<br>
Deleted: sandbox/trunk/pep362/setup.py<br>
==============================================================================<br>
--- sandbox/trunk/pep362/setup.py Wed Mar 23 22:39:32 2011<br>
+++ (empty file)<br>
@@ -1,25 +0,0 @@<br>
-from distutils.core import setup<br>
-<br>
-setup(<br>
- # Package metadata.<br>
- name='pep362',<br>
- version='0.6.2',<br>
- description='Implementation of PEP 362 (Function Signature objects)',<br>
- author='Brett Cannon',<br>
- author_email='<a href="mailto:brett@python.org" target="_blank">brett@python.org</a>',<br>
- url='<a href="http://svn.python.org/view/sandbox/trunk/pep362/" target="_blank">http://svn.python.org/view/sandbox/trunk/pep362/</a>',<br>
- # Files.<br>
- py_modules=['pep362', 'examples'],<br>
- packages=['tests'],<br>
- data_files=['README'],<br>
- classifiers=[<br>
- 'Programming Language :: Python :: 2',<br>
- 'Programming Language :: Python :: 2.5',<br>
- 'Programming Language :: Python :: 2.6',<br>
- 'Programming Language :: Python :: 2.7',<br>
- 'Programming Language :: Python :: 3',<br>
- 'Programming Language :: Python :: 3.0',<br>
- 'Programming Language :: Python :: 3.1',<br>
- 'Programming Language :: Python :: 3.2',<br>
- ]<br>
- )<br>
<br>
Copied: sandbox/trunk/pep362/test_pep362.py (from r88415, /sandbox/trunk/pep362/tests/test_pep362.py)<br>
==============================================================================<br>
--- /sandbox/trunk/pep362/tests/test_pep362.py (original)<br>
+++ sandbox/trunk/pep362/test_pep362.py Wed Mar 23 22:39:32 2011<br>
@@ -1,23 +1,49 @@<br>
import pep362<br>
<br>
import unittest<br>
-from tests import pep362_fodder<br>
-try:<br>
- from tests import pep362_py2_fodder<br>
- from test import test_support<br>
-except SyntaxError:<br>
- from tests import pep362_py3k_fodder<br>
- from test import support as test_support<br>
+from test import support as test_support<br>
from sys import version_info<br>
<br>
<br>
-def version_specific(major_number):<br>
- def inner(fxn):<br>
- if version_info[0] == major_number:<br>
- return fxn<br>
- else:<br>
- return lambda self: self<br>
- return inner<br>
+def no_args():<br>
+ pass<br>
+<br>
+def var_args(*args):<br>
+ pass<br>
+<br>
+def var_kw_args(**kwargs):<br>
+ pass<br>
+<br>
+def no_default_args(a):<br>
+ pass<br>
+<br>
+def default_args(a=42):<br>
+ pass<br>
+<br>
+def keyword_only(*, a):<br>
+ pass<br>
+<br>
+def keyword_only_default(*, a=42):<br>
+ pass<br>
+<br>
+def arg_annotation(a:int):<br>
+ pass<br>
+<br>
+def arg_annotation_default(a:int=42):<br>
+ pass<br>
+<br>
+def arg_annotation_var(*args:int, **kwargs:str):<br>
+ pass<br>
+<br>
+def arg_annotation_keyword_only(*, a:int):<br>
+ pass<br>
+<br>
+def return_annotation() -> int:<br>
+ pass<br>
+<br>
+def all_args(a:int, d=0, *args:int,<br>
+ g:int, h:int=8, **kwargs:int) -> int:<br>
+ return a, d, g, h, args, kwargs<br>
<br>
<br>
class ParameterObjectTests(unittest.TestCase):<br>
@@ -41,13 +67,14 @@<br>
self.assertEqual(param.position, pos)<br>
<br>
def test_default_values(self):<br>
- # Testing that 'default_value' is not set is handled in the testing of<br>
+ # Testing that 'default' is not set is handled in the testing of<br>
# that attribute.<br>
default_value = 42<br>
- param = pep362.Parameter('_', 0, True, default_value)<br>
- self.assertEqual(param.default_value, default_value)<br>
- param = pep362.Parameter('_', 0, False)<br>
- self.assertTrue(not hasattr(param, 'default_value'))<br>
+ param = pep362.Parameter('_', 0, has_default=True,<br>
+ default=default_value)<br>
+ self.assertEqual(param.default, default_value)<br>
+ param = pep362.Parameter('_', 0, has_default=False)<br>
+ self.assertTrue(not hasattr(param, 'default'))<br>
<br>
def test_keyword_only(self):<br>
# Setting the value for keyword_only should create an attribute.<br>
@@ -70,21 +97,21 @@<br>
def test_getitem(self):<br>
# __getitem__() should return the Parameter object for the name<br>
# parameter.<br>
- sig = pep362.Signature(pep362_fodder.default_args)<br>
+ sig = pep362.Signature(default_args)<br>
self.assertTrue(sig['a'])<br>
param = sig['a']<br>
self.assertTrue(<a href="http://param.name" target="_blank">param.name</a>, 'a')<br>
<br>
def test_iter(self):<br>
# The iterator should return all Parameter objects in the proper order.<br>
- sig = pep362.Signature(pep362_fodder.default_args)<br>
+ sig = pep362.Signature(default_args)<br>
params = list(sig)<br>
self.assertEqual(len(params), 1)<br>
self.assertEqual(params[0].name, 'a')<br>
<br>
def test_no_args(self):<br>
# Test a function with no arguments.<br>
- sig = pep362.Signature(pep362_fodder.no_args)<br>
+ sig = pep362.Signature(no_args)<br>
self.assertEqual('no_args', <a href="http://sig.name" target="_blank">sig.name</a>)<br>
self.assertTrue(not sig.var_args)<br>
self.assertTrue(not sig.var_kw_args)<br>
@@ -92,115 +119,87 @@<br>
<br>
def test_var_args(self):<br>
# Test the var_args attribute.<br>
- sig = pep362.Signature(pep362_fodder.var_args)<br>
+ sig = pep362.Signature(var_args)<br>
self.assertEqual('args', sig.var_args)<br>
self.assertEqual(0, len(list(sig)))<br>
- sig = pep362.Signature(pep362_fodder.no_args)<br>
+ sig = pep362.Signature(no_args)<br>
self.assertEqual('', sig.var_args)<br>
<br>
def test_var_kw_args(self):<br>
# Test the var_kw_args attribute and annotations.<br>
- sig = pep362.Signature(pep362_fodder.var_kw_args)<br>
+ sig = pep362.Signature(var_kw_args)<br>
self.assertEqual('var_kw_args', <a href="http://sig.name" target="_blank">sig.name</a>)<br>
self.assertEqual('kwargs', sig.var_kw_args)<br>
self.assertEqual(0, len(list(sig)))<br>
- sig = pep362.Signature(pep362_fodder.no_args)<br>
+ sig = pep362.Signature(no_args)<br>
self.assertEqual('', sig.var_kw_args)<br>
<br>
def test_parameter_positional(self):<br>
# A function with positional arguments should work.<br>
- sig = pep362.Signature(pep362_fodder.no_default_args)<br>
+ sig = pep362.Signature(no_default_args)<br>
self.assertEqual('no_default_args', <a href="http://sig.name" target="_blank">sig.name</a>)<br>
param = sig['a']<br>
self.assertEqual('a', <a href="http://param.name" target="_blank">param.name</a>)<br>
self.assertEqual(0, param.position)<br>
- self.assertTrue(not hasattr(param, 'default_value'))<br>
+ self.assertTrue(not hasattr(param, 'default'))<br>
<br>
def test_parameter_default(self):<br>
# Default parameters for a function should work.<br>
- sig = pep362.Signature(pep362_fodder.default_args)<br>
+ sig = pep362.Signature(default_args)<br>
self.assertEqual('default_args', <a href="http://sig.name" target="_blank">sig.name</a>)<br>
param = sig['a']<br>
self.assertEqual('a', <a href="http://param.name" target="_blank">param.name</a>)<br>
self.assertEqual(0, param.position)<br>
- self.assertEqual(42, param.default_value)<br>
-<br>
- @version_specific(2)<br>
- def test_parameter_tuple(self):<br>
- # A function with a tuple as a parameter should work.<br>
- sig = pep362.Signature(pep362_py2_fodder.tuple_args)<br>
- self.assertEqual('tuple_args', <a href="http://sig.name" target="_blank">sig.name</a>)<br>
- param = list(sig)[0]<br>
- self.assertTrue(isinstance(<a href="http://param.name" target="_blank">param.name</a>, tuple))<br>
- self.assertEqual(('a', ('b',)), <a href="http://param.name" target="_blank">param.name</a>)<br>
- self.assertEqual(0, param.position)<br>
- self.assertTrue(not hasattr(param, 'default_value'))<br>
+ self.assertEqual(42, param.default)<br>
<br>
- @version_specific(2)<br>
- def test_parameter_tuple_default(self):<br>
- # A default argument for a tuple parameter needs to work.<br>
- sig = pep362.Signature(pep362_py2_fodder.default_tuple_args)<br>
- self.assertEqual('default_tuple_args', <a href="http://sig.name" target="_blank">sig.name</a>)<br>
- param = list(sig)[0]<br>
- self.assertEqual(('a', ('b',)), <a href="http://param.name" target="_blank">param.name</a>)<br>
- self.assertEqual(0, param.position)<br>
- self.assertEqual((1, (2,)), param.default_value)<br>
-<br>
- @version_specific(3)<br>
def test_keyword_only(self):<br>
# Is a function containing keyword-only parameters handled properly?<br>
- sig = pep362.Signature(pep362_py3k_fodder.keyword_only)<br>
+ sig = pep362.Signature(keyword_only)<br>
param = sig['a']<br>
self.assertEqual(<a href="http://param.name" target="_blank">param.name</a>, 'a')<br>
self.assertTrue(param.keyword_only)<br>
self.assertEqual(param.position, 0)<br>
<br>
- @version_specific(3)<br>
def test_keyword_only_default(self):<br>
# Default arguments can work for keyword-only parameters.<br>
- sig = pep362.Signature(pep362_py3k_fodder.keyword_only_default)<br>
+ sig = pep362.Signature(keyword_only_default)<br>
param = sig['a']<br>
self.assertEqual(<a href="http://param.name" target="_blank">param.name</a>, 'a')<br>
self.assertTrue(param.keyword_only)<br>
self.assertEqual(param.position, 0)<br>
- self.assertEqual(param.default_value, 42)<br>
+ self.assertEqual(param.default, 42)<br>
<br>
- @version_specific(3)<br>
def test_annotations(self):<br>
# Make sure the proper annotation is found.<br>
- sig = pep362.Signature(pep362_py3k_fodder.arg_annotation)<br>
+ sig = pep362.Signature(arg_annotation)<br>
param = sig['a']<br>
self.assertEqual(<a href="http://param.name" target="_blank">param.name</a>, 'a')<br>
self.assertEqual(param.annotation, int)<br>
<br>
- @version_specific(3)<br>
def test_annotations_default(self):<br>
# Annotations with a default value should work.<br>
- sig = pep362.Signature(pep362_py3k_fodder.arg_annotation_default)<br>
+ sig = pep362.Signature(arg_annotation_default)<br>
param = sig['a']<br>
self.assertEqual(<a href="http://param.name" target="_blank">param.name</a>, 'a')<br>
self.assertEqual(param.annotation, int)<br>
- self.assertEqual(param.default_value, 42)<br>
+ self.assertEqual(param.default, 42)<br>
<br>
- @version_specific(3)<br>
def test_annotation_keyword_only(self):<br>
# Keyword-only parameters can have an annotation.<br>
- sig = pep362.Signature(pep362_py3k_fodder.arg_annotation_keyword_only)<br>
+ sig = pep362.Signature(arg_annotation_keyword_only)<br>
param = sig['a']<br>
self.assertEqual(<a href="http://param.name" target="_blank">param.name</a>, 'a')<br>
self.assertEqual(param.annotation, int)<br>
self.assertTrue(param.keyword_only)<br>
<br>
- @version_specific(3)<br>
def test_return_annotation(self):<br>
# The return value annotation.<br>
- sig = pep362.Signature(pep362_py3k_fodder.return_annotation)<br>
+ sig = pep362.Signature(return_annotation)<br>
self.assertEqual(sig.return_annotation, int)<br>
<br>
- @version_specific(3)<br>
def test_var_annotations(self):<br>
# Annotation on variable arguments (*args & **kwargs).<br>
- sig = pep362.Signature(pep362_py3k_fodder.arg_annotation_var)<br>
+ sig = pep362.Signature(arg_annotation_var)<br>
self.assertEqual(sig.var_annotations[sig.var_args], int)<br>
self.assertEqual(sig.var_annotations[sig.var_kw_args], str)<br>
<br>
@@ -224,14 +223,14 @@<br>
"""Test Signature.bind()."""<br>
<br>
def test_no_parameters(self):<br>
- sig = pep362.Signature(pep362_fodder.no_args)<br>
+ sig = pep362.Signature(no_args)<br>
binding = sig.bind()<br>
self.assertEqual({}, binding)<br>
self.assertRaises(pep362.BindError, sig.bind, 42)<br>
self.assertRaises(pep362.BindError, sig.bind, a=0)<br>
<br>
def test_var_parameters(self):<br>
- sig = pep362.Signature(pep362_fodder.var_args)<br>
+ sig = pep362.Signature(var_args)<br>
binding = sig.bind(0, 1, 2)<br>
self.assertEqual({'args':(0, 1, 2)}, binding)<br>
binding = sig.bind()<br>
@@ -239,7 +238,7 @@<br>
self.assertRaises(pep362.BindError, sig.bind, a=0)<br>
<br>
def test_var_kw_parameters(self):<br>
- sig = pep362.Signature(pep362_fodder.var_kw_args)<br>
+ sig = pep362.Signature(var_kw_args)<br>
binding = sig.bind(a=0)<br>
self.assertEqual({'kwargs':{'a':0}}, binding)<br>
binding = sig.bind()<br>
@@ -247,7 +246,7 @@<br>
self.assertRaises(pep362.BindError, sig.bind, 42)<br>
<br>
def test_positional_parameters(self):<br>
- sig = pep362.Signature(pep362_fodder.no_default_args)<br>
+ sig = pep362.Signature(no_default_args)<br>
binding = sig.bind(42)<br>
self.assertEqual({'a':42}, binding)<br>
binding = sig.bind(a=42)<br>
@@ -257,7 +256,7 @@<br>
self.assertRaises(pep362.BindError, sig.bind, b=0)<br>
<br>
def test_keyword_parameters(self):<br>
- sig = pep362.Signature(pep362_fodder.default_args)<br>
+ sig = pep362.Signature(default_args)<br>
binding = sig.bind()<br>
self.assertEqual({'a':42}, binding)<br>
binding = sig.bind(0)<br>
@@ -268,73 +267,24 @@<br>
self.assertRaises(pep362.BindError, sig.bind, a=0, b=1)<br>
self.assertRaises(pep362.BindError, sig.bind, b=1)<br>
<br>
- @version_specific(2)<br>
- def test_tuple_parameter(self):<br>
- sig = pep362.Signature(pep362_py2_fodder.tuple_args)<br>
- arg = (1, ((2,),))<br>
- binding = sig.bind(arg)<br>
- self.assertEqual({'a':1, 'b':(2,)}, binding)<br>
- self.assertRaises(pep362.BindError, sig.bind, (1,2,3))<br>
- self.assertRaises(pep362.BindError, sig.bind, (1, 2))<br>
-<br>
- @version_specific(2)<br>
- def test_default_tuple_parameter(self):<br>
- sig = pep362.Signature(pep362_py2_fodder.default_tuple_args)<br>
- binding = sig.bind()<br>
- self.assertEqual({'a':1, 'b':2}, binding)<br>
- arg = (0, (1,))<br>
- binding = sig.bind(arg)<br>
- self.assertEqual({'a':0, 'b':1}, binding)<br>
-<br>
- @version_specific(2)<br>
- def test_py2_all_args(self):<br>
- sig = pep362.Signature(pep362_py2_fodder.all_args)<br>
- # a, (b, (c,)), d=0, (e, (f,))=(4, (5,)), *g, **h<br>
- # name, position, has_default, default value<br>
- expect = (('a', 0, False, None),<br>
- (('b', ('c',)), 1, False, None),<br>
- ('d', 2, True, 0),<br>
- (('e', ('f',)), 3, True, (4, (5,))))<br>
- self.assertEqual(len(list(sig)), len(expect))<br>
- for param, check in zip(list(sig), expect):<br>
- name, pos, has_default, default_value = check<br>
- self.assertEqual(<a href="http://param.name" target="_blank">param.name</a>, name)<br>
- self.assertEqual(param.position, pos)<br>
- if has_default:<br>
- self.assertEqual(param.default_value, default_value)<br>
- else:<br>
- self.assertTrue(not hasattr(param, 'default_value'))<br>
- self.assertTrue(not param.keyword_only)<br>
- self.assertTrue(not hasattr(param, 'annotation'))<br>
- self.assertEqual(sig.var_args, 'g')<br>
- self.assertEqual(sig.var_kw_args, 'h')<br>
- self.assertEqual(len(sig.var_annotations), 0)<br>
- binding = sig.bind(0, (1, (2,)), d=3, i=7)<br>
- expected = {'a':0, 'b':1, 'c':2, 'd':3, 'e':4, 'f':5, 'g':tuple(),<br>
- 'h':{'i':7}}<br>
- self.assertEqual(expected, binding)<br>
-<br>
- @version_specific(3)<br>
def test_keyword_only(self):<br>
- sig = pep362.Signature(pep362_py3k_fodder.keyword_only)<br>
+ sig = pep362.Signature(keyword_only)<br>
binding = sig.bind(a=42)<br>
self.assertEqual(binding, {'a':42})<br>
self.assertRaises(pep362.BindError, sig.bind)<br>
self.assertRaises(pep362.BindError, sig.bind, 42)<br>
<br>
- @version_specific(3)<br>
def test_keyword_only_default(self):<br>
- sig = pep362.Signature(pep362_py3k_fodder.keyword_only_default)<br>
+ sig = pep362.Signature(keyword_only_default)<br>
binding = sig.bind()<br>
self.assertEqual(binding, {'a':42})<br>
binding = sig.bind(a=1)<br>
self.assertEqual(binding, {'a':1})<br>
self.assertRaises(pep362.BindError, sig.bind, 1)<br>
<br>
- @version_specific(3)<br>
def test_all_py3k_args(self):<br>
# a:int, d=0, *args:int, g:int, h:int=8, **kwargs:int) -> int<br>
- sig = pep362.Signature(pep362_py3k_fodder.all_args)<br>
+ sig = pep362.Signature(all_args)<br>
# name, position, kw only, has_default, default, has anno, anno<br>
expected = (('a', 0, False, False, None, True, int),<br>
('d', 1, False, True, 0, False, None),<br>
@@ -353,9 +303,9 @@<br>
else:<br>
self.assertTrue(not param.keyword_only)<br>
if has_default:<br>
- self.assertEqual(param.default_value, default)<br>
+ self.assertEqual(param.default, default)<br>
else:<br>
- self.assertTrue(not hasattr(param, 'default_value'))<br>
+ self.assertTrue(not hasattr(param, 'default'))<br>
if has_anno:<br>
self.assertEqual(param.annotation, anno)<br>
else:<br>
@@ -373,7 +323,7 @@<br>
<br>
def test_too_many_arguments(self):<br>
# Only one argument should pair up with a parameter.<br>
- sig = pep362.Signature(pep362_fodder.no_default_args)<br>
+ sig = pep362.Signature(no_default_args)<br>
self.assertRaises(pep362.BindError, sig.bind, 1, a=1)<br>
<br>
<br>
_______________________________________________<br>
Python-checkins mailing list<br>
<a href="mailto:Python-checkins@python.org" target="_blank">Python-checkins@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-checkins" target="_blank">http://mail.python.org/mailman/listinfo/python-checkins</a><br>
</blockquote></div><br></div></div></div></div></div></div>
<br>_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-dev" target="_blank">http://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="http://mail.python.org/mailman/options/python-dev/brett%40python.org" target="_blank">http://mail.python.org/mailman/options/python-dev/brett%40python.org</a><br>
<br></blockquote></div><br>