-->
With Examples Fix attributeerror: 'function' object has no attribute func and AttributeError: 'function' object has no attribute assert_called Python.

With Examples Fix attributeerror: 'function' object has no attribute func and AttributeError: 'function' object has no attribute assert_called Python.

Back to top

Updated by Ashirafu Kibalama on September 17, 2024

Why do you see "AttributeError: 'function' object has no attribute 'func' and AttributeError: 'function' object has no attribute assert_called in Python:" A Comprehensive Guide with Examples





Struggling with the AttributeError: 'function' object has no attribute 'func' and AttributeError: 'function' object has no attribute 'assert_called' Python.


With detailed explanations and real-world examples, you are to learn how these errors arise and how to fix them, helping you write more robust Python code.


Fix or Resolve attributeerror: 'function' object has no attribute func Python With Examples.




The error AttributeError: 'function' object has no attribute 'func' typically occurs in Python when you mistakenly treat a function object as though it has an attribute called func, which doesn't exist.


To fix this error, you must ensure you're correctly calling a function or not confusing it with another object type (e.g., class or method).


2 Examples Where attributeerror: 'function' object has no attribute func Python might occur and how to fix each of them:


Example 1: Calling a Non-Existent Attribute on a Function


Codes Before Fixing attributeerror: 'function' object has no attribute func Python


def greet():
print("Hello!")

# Trying to access a non-existent attribute 'func'
greet.func()


In this case, greet is a function that doesn't have an attribute called func. The error occurs because you're trying to call an attribute (func()) that doesn't exist.


Output:





Solution:

Make sure you are calling something other than a non-existent attribute or method. If you want to call the function itself, call greet() like this:


Codes After Fixing attributeerror: 'function' object has no attribute func Python


# fix

def greet():
print("Hello!")


# # Trying to access a non-existent attribute 'func'
# greet.func()

greet() # Correct call


Output:




Example 2: Incorrect Use of Decorator Function


Codes Before Fixing attributeerror: 'function' object has no attribute func Python



def decorator_func(func):
def wrapper():
print("Before function call")
func()
print("After function call")

return wrapper


# Defining a function to be decorated
def my_function():
print("Function is running")


# Applying the decorator
decorated = decorator_func(my_function)

# Incorrectly trying to access a 'func' attribute
decorated.func()


Here, decorated is the wrapped function returned by decorator_func, which doesn't have an attribute called func.


Output:




Solution:

To fix this, call the decorated function like this:


Codes After Fixing attributeerror: 'function' object has no attribute func Python


def decorator_func(func):
def wrapper():
print("Before function call")
func()
print("After function call")

return wrapper


# Defining a function to be decorated
def my_function():
print("Function is running")


# Applying the decorator
decorated = decorator_func(my_function)

# # Incorrectly trying to access a 'func' attribute
# decorated.func()


decorated() # Correct call


Output:



In Summary, to fix attributeerror: 'function' object has no attribute func Python:


  • Call the correct function:

Ensure you are calling the function itself instead of trying to access an attribute that doesn't exist.


  • Decorators:

Ensure you call the decorated function directly instead of attempting to access attributes not present in the returned function object.


Fix or Resolve AttributeError: 'function' object has no attribute 'assert_called' Python With Examples.


The error AttributeError: 'function' object has no attribute 'assert_called' occurs when attempting to call the assert_called method on something that is not a mock object but rather a regular function.

This typically occurs when using Python's unittest.mock library.


An Example Where AttributeError: 'function' object has no attribute assert_called Python might occur and how to fix each of them:


Example: Forgetting to Mock a Class Method


You're testing a class method but forgetting to mock another method the class uses internally, resulting in an error.


Codes Before Fixing AttributeError: 'function' object has no attribute 'assert_called' Python



from unittest import mock

class MyClass:
def method(self):
self.other_method()

def other_method(self):
pass

# Test case
obj = MyClass()
obj.method()

# Trying to assert that other_method was called
obj.other_method.assert_called() # AttributeError: 'function' object has no attribute 'assert_called'


other_method is an instance method, not a mock object, so it doesn't have the assert_called() method.


Output:




Solution:

Mock the class method other_method before calling method() using mock.patch.object.


Codes After Fixing AttributeError: 'function' object has no attribute 'assert_called' Python


# fix

from unittest import mock
import unittest

class MyClass:
def method(self):
self.other_method()

def other_method(self):
pass

class TestMyClass(unittest.TestCase):
@mock.patch.object(MyClass, 'other_method')
def test_method(self, mock_other_method):
obj = MyClass()
obj.method()
mock_other_method.assert_called() # Now it works!

if __name__ == "__main__":
unittest.main()



Output:




  • mock.patch.object(MyClass, 'other_method') replaces other_method with a mock object during the test.
  • This allows mock_other_method.assert_called() to be used without error.


In Summary, to fix the AttributeError: 'function' object has no attribute 'assert_called' Python:


  • Ensure Correct Timing: Mock functions before they are called.
  • Mock Class Methods Properly: Use mock.patch.object() to mock methods in classes.


Following these techniques, you can resolve the attributeerror: 'function' object has no attribute func and AttributeError: 'function' object has no attribute 'assert_called' error in various contexts.


Other Posts:

1 With Examples Fix: Python Object Has No Attribute, but it Does.


2 With Examples Fix: AttributeError: 'function' object has no attribute 'patch and Attributeerror: 'function' object has no attribute count Python


3 With Examples Fix: ModuleNotFoundError: No module named 'module' and ImportError: attempted relative import with no known parent package Python


4 With Examples Fix: attributeerror: 'function' object has no attribute register and AttributeError: 'function' object has no attribute 'get Python


5  With Examples fix: AttributeError: 'function' object has no attribute 'glob and AttributeError: 'function' object has no attribute 'get_extra_actions Python

6 How to Fix AttributeError: 'dict_values' object has no attribute 'update_relative' in Python With Examples.