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