-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[red-knot] Compare expression inference - Instance (+ Rich Comparison, Membership Test) #13872
Comments
I realized that this issue should also include the "membership test" docs The "membership test" involves logic for checking |
The algorithm you'll want to use for this will be similar in many ways to the one we implemented for binary arithmetic here (but there are some important differences, including the one you already noted for membership testing): ruff/crates/red_knot_python_semantic/src/types/infer.rs Lines 2704 to 2736 in 7dbd8f0
See also:
|
Very happy to give additional clarifications if you need any further pointers! |
Thank you!!! Your input was very helpful during the implementation. BTW, I’m a bit confused about something, and I’d like to discuss it. The part I’m struggling with is: how type mismatches between a dunder function signature and argument types are handled. From my understanding, when there’s a type mismatch, the dunder method should return class A:
def __lt__(self, other: int) -> bool:
if not isinstance(other, int):
return NotImplemented
else:
return True
A() < 3 # No issues
A() < "qwr" # runtime error: not supported operator
# also in builtins
42 < 3.14 # No issues
(42).__lt__(3.14) # NotImplemented
(3.14).__gt__(42) # bool With this assumption, According to the Python docs, when from __future__ import annotations
from typing import reveal_type
class A:
def __eq__(self, other: int) -> A:
...
class B:
def __eq__(self, other: int) -> B:
...
reveal_type(A() == B()) # revealed: bool? or Unknown? I think it should reveal |
The item mentioned in #13618, "Bug with defined method but other type that doesn't match the signature," might complicate the upcoming PR too much. For now, I’ll focus solely on the implementation of Rich Comparison.
The text was updated successfully, but these errors were encountered: