Are you stuck with this “typeerror: unsupported operand type s for nonetype and nonetype” error message?
Well, you’re lucky enough, because in this article we are going to explore this type error.
We will explain thoroughly what this error means and why it occurs.
Apart from that, we’ll hand you the effective solutions that will help you resolve this “unsupported operand type s for nonetype and nonetype” type error.
What is “typeerror unsupported operand type s for nonetype and nonetype”?
The “typeerror unsupported operand type s for nonetype and nonetype” is an error message that occurs when you are trying to perform an operation between two NoneType objects.
Or you are trying to perform an operation between two incompatible data types.
In addition to that, this error usually occurs when operations are performed on incompatible types or NoneType objects.
For example:
sample1 = None
sample2 = None
result = sample1 + sample2
print(result)
As a result, it will throw an error message:
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'It will result in a typeerror, mainly because NoneType is not a numerical value and cannot be added together.
Why does this error occur?
The following are several reasons why this error appears in your code:
→ If the variable is not assigned a value before being used in an operation.
→ If the function returns None instead of a numerical value.
→ If the value is explicitly set to None.
What is “NoneType” in Python?
“NoneType” is the type of the None object which represents a lack of value or a null value. It is an object that represents no value.
So, since it represents no value, definitely you can’t perform operations like addition or subtraction between two “NoneType” objects.
How to fix “typeerror unsupported operand type s for nonetype and nonetype”?
The following are the different solutions you may use to fix this error:
Solution 1: Use if statement to check nonetype objects before performing the operation
As you have seen in this solution, we check whether both sample1 and sample2 are NoneType objects before performing the operation.
If either x or y is NoneType, the code will skip the operation and set the result to None.
sample1 = None
sample2 = None
if sample1 is not None and sample2 is not None:
result = sample1 + sample2
else:
result = None
print(result)
Output:
NoneSolution 2: Assign value to the variable
You need to assign a value to the variable before using it in an operation.
When a variable is not assigned a value before being used in an operation, it will have a default value of None.
Ensure that you have already assigned a value to the variable before using it in an operation.
sample1 = 100
sample2 = 200
result = sample1 + sample2
print(result)Output:
300Solution 3: Use a default value
You can also use a default value for the variable if it is “None.”
In this example code, we use “0” as the default value.
sample1 = None
sample2 = 100
result = (sample1 if sample1 is not None else 0) + (sample2 if sample2 is not None else 0)
print(result)Output:
100Solution 4: Convert the variable
Here we convert the variable to a different data type before performing the operation.
As you can see in the example code below, we convert the variables to integers using the int() function.
sample1 = 1000
sample2 = None
result = int(sample1 or 0) + int(sample2 or 0)
print(result)Output:
1000Solution 5: Use isinstance() function
Alternatively, you can also use the isinstance() function to check if x and y are NoneType objects.
If they are, they are assigned a default value of 0 before performing the operation.
This approach is more explicit than checking for None using the if “sample1” is None syntax, which can lead to unexpected behavior in certain cases.
sample1 = None
sample2 = 10
if isinstance(sample1, type(None)):
sample1 = 0
if isinstance(sample2, type(None)):
sample2 = 0
result = sample1 + sample2
print(result)Output:
10Conclusion
In conclusion, the “typeerror unsupported operand type s for nonetype and nonetype” is an error message in Python.
That occurs when you are trying to perform an operation between two NoneType objects.
You are lucky enough because this article provides effective solutions. So that you can fix the “unsupported operand type s for str and float” error message.
We are hoping that this article provided you with sufficient solutions to get rid of the error.
You could also check out other “typeerror” articles that may help you in the future if you encounter them.
- Typeerror unsupported operand type s for str and float
- Typeerror: ‘dict_keys’ object is not subscriptable
- Typeerror: object of type ndarray is not json serializable
Thank you very much for reading to the end of this article.
Why NoneType TypeErrors happen so often
Python returns None from many functions when there is nothing meaningful to return: from methods that mutate in place (list.append, dict.update), from lookups that miss (dict.get with no default), and from any function that ends without an explicit return. When you use that None as if it were the expected value, you get a TypeError.
Common triggers of NoneType TypeError
- Method chaining on mutating operations. sorted(list) returns a new sorted list, but list.sort() sorts in place and returns None. Never write
x = my_list.sort(). - Print statements accidentally assigned.
x = print(value)assigns None, not value. - Missing return in a function. A function that only has if branches without returns falls through to an implicit return None.
- Dictionary lookups with missing key.
value = my_dict.get(key)returns None if key is missing. Provide a default:my_dict.get(key, default_value). - Regex match returning None. re.search returns None if no match. Guard with an if before .group().
Working diagnostic pattern
# BAD — silent None propagation
def get_config(env):
if env == "prod":
return {"db": "prod-cluster"}
# No else — implicit return None
config = get_config("staging")
print(config["db"]) # TypeError: 'NoneType' object is not subscriptable
# GOOD — explicit branch handling
def get_config(env):
if env == "prod":
return {"db": "prod-cluster"}
if env == "staging":
return {"db": "staging-cluster"}
raise ValueError(f"Unknown env: {env}")
Best practices
- Use type hints (Python 3.6+). Type-checkers like mypy or Pyright catch these before runtime.
- Prefer Optional[T] when a function may return None. Callers must handle the None case.
- Fail fast at boundaries. Raise an explicit exception in helper functions rather than returning None silently.
Official documentation
Frequently Asked Questions
What is Python TypeError and what causes it?
TypeError is raised when an operation is applied to an object of the wrong type. Common patterns: calling a non-callable object, adding incompatible types (str + int), passing the wrong number of arguments, or accessing attributes on a NoneType. Each TypeError message names the operation and expected vs actual types, the fix is almost always to convert types explicitly (int(), str()) or fix the wrong variable assignment.
How do I quickly debug a Python TypeError?
Three steps: (1) Read the full error message, it names the exact operation and types involved. (2) Print the type of every variable in that line: print(type(var1), type(var2)). (3) Check what the function expected vs what you passed. Most TypeError fixes are 1-line type casts or fixing a variable that became None unexpectedly.
Should I catch TypeError or let it propagate?
For internal code, let TypeError propagate, it’s almost always a real bug (wrong type passed). For boundary code (parsing user input, third-party API responses), catch TypeError + ValueError together: try: parsed = int(value) except (TypeError, ValueError): parsed = 0. Catching internal TypeErrors hides bugs.
How do I prevent TypeError in production?
Three patterns: (1) Use type hints (def add(a: int, b: int) -> int) and check with mypy / pyright in CI. (2) Validate inputs at boundaries (Pydantic for FastAPI, DRF serializers for Django). (3) Default values that match expected types (return 0 not None for numeric functions). Static typing catches 80% of TypeErrors before runtime.
Where can I find more TypeError fixes?
Browse the TypeError reference hub for 220+ specific TypeError fixes. For broader Python debugging, see the Python Tutorial hub. For related error types, see ValueError and AttributeError guides.
