Missing reportUnnecessaryComparison
when comparing objects of different types
#9349
Answered
by
erictraut
tkukushkin
asked this question in
Q&A
-
Hello! I've noticed, that pyright doesn't report class Foo:
pass
def test(x: list[Foo], y: Foo) -> None:
if x is y:
pass I understand why this might not work with |
Beta Was this translation helpful? Give feedback.
Answered by
erictraut
Nov 6, 2024
Replies: 1 comment 5 replies
-
What you're suggesting isn't safe. It's possible to create a class that subclasses from both class Bar(Foo, list):
pass
b = Bar()
test(b, b) The |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are some heuristics involved in pyright's check here. It assumes that if the types are both built-in types like
int
andlist
, then it's not possible to create a subclass of the two types. Indeed, if you try to create a class that subclasses from bothint
andlist
, you will see that the runtime doesn't allow this. Compare this to my example above where it is possible to create a subclass of bothFoo
andlist
.All else equal, when we need to choose between a potential false positive and a potential false negative, we tend to opt for the false negative. That informs the heuristics in this check.
In any case, pyright is working as intended here.