TypeError: argument 1 has unexpected type NoneType

TypeError: argument 1 has unexpected type NoneType is an error which happens when your function does not return anything in Python.

In this BIog Post, I am going to explain what has usually happened in the background when you got this error and how you can solve it with a main solution which can possibly solve the issue.

Explaining the Error : TypeError: argument 1 has unexpected type NoneType

This is how the important line of the error message would look like if you had this error.

                                                                       #
TypeError: argument 1 has unexpected type 'NoneType'
                                                                       #

We will try replicating this error, in this example we are using PyQt.  Here is the example we will be working with.

                                                                       #

app = QApplication(sys.argv)
cocktail = loadUi('create.ui')

def mixCocktail(str):
      cocktail.show()
      cocktail.showFullScreen()
      cocktail.lbl_header.setText(str)

widget = loadUi('drinkmixer.ui')
widget.btn_ckt1.clicked.connect(mixCocktail("string"))
widget.show()
sys.exit(app.exec_())
                                                                       #

This is the line you should be paying attention to, this is where the error or the cause of the error happens.

                                                                       #
widget.btn_ckt1.clicked.connect(mixCocktail("string"))
                                                                       #

It is worth to note that in this particular case the error has occurred with a PyQt button action, the goal was to send a string with the Function.

To solve the problem above, I have one solution which worked for me and works for 90 percent of people with this error.

Solution : using the lambda function in python

Since you are expected to provide a callable function, the only way to fix the error is using lambda.

So, the solution is to replace the argument with the following line bellow

                                                                       #
lambda: mixCocktail("string")
                                                                       #

As a result, the code bellow will be replaced.

                                                                       #
def mixCocktail(str):
      cocktail.show()
      cocktail.showFullScreen()
      cocktail.lbl_header.setText(str)

widget = loadUi('drinkmixer.ui')

widget.btn_ckt1.clicked.connect(mixCocktail("string"))

widget.show()
sys.exit(app.exec_())
                                                                       #

With the following code

                                                                       #
def mixCocktail(_str):
      cocktail.show()
      cocktail.showFullScreen()
      cocktail.lbl_header.setText(_str)

widget = loadUi('drinkmixer.ui')

widget.btn_ckt1.clicked.connect(lambda: mixCocktail("string"))

widget.show()
sys.exit(app.exec_())
                                                                       #

I hope the solution has been helpful, I hope you solved the error already, you can support us by donating to our Kofi account, this website is free to use but any help is very appreciated and goes a long way.

Summing-up

I hope my article was helpful in solving the error TypeError: argument 1 has unexpected type NoneType, errors are part of our journey of coding with Python, keep learning, keep coding and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/