TypePython Error

TypeError

TypeError: function() takes 2 positional arguments but 3 were given

Traceback

terminal
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    add(1, 2, 3)
TypeError: function() takes 2 positional arguments but 3 were given

What causes this error

More positional arguments were passed to a function than it is defined to accept. In methods, forgetting that self counts as an argument is a common trigger.

How to fix it

Check the function's expected parameter count. For methods, remember self is implicit. Use *args if the function should accept variable numbers of arguments. Remove extra arguments from the call.

Code that causes this error

Broken
def add(a, b):
    return a + b

add(1, 2, 3)

Fixed code

Fixed
def add(*args):
    return sum(args)

print(add(1, 2, 3))  # 6

About TypeError

This error is raised when more positional arguments are passed to a function than its signature allows. The error message shows exactly how many arguments were expected versus how many were received. A particularly common variant occurs in class methods where developers forget that `self` counts as the first argument.

If you define `def method(self, x)` and call `obj.method(1, 2)`, Python sees three arguments: self, 1, and 2 — but the method only accepts two (self and x). This same issue applies when a regular function is accidentally called as a method, or when extra arguments slip in through unpacking. The error can also indicate a design issue where the function should accept `*args` for variadic arguments.

Common scenarios

1

Performing operations between incompatible types like strings and integers

2

Calling methods that return None and chaining operations on the result

3

Passing the wrong number or type of arguments to a function

4

Using bracket notation on objects that do not support indexing

Related errors