Python
error debugging
programming
set_model function
positional arguments

set_model missing 1 required positional argument 'model'

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

The error TypeError: set_model() missing 1 required positional argument: 'model' occurs when you call an instance method without an instance, or when you pass one fewer argument than the method signature expects. In Python, instance methods automatically receive self as the first argument when called on an object. If you call the method on the class itself (e.g., MyClass.set_model(m) instead of instance.set_model(m)), Python does not inject self, so your model argument fills the self slot and the actual model parameter is left empty. This error commonly appears with Keras/scikit-learn wrappers, ORMs, and any code using a set_model method.

The Root Cause

python
1class Trainer:
2    def set_model(self, model):
3        self.model = model
4
5# CORRECT — calling on an instance
6trainer = Trainer()
7trainer.set_model(my_model)  # self=trainer, model=my_model
8
9# BROKEN — calling on the class
10Trainer.set_model(my_model)
11# TypeError: set_model() missing 1 required positional argument: 'model'
12# Python interprets: self=my_model, model=???

When you call Trainer.set_model(my_model), Python treats my_model as self and has nothing left for model. The fix is to call it on an instance.

Keras Callback Example

python
1import tensorflow as tf
2
3class CustomCallback(tf.keras.callbacks.Callback):
4    def on_epoch_end(self, epoch, logs=None):
5        print(f"Epoch {epoch}: loss={logs['loss']:.4f}")
6
7# BROKEN — passing the class instead of an instance
8model.fit(X, y, callbacks=[CustomCallback])
9# TypeError: set_model() missing 1 required positional argument: 'model'
10
11# FIX — pass an instance (note the parentheses)
12model.fit(X, y, callbacks=[CustomCallback()])

Keras internally calls callback.set_model(self) on each callback. If you pass the class CustomCallback instead of an instance CustomCallback(), Keras tries to call set_model as an unbound method, causing the error.

Scikit-Learn Wrapper Example

python
1from sklearn.base import BaseEstimator
2
3class MyEstimator(BaseEstimator):
4    def set_model(self, model):
5        self.model_ = model
6        return self
7
8    def fit(self, X, y):
9        # ... training logic
10        return self
11
12# BROKEN — forgot to instantiate
13estimator = MyEstimator
14estimator.set_model(some_model)
15# TypeError: set_model() missing 1 required positional argument: 'model'
16
17# FIX — instantiate with parentheses
18estimator = MyEstimator()
19estimator.set_model(some_model)

Inheritance and super() Issues

python
1class BaseTrainer:
2    def set_model(self, model):
3        self.model = model
4
5class CustomTrainer(BaseTrainer):
6    def set_model(self, model):
7        # BROKEN — calling on the class, not the instance
8        BaseTrainer.set_model(model)
9        # TypeError: set_model() missing 1 required positional argument: 'model'
10
11        # FIX 1 — pass self explicitly
12        BaseTrainer.set_model(self, model)
13
14        # FIX 2 — use super() (preferred)
15        super().set_model(model)
16
17        self.custom_setup()

When calling a parent class method directly (without super()), you must pass self explicitly. Using super() is cleaner and handles MRO correctly.

Static Method vs Instance Method

python
1class ModelManager:
2    # Instance method — requires self
3    def set_model(self, model):
4        self.model = model
5
6    # Static method — no self needed
7    @staticmethod
8    def validate_model(model):
9        return model is not None
10
11    # Class method — cls instead of self
12    @classmethod
13    def from_config(cls, config):
14        instance = cls()
15        instance.set_model(config['model'])
16        return instance
17
18# Static methods work without an instance
19ModelManager.validate_model(my_model)  # OK
20
21# Instance methods require an instance
22ModelManager.set_model(my_model)  # ERROR
23ModelManager().set_model(my_model)  # OK

If set_model does not need instance state, consider making it a @staticmethod or @classmethod.

Debugging the Error

python
1# Check if you have a class or an instance
2print(type(trainer))
3# <class 'type'> → you have a CLASS, not an instance
4# <class '__main__.Trainer'> → you have an INSTANCE
5
6# Quick check
7import inspect
8print(inspect.isclass(trainer))  # True = class, False = instance
9
10# Common pattern: factory function returning class instead of instance
11def create_trainer():
12    return Trainer  # BUG: returns the class
13
14trainer = create_trainer()
15trainer.set_model(model)  # TypeError
16
17def create_trainer():
18    return Trainer()  # FIX: returns an instance

Common Pitfalls

  • Missing parentheses during instantiation: Writing callback = MyCallback assigns the class itself, not an instance. Always use MyCallback() with parentheses. This is the most common cause of the error, especially in Keras callback lists.
  • Calling parent methods without self: BaseClass.method(arg) treats arg as self. Use super().method(arg) or BaseClass.method(self, arg) to pass both self and the argument correctly.
  • Confusing class and instance in factory functions: A factory that returns MyClass instead of MyClass() silently gives you a class reference. Downstream method calls then fail because there is no instance to bind self to.
  • Decorating with @staticmethod when self is needed: If you accidentally add @staticmethod to a method that accesses self.model, the first argument is no longer self — it becomes the model parameter. Remove the decorator or adjust the method signature.
  • Passing class references in configuration dictionaries: Frameworks like Keras accept callbacks as a list of instances. Passing [CustomCallback] (class) instead of [CustomCallback()] (instance) causes the framework's internal set_model() call to fail with this exact error.

Summary

  • The error means Python received one fewer argument than expected because self was not automatically provided
  • Most common cause: calling an instance method on a class instead of an instance (missing () during instantiation)
  • Use super().method(arg) instead of ParentClass.method(arg) in subclasses
  • Check type(obj) or inspect.isclass(obj) to verify you have an instance, not a class
  • In Keras, always pass callback instances (MyCallback()) not classes (MyCallback) in the callbacks list

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.