GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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
Filter options
  • python
    • egglog
      • conversion.py
    • tests
      • test_high_level.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎python/egglog/conversion.py‎

Copy file name to clipboardExpand all lines: python/egglog/conversion.py
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ def _all_conversions_from(tp: JustTypeRef | type) -> list[tuple[int, JustTypeRef
186186
187187
Returns a list of tuples of (cost, target type, conversion function).
188188
"""
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+
]
190194

191195

192196
def identity(x: Any) -> Any:
Collapse file

‎python/tests/test_high_level.py‎

Copy file name to clipboardExpand all lines: python/tests/test_high_level.py
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,3 +1538,17 @@ def mutates(self) -> None:
15381538
x = MutatePreserved()
15391539
x.mutates()
15401540
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)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.