TypeError
TypeError: function() got an unexpected keyword argument 'x'
Traceback
Traceback (most recent call last):
File "main.py", line 4, in <module>
create_user(name="Alice", emial="[email protected]") # typo
TypeError: function() got an unexpected keyword argument 'x'What causes this error
A keyword argument was passed that does not match any parameter in the function signature. Typos, API changes, or incorrect argument forwarding are common triggers.
How to fix it
Check the function's parameter names and fix the typo. Consult the library documentation for the correct API. Use IDE autocompletion to avoid typos in keyword arguments.
Code that causes this error
def create_user(name, email):
return {"name": name, "email": email}
create_user(name="Alice", emial="[email protected]") # typoFixed code
def create_user(name, email):
return {"name": name, "email": email}
create_user(name="Alice", email="[email protected]")About TypeError
This error is raised when a function is called with a keyword argument that does not match any parameter name. This happens when there is a typo in the argument name, when the function signature changed between versions of a library, or when passing keyword arguments to a function that uses `*args` but not `**kwargs`. The error message specifies the unexpected keyword, making the typo easy to spot.
In class inheritance, this can occur when __init__ signatures do not align between parent and child classes and keyword arguments are forwarded with `super().__init__(**kwargs)`. When designing APIs, accepting `**kwargs` provides flexibility but makes it harder to catch typos — consider using explicit parameters with clear names instead.
Common scenarios
Performing operations between incompatible types like strings and integers
Calling methods that return None and chaining operations on the result
Passing the wrong number or type of arguments to a function
Using bracket notation on objects that do not support indexing