Python decorator to ensure that kwargs are correct
<p>I have done a decorator that I used to ensure that the keyword arguments passed to a constructor are the <strong>correct/expected ones</strong>. The code is the following:
<code></p>

<pre><code>from functools import wraps

def keyargs_check(keywords):
"""
This decorator ensures that the keys passed in kwargs are the onces that
are specified in the passed tuple. When applied this decorate will
check the keywords and will throw an exception if the developer used
one that is not recognized.

@type keywords: tuple
@param keywords: A tuple with all the keywords recognized by the function.
"""

def wrap(f):
    @wraps(f)
    def newFunction(*args, **kw):
        # we are going to add an extra check in kw
        for current_key in kw.keys():
            if not current_key in keywords:
                raise ValueError(
                    "The key {0} is a not recognized parameters by {1}.".format(
                        current_key, f.__name__))
        return f(*args, **kw)
    return newFunction
return wrap
</code></pre>

<p></code>
An example use of this decorator would be the following:
<code></p>

<pre><code>class Person(object):

@keyargs_check(("name", "surname", "age"))
def __init__(self, **kwargs):
    # perform init according to args
</code></pre>

<p></code>
Using the above code if the developer passes a key args like "blah" it will <strong>throw an exception</strong>. Unfortunately my implementation has a major problem with inheritance, if I define the following:
<code></p>

<pre><code>class PersonTest(Person):

@keyargs_check(("test"))
def __init__(self, **kwargs):
    Person.__init__(self,**kwargs)
</code></pre>

<p></code></p>

<p>Because I'm passing kwargs to the super class init method, I'm going to get an exception because "test" <strong>is not in the tuple</strong> passed to the decorator of the super class. Is there a way to let the decorator used in the super class to know about the extra keywords? or event better, is there a standard way to achieve what I want?</p>

<p>Update: I am more interested in automate the way I throw an exception when a developer passes the wrong kwarg rather than on the fact that I use kwargs instead of args. What I mean is, I do not want have to write the code that check the args passed to the method in every class.</p>
