-
Hello! Love using Pyright -- it has changed the way I write python. I noticed that my Pyright doesn't warn/error when making comparisons between two different types. I have tested this in a clean repo with strict enabled. # pyright: strict
import typing
A = typing.NewType("A", str)
B = typing.NewType("B", int)
x = A("123")
y = B(234)
x == y # no warn/error
class C:
...
class D:
...
C() == D() # no warn/error Is there an existing config to enable warning/error when making comparison between types? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Most classes support equality checks between arbitrary types. For example, the following code runs without an exception: x = 1
y = "hi"
print(x == y) # False
print(y == None) # False This is not only legal at runtime, it's also specifically allowed by the class object:
...
def __eq__(self, o: object) -> bool: ...
def __ne__(self, o: object) -> bool: ... You're free to declare class A:
# Allow comparison only with other "A" instances.
def __eq__(self, o: "A") -> bool: ...
def __ne__(self, o: "A") -> bool: ... |
Beta Was this translation helpful? Give feedback.
Most classes support equality checks between arbitrary types. For example, the following code runs without an exception:
This is not only legal at runtime, it's also specifically allowed by the
__eq__
and__ne__
methods inherited fromobject
by most classes.You're free to declare
__eq__
and__ne__
methods on your custom class that accept narrower types thanobject
. For example: