TypeError: function() got an unexpected keyword argument
For more information about argument types and parameter types, see Argument Types in Python Functions, Parameter Types in Python Functions.
Explanation of the Error
This type of error can occur both in standalone functions and in class methods that are also functions.
Function:
... function(param="value") TypeError: function() got an unexpected keyword argument 'param'
__init__
method:
... class_instance = MyClass(param="value") TypeError : MyClass.__init__() got an unexpected keyword argument 'param'
Other methods of the class:
... class_instance.method(param="value") TypeError: MyClass.method() got an unexpected keyword argument 'param'
What does the error message mean?
In short, this error message means that the keyword argument you're passing doesn't match the function parameters.
The function does not expect to pass such a keyword argument (parameter=value pair) when it is called.
Cases Where an Error Happens
There are several possible cases where this error can occur.
For example, inattention. The name of the parameter may be spelled incorrectly.
def function(param): pass function(parm="value") # TypeError: function() got an unexpected keyword argument 'parm' # correct: function(param="value")
We can also pass values to the wrong class or function. Especially if their names are similar.
choice
and choices
(Python 3.6+).random.choice(seq)
random.choices(population, weights=None, *, cum_weights=None, k=1)
choice
method with a keyword argument that belongs to the choices
method, we get an error.import random r = random.choice([0, 1, 2, 3], k=10) # TypeError: Random.choice() got an unexpected keyword argument 'k'
A function may take no arguments at all.
def function(): pass function(param="value") # TypeError: function() got an unexpected keyword argument 'param' # correct: function()
When it comes to classes, a class method may not accept any additional arguments. In this case, the method will have only one parameter - self
.
class A: def __init__(self): pass def method(self, param): pass a = A(param="value") # TypeError: A.__init__() got an unexpected keyword argument 'param' # correct: # a = A() # a.method(param="value")
This error can also occur if the function specifies that an arbitrary sequence of positional arguments can be provided (*args). Keyword arguments cannot be used in such functions.
def function(*args): pass function(param="value") # TypeError: function() got an unexpected keyword argument 'param' # correct: function("value")
Same with class methods.
class A: def __init__(self, *args): pass a = A(param="value") # TypeError: A.__init__() got an unexpected keyword argument 'param' # correct: a = A("value")
The most confusing TypeError can happen when we use external libraries, packages from PyPI, or modules from the Python Standard Library. This error may mean that there is a compatibility issue between package versions or Python versions.
TypeError When Using Python Libraries, Packages, PSL Modules
Popular libraries that people use are constantly evolving, adding new features and updating old functions and classes.
If we take the matplotlib changelog for example, we can see that some parameters in public functions can be renamed or removed. To extend the capabilities of class methods, developers can also add new parameters to them.
Each major version of a package contains changes that are incompatible with previous versions. With a new version of the package, the minimum supported versions of other dependency libraries may also be increased.
So, when you try to run some code in your environment and encounter a TypeError with an unexpected keyword argument, make sure you have the appropriate version of the package installed. In some cases, it may also matter which version of Python you have.
Python versions also differ from each other. For example, there is a run function in the subprocess module. This function has quite a long list of parameters. If we scroll down to the end of the description of this feature, we will see notes related to its development. New parameters were added to this function in different versions of Python. So, if we want to use this function with a specific parameter, we need to make sure we have the appropriate version of Python.