I don't think that I like that the following is True. I understand that's the case because NewType is only examined by the static checker, and ignored at runtime. If I want it to be False, what's the best way to do that? Make a @dataclass with a single field like "value", take the performance hit, and write the forwarding functions that you want to have available?
from typing import NewType
A = NewType("A", int)
B = NewType("B", int)
print(A(1) == B(1))
What I'm doing right now is:
from dataclasses import dataclass
@dataclass
class Integer:
value: int
@dataclass
class A(Integer):
pass
@dataclass
class B(Integer):
pass
This prevents the classes from being compared, but certainly has a performance hit. I tried inheriting from int, but that way it does say A(1) == B(1) is True.
Subclass
intand override__eq__(and other methods like__add__, if appropriate).The
__eq__operator does not inherently need to returnFalseif the two objects are different types. If you want that behavior, you'll have to specify that by adding it like above.