Skip to content

Fix: Python TypeError: missing required positional argument

FixDevs ·

Quick Answer

How to fix Python TypeError missing required positional argument caused by missing self, wrong argument count, class instantiation errors, and decorator issues.

The Error

You call a function or method and get:

TypeError: __init__() missing 1 required positional argument: 'name'

Or variations:

TypeError: greet() missing 1 required positional argument: 'name'
TypeError: process_data() missing 2 required positional arguments: 'data' and 'format'
TypeError: send_email() missing 1 required positional argument: 'self'

Python called a function or method but did not receive all the arguments it requires. The function expects more arguments than were provided.

Why This Happens

Python functions have a fixed set of required parameters (those without default values). When you call a function without providing all required arguments, Python raises TypeError before the function body even executes.

Common causes:

  • Missing self error. You called an instance method on the class itself instead of on an instance.
  • Wrong number of arguments. You forgot to pass one or more required arguments.
  • Missing parentheses. You referenced the class instead of instantiating it.
  • Decorator issue. A decorator consumed an argument or changed the function signature.
  • Callback signature mismatch. A callback function expects more arguments than the caller provides.
  • Refactored function. The function signature changed but not all call sites were updated.

Fix 1: Fix the “missing self” Error

The most common case. If the error says missing 1 required positional argument: 'self', you called a method on the class instead of an instance:

Broken:

class User:
    def __init__(self, name):
        self.name = name

    def greet(self):
        return f"Hello, {self.name}!"

# Calling on the class (wrong):
User.greet()  # TypeError: greet() missing 1 required positional argument: 'self'

Fixed — create an instance first:

user = User("Alice")
user.greet()  # "Hello, Alice!"

When you call user.greet(), Python automatically passes user as self. When you call User.greet(), there is no instance to pass as self, so it is missing.

Broken — forgot to instantiate:

class Database:
    def __init__(self, url):
        self.url = url

    def connect(self):
        print(f"Connecting to {self.url}")

# Missing parentheses — Database is the class, not an instance:
db = Database
db.connect()  # TypeError: connect() missing 1 required positional argument: 'self'

Fixed:

db = Database("postgresql://localhost/mydb")
db.connect()  # Works

Pro Tip: If you see missing 1 required positional argument: 'self', the fix is almost always to change ClassName.method() to instance.method(). The self argument is never passed explicitly by the caller — Python handles it when you call a method on an instance.

Fix 2: Pass All Required Arguments

Check the function signature and provide every argument that does not have a default value:

def send_email(to, subject, body):
    print(f"Sending to {to}: {subject}")

# Missing arguments:
send_email("[email protected]")
# TypeError: send_email() missing 2 required positional arguments: 'subject' and 'body'

# Fixed — pass all arguments:
send_email("[email protected]", "Hello", "How are you?")

Use keyword arguments for clarity:

send_email(
    to="[email protected]",
    subject="Hello",
    body="How are you?"
)

Add default values for optional parameters:

def send_email(to, subject="No Subject", body=""):
    print(f"Sending to {to}: {subject}")

# Now only 'to' is required:
send_email("[email protected]")  # Works

Fix 3: Fix init Arguments

When creating instances, the error tells you what argument __init__ expects:

class Config:
    def __init__(self, filepath, encoding="utf-8"):
        self.filepath = filepath
        self.encoding = encoding

# Missing filepath:
config = Config()
# TypeError: __init__() missing 1 required positional argument: 'filepath'

# Fixed:
config = Config("config.yaml")
config = Config(filepath="config.yaml", encoding="latin-1")

Common in Django models:

# Django model
class Article(models.Model):
    title = models.CharField(max_length=200)

# Creating without required field:
article = Article()
article.save()
# IntegrityError or TypeError depending on validation

For Django database errors, see Fix: Django OperationalError: no such table.

Fix 4: Fix Static and Class Methods

If a method does not need self, declare it as @staticmethod or @classmethod:

Broken — method does not use self but still requires it:

class MathHelper:
    def add(a, b):  # Missing 'self' parameter
        return a + b

helper = MathHelper()
helper.add(2, 3)
# TypeError: add() takes 2 positional arguments but 3 were given
# (Python passed 'self' as the first argument, shifting a and b)

Fixed — use @staticmethod:

class MathHelper:
    @staticmethod
    def add(a, b):
        return a + b

MathHelper.add(2, 3)  # Works — no self needed
helper = MathHelper()
helper.add(2, 3)       # Also works

Fixed — use @classmethod if you need the class:

class Config:
    default_path = "/etc/config.yaml"

    @classmethod
    def from_default(cls):
        return cls(cls.default_path)

    def __init__(self, path):
        self.path = path

config = Config.from_default()  # Works — cls is passed automatically

Fix 5: Fix Callback and Lambda Arguments

When passing functions as callbacks, the callback signature must match what the caller expects:

Broken — callback expects more arguments than provided:

def on_complete(result, status):
    print(f"Done: {result} ({status})")

# The caller only passes one argument:
results = [1, 2, 3]
list(map(on_complete, results))
# TypeError: on_complete() missing 1 required positional argument: 'status'

Fixed — match the expected signature:

def on_complete(result):
    print(f"Done: {result}")

list(map(on_complete, results))

Or provide defaults:

def on_complete(result, status="success"):
    print(f"Done: {result} ({status})")

list(map(on_complete, results))  # Works — status defaults to "success"

Broken — sorted() key function with wrong signature:

def compare(a, b):
    return a - b

# sorted() key takes one argument, not two:
sorted([3, 1, 2], key=compare)
# TypeError: compare() missing 1 required positional argument: 'b'

Fixed:

sorted([3, 1, 2], key=lambda x: x)  # Key takes one argument

Fix 6: Fix Decorator Issues

Decorators can change how arguments are passed:

Broken — decorator that does not pass arguments through:

def my_decorator(func):
    def wrapper():  # Missing *args, **kwargs!
        print("Before")
        func()      # Missing arguments!
        print("After")
    return wrapper

@my_decorator
def greet(name):
    print(f"Hello, {name}")

greet("Alice")
# TypeError: wrapper() takes 0 positional arguments but 1 was given

Fixed:

def my_decorator(func):
    def wrapper(*args, **kwargs):
        print("Before")
        result = func(*args, **kwargs)
        print("After")
        return result
    return wrapper

@my_decorator
def greet(name):
    print(f"Hello, {name}")

greet("Alice")  # Works

Always use *args, **kwargs in decorator wrappers to forward all arguments.

Common Mistake: Writing a decorator that works for functions with no arguments but breaks when applied to functions with arguments. Always use *args, **kwargs in the wrapper function. Use functools.wraps to preserve the original function’s name and docstring:

from functools import wraps

def my_decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper

Fix 7: Fix Inheritance and super() Calls

When overriding methods in subclasses, make sure you pass the required arguments to super():

Broken:

class Animal:
    def __init__(self, name, species):
        self.name = name
        self.species = species

class Dog(Animal):
    def __init__(self, name):
        super().__init__(name)  # Missing 'species'!
        # TypeError: __init__() missing 1 required positional argument: 'species'

Fixed:

class Dog(Animal):
    def __init__(self, name):
        super().__init__(name, "Dog")

Fix 8: Debug the Error

When the error is not obvious, inspect the function signature:

import inspect

# See the function signature:
print(inspect.signature(my_function))

# See all parameters:
for name, param in inspect.signature(my_function).parameters.items():
    print(f"{name}: {param.kind.name}, default={param.default}")

This shows you exactly which parameters are required and which have defaults.

Use IDE features:

Most IDEs (VS Code, PyCharm) show function signatures as you type. Hover over the function name to see the expected arguments.

Check for method vs function:

print(type(obj.method))    # <class 'method'> — self is passed automatically
print(type(MyClass.method)) # <class 'function'> — self must be passed manually

If you see function where you expect method, you are calling it on the class instead of an instance.

Still Not Working?

If you have checked all the fixes above:

Check for __init__ in metaclasses or __new__. If the class uses a custom metaclass or overrides __new__, the error might originate there instead of __init__.

Check for property setters. A @property setter must accept exactly one value argument:

class Config:
    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):  # Must have 'value'
        self._name = value

Check for *args and **kwargs masking. If a base class accepts **kwargs and a subclass does not, arguments intended for the subclass might be swallowed:

class Base:
    def __init__(self, **kwargs):
        pass

class Child(Base):
    def __init__(self, name):  # 'name' must be passed positionally
        super().__init__()
        self.name = name

# This fails:
Child(name="Alice")  # Works
Child()               # TypeError: missing 'name'

Check for circular imports. If a circular import causes a module to be partially loaded, classes or functions might be None or incomplete, leading to unexpected TypeError. See Fix: Python ImportError: circular import.

Check for Python version differences. Some functions changed their signatures between Python versions. For example, collections.OrderedDict.move_to_end() was added in Python 3.2. Check the Python docs for your version. If Python itself is not found, see Fix: python command not found.

F

FixDevs

Solo developer based in Japan. Every solution is cross-referenced with official documentation and tested before publishing.

Was this article helpful?

Related Articles