TypeError: unsupported operand type(s) for +: 'int' and 'str'
Python · updated 2026-11-03
Raised when an operator gets types it can't combine. The message names the operator and both types, in order: the left operand first. 'NoneType' in the message means one side was None.
Traceback (most recent call last):
File "/app/cart.py", line 14, in total
subtotal = subtotal + item["qty"] * item["price"]
~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TypeError: unsupported operand type(s) for +: 'int' and 'str'Quick fix
Read the two types in the message. 'str' means a number arrived as text (form data, CSV, JSON or env vars): convert it with int() or Decimal() where it enters the program. 'NoneType' means a value was missing: find out why, or give it a default before the math.
- Note the operator and the two types in the message, left then right.
- Print type() of each operand just before the line to see which one is wrong.
- Convert input once at the boundary (request parsing, CSV read, settings load), not inside every calculation.
- For money, use Decimal(str_value) instead of float.
- For None, decide whether it is a bug upstream or a valid "missing" that should default to 0.
from decimal import Decimal
def parse_item(raw: dict) -> dict:
"""Convert once, where the data comes in."""
return {
"qty": int(raw["qty"]),
"price": Decimal(str(raw["price"])),
}
items = [parse_item(r) for r in rows]
subtotal = sum((i["qty"] * i["price"] for i in items), Decimal("0"))Why it happens
Numbers that arrived as strings
Form fields, query strings, CSV cells, environment variables and some JSON APIs deliver numbers as text. "3" + 1 fails; the conversion was skipped somewhere upstream.
A None from a missing value
dict.get() without a default, a function that returns nothing on one branch, or an empty database column gives None, and None + 1 raises this error with 'NoneType' in the message.
Concatenating text with a number
Building a message with "Total: " + total fails when total is a number. Use an f-string instead: f"Total: {total}".
Mixing incompatible numeric types
Decimal + float raises this error, as does a datetime + an int. Keep money in Decimal throughout and use timedelta for date arithmetic.
Other fixes
- Add type hints and run mypy or pyright: most of these errors show up as type mismatches before the code runs.
- Validate request bodies with pydantic so numeric fields arrive as numbers.
- sum() on a list of strings fails the same way: convert the items, or pass start='' to join text (better: ''.join()).
Same error, other wording
- TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
- TypeError: unsupported operand type(s) for -: 'str' and 'int'
- TypeError: can only concatenate str (not "int") to str
Let your coding agent catch the next one
Runheed records this error in production with the stack trace, the breadcrumbs before it and the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:
> Find the 'unsupported operand type' errors in production, show me which values had the wrong type and where they entered the program, and convert them at that boundary instead of at the calculation.
Related errors
- ValueError: invalid literal for int() with base 10: ''
- TypeError: 'NoneType' object is not subscriptable
- KeyError: 'email'
References: Python docs: built-in exceptions (TypeError), Python docs: decimal module