Skip to content

Commit 4d9340a

Browse files
Update docs to reflect new conversion logic
1 parent 7d750b4 commit 4d9340a

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

docs/reference/python-integration.md

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,13 @@ instead of the normal mechanism which relies on `__getattr__`, you can call `egg
379379
with the name of a method. This is only needed for third party code that inspects the type object itself to see if a
380380
method is defined instead of just attempting to call it.
381381

382-
### Reflected methods
382+
### Binary Method Conversions
383383

384-
Note that reflected methods (i.e. `__radd__`) are handled as a special case. If defined, they won't create their own egglog functions.
384+
For [rich comparison methods](https://docs.python.org/3/reference/datamodel.html#object.__lt__) (like `__lt__`, `__le__`, `__eq__`, etc.) and [binary numeric methods](https://docs.python.org/3/reference/datamodel.html#object.__add__) (like `__add__`, `__sub__`, etc.), some more advanced conversion logic is needed to ensure they are converted properly. We add the `__r<name>__` methods for all expressions so that we can handle either position they are placed in.
385385

386-
Instead, whenever a reflected method is called, we will try to find the corresponding non-reflected method and call that instead.
387-
388-
Also, if a normal method fails because the arguments cannot be converted to the right types, the reflected version of the second arg will be tried.
386+
If we have two values `lhs` and `rhs`, we will try to find the minimum cost conversion for both of them, and then call the method on the converted values.
387+
If both are expression instances, we will convert at most one of them. However, if one is an expression and the other
388+
is a different Python value (like an `int`), we will consider all possible conversions of both arguments to find the minimum.
389389

390390
```{code-cell} python
391391
class Int(Expr):
@@ -423,11 +423,6 @@ converter(Int, Float, Float.from_int)
423423
assert str(-1.0 + Int.var("x")) == "Float(-1.0) + Float.from_int(Int.var(\"x\"))"
424424
```
425425

426-
For methods which allow returning `NotImplemented`, i.e. the comparison + binary math methods, we will also try upcasting both
427-
types to the type which is lowest cost to convert both to.
428-
429-
For example, if you have `Float` and `Int` wrapper types and you have write the expr `-1.0 + Int.var("x")` you might want the result to be `Float(-1.0) + Float.from_int(Int.var("x"))`:
430-
431426
### Mutating arguments
432427

433428
In order to support Python functions and methods which mutate their arguments, you can pass in the `mutate_first_arg` keyword argument to the `@function` decorator and the `mutates_self` argument to the `@method` decorator. This will cause the first argument to be mutated in place, instead of being copied.

0 commit comments

Comments
 (0)