GeezerVison is showing github.com — Support upcasting for binary methods based on parent classes · mjdhasan/egglog-python@1efba4b
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: python/egglog/conversion.py +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -186,7 +186,11 @@ def _all_conversions_from(tp: JustTypeRef | type) -> list[tuple[int, JustTypeRef |
186 | 186 |
|
187 | 187 | Returns a list of tuples of (cost, target type, conversion function). |
188 | 188 | """ |
189 | | - return [(cost, target, fn) for (source, target), (cost, fn) in CONVERSIONS.items() if source == tp] |
| 189 | + return [ |
| 190 | + (cost, target, fn) |
| 191 | + for (source, target), (cost, fn) in CONVERSIONS.items() |
| 192 | + if (issubclass(tp, source) if isinstance(tp, type) and isinstance(source, type) else source == tp) |
| 193 | + ] |
190 | 194 |
|
191 | 195 |
|
192 | 196 | def identity(x: Any) -> Any: |
|
Collapse file
Copy file name to clipboardExpand all lines: python/tests/test_high_level.py +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1538,3 +1538,17 @@ def mutates(self) -> None: |
1538 | 1538 | x = MutatePreserved() |
1539 | 1539 | x.mutates() |
1540 | 1540 | assert x == MutatePreserved().incr() |
| 1541 | + |
| 1542 | + |
| 1543 | +def test_binary_conversion_lookup_parent_class(): |
| 1544 | + class X(Expr): |
| 1545 | + def __init__(self, value: object) -> None: ... |
| 1546 | + |
| 1547 | + def __add__(self, other: XLike) -> X: ... |
| 1548 | + def __radd__(self, other: XLike) -> X: ... |
| 1549 | + |
| 1550 | + XLike: TypeAlias = X | object |
| 1551 | + converter(object, X, lambda x: X(PyObject(x))) |
| 1552 | + |
| 1553 | + assert X(1) + 2 == X(1) + X(2) |
| 1554 | + assert 2 + X(1) == X(2) + X(1) |