Help? :) "TypeError: foo() got multiple values for keyword argument 'omicron'

Stephen Hansen news at myNOSPAM.org
Tue May 1 02:16:05 EDT 2001


    I understand what the above mentioned error is susposed to mean, but I
just am not seeing where its coming from. The code that's producing it is
below; its basically an interface/type-safety sort of thing I fiddled with
after deciding I really liked the idea of interfaces and
optional-type-safety from listing to the ng.

    It may not be the most elegant, but I think it works so far -- I just
finished writing it and I was in the process of testing it out when the
popped up, and I'm stumped as to how to get rid of it. My debug 'prints'
seem to argue the point of there being more then one 'omicron'.

Thanks.

--Stephen
(replace 'NOSPAM' with 'seraph' to respond in email)

--- cut ---

from types import *

class Wrapper:
    def __init__(self, func):
        self.func = func
    def __call__(self, *args, **kwargs):
        fn = self.func
        if len(args) < len(fn.accept_types):
            raise TypeError, "%s requires at least %s arguments, received
%s." % (
                fn.__name__, len(fn.accept_types), len(args) )

        for i in range(len(args)):
            if not self._check_sanity(fn, i, args[i]):
                raise TypeError, self._error_str(i + 1,
                        fn.accept_types[i], fn.accept_implements[i])
        for key in kwargs.keys():
            if not self._check_sanity(fn, key, kwargs[key]):
                raise TypeError, self._error_str(i + 1,
                        fn.kw_accept_types[key],
fn.kw_accept_implements[key])
        print args
        print kwargs
        return apply(fn, args, kwargs)

    def _check_sanity(self,func, pos, arg):
        if type(pos) == IntType:
            typefunc = func.accept_types
            implfunc = func.accept_implements
        else:
            typefunc = func.kw_accept_types
            implfunc = func.kw_accept_implements

        if typefunc[pos] != None and type(arg) in typefunc[pos]:
            return 1
        elif implfunc[pos] != None:
            for fn in implfunc[pos]:
                if not hasattr(arg, fn):
                    return 0
        return 1
    def _error_str(self,pos, types, funcs):
        return "Argument %s must be of types %s or implement the " \
               "methods %s" % \
                ( pos,
                  map(lambda x: str(x)[7:-2].capitalize(), types),
                  list(funcs)
                )

class Test:
    def foo(alpha, beta, omicron=None, omega=[1, 2, 3]):
        """ Foo accepts four arguments:
        alpha: a string
        beta: a file object, string, or None
        omicron: a number
        omega: a list (defaults to [1,2,3] )"""
        print "In Foo: Alpha: %s Beta: %s Omicron: %s Omega: %s" % (
            alpha, beta, omicron, omega)
    foo.accept_types = ( (StringType,), (FileType, NoneType ) )
    foo.accept_implements = ( None, ('readline', ) )
    foo.kw_accept_types = {"omicron": (StringType, None ), "omega":
(ListType, None) }
    foo.kw_accept_implements = {"omicron": None,
                                "omega": ('__getitem__', '__setitem__') }

t = Test()
tst = Wrapper(t.foo)
tst("String", None, omicron=5, omega=[1,2,3])







More information about the Python-list mailing list