Skip to content

mypy: Unsupported operand types for + ("int" and "str")

Python & Gotested on Python 3.14.2, uv 0.9.26, mypy

Python itself checks types only when the line runs. mypy reads the type annotations and finds the errors beforehand – in the course two deliberately planted ones in a throwaway file.

Symptom

uv run --with mypy mypy src; echo "exit=$?"
src/logsum/typo.py:3: error: Argument "since" to "summarize" has incompatible
  type "str"; expected "datetime | None"  [arg-type]
src/logsum/typo.py:4: error: Unsupported operand types for + ("int" and "str")
  [operator]
Found 2 errors in 1 file (checked 6 source files)
exit=1

uv run --with mypy starts mypy in a throwaway environment without installing it.

Diagnosis

The two lines of the checked file.

from logsum.stats import summarize

s = summarize([], since="2026-10-10T08:30")
print(s.total + "Anfragen")

Causes

[operator]int + str

s.total is a number, "Anfragen" (“requests”) a string. Python would abort with TypeError at runtime.

[arg-type]String instead of datetime

summarize expects a datetime or None for since but gets a string.

Fix

Fix the types (for example f"{s.total} requests" and pass a datetime object) or remove the file and check strictly.
rm src/logsum/typo.py
uv run --with mypy mypy --strict src
Success: no issues found in 5 source files

Don't: Silence errors with # type: ignore. The runtime errors still come.

▶
Break it yourself and find it – free
Python hands-on, Day 2: functions, dataclasses, types and mypy with the logsum tool.
Go to day 2 →

FAQ

Do I have to install mypy?

No. uv run --with mypy mypy src starts mypy in a throwaway environment.

What does --strict add?

--strict turns on extra checks such as missing annotations. In the course logsum passes it without findings.

Related error patterns

More