I am making a simple vector class, and I'm struggling to understand how mypy is working on my __add__
and __sub__
methods (in particular the difference in the mypy output in Code 1 vs Code 3 below).
Code 1:
from typing import NamedTuple
class Vector(NamedTuple):
x: float
y: float
def __add__(self, other: Vector) -> Vector:
return Vector(self.x + other.x, self.y + other.y)
Code 2:
from typing import NamedTuple
class Vector(NamedTuple):
x: float
y: float
def __add__(self, other: object) -> Vector:
if not isinstance(other, Vector):
return NotImplemented
return Vector(self.x + other.x, self.y + other.y)
Code 3:
from typing import NamedTuple
class Vector(NamedTuple):
x: float
y: float
def __sub__(self, other: Vector) -> Vector:
return Vector(self.x - other.x, self.y - other.y)
With Code 1, I get the following error when running mypy:
error: Signature of "__add__" incompatible with supertype "tuple"
.
With Code 2 and Code 3 I get no errors.
Why do I get an error with Code 1 and not Code 2?
Second (and this confuses me more), why do I not get the same error from Code 3 as I do with Code 1?
Many thanks.
EDIT
I guess the answer is because the superclass NamedTuple
allows for addition, but specifies that the second argument is type object
which Code 2 allows for, and Code 1 doesn't.
And that mypy is find with Code 3 because NamedTuple
doesn't implement __sub__
.