From 660f1071cf765cdcebb6675a1ce5f0f925cbcb3a Mon Sep 17 00:00:00 2001 From: "steven.bethard" Date: Sat, 12 Sep 2009 16:08:15 +0000 Subject: Fix bug where classic classes were being rejected as type= arguments. --- argparse.py | 5 +++-- test/test_argparse.py | 29 ++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/argparse.py b/argparse.py index cb77a4a..89d6f49 100644 --- a/argparse.py +++ b/argparse.py @@ -2125,8 +2125,9 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): def _get_value(self, action, arg_string): type_func = self._registry_get('type', action.type, action.type) if not hasattr(type_func, '__call__'): - msg = _('%r is not callable') - raise ArgumentError(action, msg % type_func) + if not hasattr(type_func, '__bases__'): # classic classes + msg = _('%r is not callable') + raise ArgumentError(action, msg % type_func) # convert the value to the appropriate type try: diff --git a/test/test_argparse.py b/test/test_argparse.py index 6c8ee06..ad2be1a 100644 --- a/test/test_argparse.py +++ b/test/test_argparse.py @@ -1420,12 +1420,9 @@ class TestTypeUserDefined(ParserTestCase): def __eq__(self, other): return (type(self), self.value) == (type(other), other.value) - def get_my_type(value): - return TestTypeUserDefined.MyType(value) - argument_signatures = [ - Sig('-x', type=get_my_type), - Sig('spam', type=get_my_type), + Sig('-x', type=MyType), + Sig('spam', type=MyType), ] failures = [] successes = [ @@ -1434,6 +1431,28 @@ class TestTypeUserDefined(ParserTestCase): ] +class TestTypeClassicClass(ParserTestCase): + """Test a classic class type""" + + class C: + + def __init__(self, value): + self.value = value + + def __eq__(self, other): + return (type(self), self.value) == (type(other), other.value) + + argument_signatures = [ + Sig('-x', type=C), + Sig('spam', type=C), + ] + failures = [] + successes = [ + ('a -x b', NS(x=C('b'), spam=C('a'))), + ('-xf g', NS(x=C('f'), spam=C('g'))), + ] + + class TestTypeRegistration(TestCase): """Test a user-defined type by registering it""" -- cgit v1.2.1