Skip to content

Commit c8e1d81

Browse files
improve tracebacks on failing conversions
1 parent 4cbbc73 commit c8e1d81

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ _This project uses semantic versioning_
1010
- Add error method if `@method` decorator is in wrong place
1111
- Subsumes lambda functions after replacing
1212
- Add working loopnest test
13+
- Improve tracebacks on failing conversions.
1314

1415
## 8.0.1 (2024-10-24)
1516

python/egglog/conversion.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,22 @@
1515

1616
from .egraph import Expr
1717

18-
__all__ = ["convert", "converter", "resolve_literal", "convert_to_same_type"]
18+
__all__ = ["convert", "convert_to_same_type", "converter", "resolve_literal"]
1919
# Mapping from (source type, target type) to and function which takes in the runtimes values of the source and return the target
2020
TypeName = NewType("TypeName", str)
2121
CONVERSIONS: dict[tuple[type | TypeName, TypeName], tuple[int, Callable]] = {}
2222
# Global declerations to store all convertable types so we can query if they have certain methods or not
23-
# Defer it as a thunk so we can register conversions without triggering type signature loading
24-
CONVERSIONS_DECLS: Callable[[], Declarations] = Thunk.value(Declarations())
23+
_CONVERSION_DECLS = Declarations.create()
24+
# Defer a list of declerations to be added to the global declerations, so that we can not trigger them procesing
25+
# until we need them
26+
_TO_PROCESS_DECLS: list[DeclerationsLike] = []
27+
28+
29+
def _retrieve_conversion_decls() -> Declarations:
30+
_CONVERSION_DECLS.update(*_TO_PROCESS_DECLS)
31+
_TO_PROCESS_DECLS.clear()
32+
return _CONVERSION_DECLS
33+
2534

2635
T = TypeVar("T")
2736
V = TypeVar("V", bound="Expr")
@@ -100,25 +109,20 @@ def process_tp(tp: type | RuntimeClass) -> TypeName | type:
100109
"""
101110
Process a type before converting it, to add it to the global declerations and resolve to a ref.
102111
"""
103-
global CONVERSIONS_DECLS
104112
if isinstance(tp, RuntimeClass):
105-
CONVERSIONS_DECLS = Thunk.fn(_combine_decls, CONVERSIONS_DECLS, tp)
113+
_TO_PROCESS_DECLS.append(tp)
106114
egg_tp = tp.__egg_tp__
107115
if egg_tp.args:
108116
raise TypeError(f"Cannot register a converter for a generic type, got {tp}")
109117
return TypeName(egg_tp.name)
110118
return tp
111119

112120

113-
def _combine_decls(d: Callable[[], Declarations], x: HasDeclerations) -> Declarations:
114-
return Declarations.create(d(), x)
115-
116-
117121
def min_convertable_tp(a: object, b: object, name: str) -> TypeName:
118122
"""
119123
Returns the minimum convertable type between a and b, that has a method `name`, raising a ConvertError if no such type exists.
120124
"""
121-
decls = CONVERSIONS_DECLS()
125+
decls = _retrieve_conversion_decls()
122126
a_tp = _get_tp(a)
123127
b_tp = _get_tp(b)
124128
a_converts_to = {
@@ -161,7 +165,7 @@ def with_type_args(args: tuple[JustTypeRef, ...], decls: Callable[[], Declaratio
161165

162166

163167
def resolve_literal(
164-
tp: TypeOrVarRef, arg: object, decls: Callable[[], Declarations] = CONVERSIONS_DECLS
168+
tp: TypeOrVarRef, arg: object, decls: Callable[[], Declarations] = _retrieve_conversion_decls
165169
) -> RuntimeExpr:
166170
arg_type = _get_tp(arg)
167171

python/tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ def _reset_conversions():
99
import egglog.conversion
1010

1111
old_conversions = copy.copy(egglog.conversion.CONVERSIONS)
12-
old_conversion_decls = copy.copy(egglog.conversion.CONVERSIONS_DECLS)
12+
old_conversion_decls = copy.copy(egglog.conversion._TO_PROCESS_DECLS)
1313
yield
1414
egglog.conversion.CONVERSIONS = old_conversions
15-
egglog.conversion.CONVERSIONS_DECLS = old_conversion_decls
15+
egglog.conversion._TO_PROCESS_DECLS = old_conversion_decls
1616

1717

1818
class PythonSnapshotExtension(SingleFileSnapshotExtension):

0 commit comments

Comments
 (0)