Skip to main content

How to Understand Common Python TypeErrors: missing 'self' and missing 'Loader'

TypeError exceptions are common in Python, often indicating a mismatch between how a function or method is called and its definition.

This guide addresses two frequent TypeError messages: "missing 1 required positional argument: 'self'" and "load() missing 1 required positional argument: 'Loader'", explaining their causes and providing clear solutions.

TypeError: missing 1 required positional argument: 'self'​

This error typically occurs when you try to call an instance method directly on a class itself, instead of on an instance (object) of that class.

Cause: Calling Instance Methods on the Class​

Instance methods in Python require an instance (usually referred to as self by convention) as their first argument. When you call the method on the class, this instance is missing.

# Error Example
class Employee():
def __init__(self, name, salary):
self.name = name
self.salary = salary

def get_name(self): # Instance method expecting 'self'
return self.name

# ⛔️ TypeError: Employee.get_name() missing 1 required positional argument: 'self'
try:
print(Employee.get_name())
except TypeError as e:
print(e)

Solution: Instantiate the Class​

To fix this, create an instance of the class and then call the method on that instance. Python automatically passes the instance (self) as the first argument.

# Correct Usage
class Employee():
def __init__(self, name, salary):
self.name = name
self.salary = salary

def get_name(self):
return self.name

emp1 = Employee('Tom Nolan', 100) # Instantiate the class
print(emp1.get_name()) # Call the method on the instance -> Output: Tom Nolan

Alternatives: @staticmethod and @classmethod​

If a method doesn't actually need access to instance-specific data (self), consider making it a staticmethod (doesn't receive self or the class) or a classmethod (receives the class (cls) as the first argument).

class Employee():
# ... (init method) ...

@staticmethod
def static_method_example():
return 'This is a static method'

@classmethod
def class_method_example(cls):
print(f"Called from class: {cls.__name__}")
return 'This is a class method'

print(Employee.static_method_example()) # Call static method on class
print(Employee.class_method_example()) # Call class method on class
  • These decorators change how the method behaves and are suitable when you don't need the instance context.

TypeError: load() missing 1 required positional argument: 'Loader' (PyYAML)​

This specific error arises when using the yaml.load() function from the PyYAML library without specifying the mandatory Loader argument.

Cause: Changes in PyYAML Library​

For security reasons, recent versions of PyYAML require the Loader argument to be explicitly provided to yaml.load() to prevent potential execution of arbitrary code from untrusted YAML sources.

# Error Example
import yaml

document = """
a: 1
b:
c: 3
d: 4
"""
try:
# ⛔️ TypeError: load() missing 1 required positional argument: 'Loader'
data = yaml.load(document)
print(yaml.dump(data))
except TypeError as e:
print(e)

Solutions: Using safe_load() or full_load()​

The recommended way to handle this is to use the safer alternatives provided by PyYAML:

  • yaml.safe_load(): This is the preferred method for loading YAML from untrusted sources. It only loads standard YAML tags and prevents arbitrary code execution.
  • yaml.full_load(): Loads the full YAML language, including complex Python objects. Use with caution, only on trusted input, as it can be insecure.
# Correct Usage
import yaml

document = """
a: 1
b:
c: 3
d: 4
"""

# Using safe_load (Recommended for untrusted data)
safe_data = yaml.safe_load(document)
print("Safe Load Output:")
print(yaml.dump(safe_data))

# Using full_load (Use only with trusted data)
full_data = yaml.full_load(document)
print("\nFull Load Output:")
print(yaml.dump(full_data))

Alternative: Explicitly Providing the Loader​

You can still use yaml.load() by explicitly passing a Loader:

import yaml

document = """
a: 1
b:
c: 3
d: 4
"""

# Explicitly providing SafeLoader
data_safe = yaml.load(document, Loader=yaml.SafeLoader)
print("Load with SafeLoader:")
print(yaml.dump(data_safe))

# Explicitly providing FullLoader (Use with caution)
data_full = yaml.load(document, Loader=yaml.FullLoader)
print("\nLoad with FullLoader:")
print(yaml.dump(data_full))
note

Using yaml.safe_load() is generally safer and more readable than explicitly passing Loader=yaml.SafeLoader.

Conclusion​

Understanding the context of TypeError exceptions is key to resolving them.

  • The "missing 'self'" error indicates an incorrect call to an instance method, solved by instantiating the class.
  • The "missing 'Loader'" error in PyYAML requires using safer functions like yaml.safe_load() or explicitly providing a Loader due to security updates.

Addressing these errors correctly ensures your Python code functions as intended and remains secure.