I'm not sure this is supposed to work:
from typing import Optional
class A:
def __init__(self):
self.n: Optional[int] = 1
def f(self) -> None:
if self.n is None:
raise RuntimeError("")
self.n += 1 # no mypy error here
def f_false_positive(self) -> None:
self.check_n()
self.n += 1 # mypy error here
def destroy(self) -> None:
self.n = None
def check_n(self):
if self.n is None:
raise RuntimeError("")
The error is at line 16:
$ mypy /tmp/1.py
/tmp/1.py:16: error: Unsupported operand types for + ("None" and "int")
/tmp/1.py:16: note: Left operand is of type "Optional[int]"
Line 11 is correct instead.