Skip to content

Commit f741bb5

Browse files
Fix f64Like import
1 parent cd6f4f4 commit f741bb5

File tree

8 files changed

+36
-26
lines changed

8 files changed

+36
-26
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/changelog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ _This project uses semantic versioning_
44

55
## UNRELEASED
66

7+
- fix using `f64Like` when not importing star (also properly includes removal of `Callable` special case from previous release).
8+
79
## 10.0.1 (2025-04-06)
810

911
- Fix bug on resolving types if not all imported to your module [#286](https://github.com/egraphs-good/egglog-python/pull/286)
10-
- Also stops special casing including `Callable` as a global. So if you previously included this in a `TYPE_CHECKING` block so it wasn
12+
- Also stops special casing including `Callable` as a global. So if you previously included this in a `TYPE_CHECKING` block so it wasn't
1113
available at runtime you will have to move this to a runtime import if used in a type alias.
1214

1315
## 10.0.0 (2025-03-28)

docs/reference/python-integration.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ If you want to have this work with the static type checker, you can define your
157157
the `Expr` class as the first item in the union. For example, in this case you could then define:
158158

159159
```{code-cell} python
160-
from typing import Union
161-
MathLike = Union[Math, i64Like, StringLike]
160+
MathLike = Math | i64Like | StringLike
162161
163162
@function
164163
def some_math_fn(x: MathLike) -> MathLike:

python/egglog/builtins.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from fractions import Fraction
1010
from functools import partial, reduce
1111
from types import FunctionType, MethodType
12-
from typing import TYPE_CHECKING, Generic, Protocol, TypeAlias, TypeVar, Union, cast, overload
12+
from typing import TYPE_CHECKING, Generic, Protocol, TypeAlias, TypeVar, cast, overload
1313

1414
from typing_extensions import TypeVarTuple, Unpack
1515

@@ -101,8 +101,6 @@ def join(*strings: StringLike) -> String: ...
101101

102102
converter(str, String, String)
103103

104-
BoolLike: TypeAlias = Union["Bool", bool]
105-
106104

107105
class Bool(BuiltinExpr, egg_sort="bool"):
108106
@method(preserve=True)
@@ -133,10 +131,10 @@ def __xor__(self, other: BoolLike) -> Bool: ...
133131
def implies(self, other: BoolLike) -> Bool: ...
134132

135133

136-
converter(bool, Bool, Bool)
134+
BoolLike: TypeAlias = Bool | bool
137135

138-
# The types which can be convertered into an i64
139-
i64Like: TypeAlias = Union["i64", int] # noqa: N816, PYI042
136+
137+
converter(bool, Bool, Bool)
140138

141139

142140
class i64(BuiltinExpr): # noqa: N801
@@ -248,16 +246,16 @@ def bool_le(self, other: i64Like) -> Bool: ...
248246
def bool_ge(self, other: i64Like) -> Bool: ...
249247

250248

249+
# The types which can be convertered into an i64
250+
i64Like: TypeAlias = i64 | int # noqa: N816, PYI042
251+
251252
converter(int, i64, i64)
252253

253254

254255
@function(builtin=True, egg_fn="count-matches")
255256
def count_matches(s: StringLike, pattern: StringLike) -> i64: ...
256257

257258

258-
f64Like: TypeAlias = Union["f64", float] # noqa: N816, PYI042
259-
260-
261259
class f64(BuiltinExpr): # noqa: N801
262260
@method(preserve=True)
263261
def eval(self) -> float:
@@ -337,6 +335,9 @@ def from_i64(cls, i: i64) -> f64: ...
337335
def to_string(self) -> String: ...
338336

339337

338+
f64Like: TypeAlias = f64 | float # noqa: N816, PYI042
339+
340+
340341
converter(float, f64, f64)
341342

342343

python/egglog/egraph.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -569,16 +569,11 @@ def _fn_decl(
569569
if not isinstance(fn, FunctionType):
570570
raise NotImplementedError(f"Can only generate function decls for functions not {fn} {type(fn)}")
571571

572-
hint_globals = fn.__globals__.copy()
573-
# Copy Callable into global if not present bc sometimes it gets automatically removed by ruff to type only block
574-
# https://docs.astral.sh/ruff/rules/typing-only-standard-library-import/
575-
if "Callable" not in hint_globals:
576-
hint_globals["Callable"] = Callable
577572
# Instead of passing both globals and locals, just pass the globals. Otherwise, for some reason forward references
578573
# won't be resolved correctly
579574
# We need this to be false so it returns "__forward_value__" https://github.com/python/cpython/blob/440ed18e08887b958ad50db1b823e692a747b671/Lib/typing.py#L919
580575
# https://github.com/egraphs-good/egglog-python/issues/210
581-
hint_globals.update(hint_locals)
576+
hint_globals = {**fn.__globals__, **hint_locals}
582577
hints = get_type_hints(fn, hint_globals)
583578

584579
params = list(signature(fn).parameters.values())

python/egglog/exp/program_gen.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55

66
from __future__ import annotations
77

8-
from typing import TypeAlias, Union
8+
from typing import TypeAlias
99

1010
from egglog import *
1111

12-
ProgramLike: TypeAlias = Union["Program", StringLike]
13-
1412

1513
class Program(Expr):
1614
"""
@@ -97,6 +95,9 @@ def is_identifer(self) -> Bool:
9795
"""
9896

9997

98+
ProgramLike: TypeAlias = Program | StringLike
99+
100+
100101
converter(String, Program, Program)
101102

102103

python/tests/test_high_level.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pathlib
66
from copy import copy
77
from fractions import Fraction
8-
from typing import ClassVar, TypeAlias, Union
8+
from typing import ClassVar, TypeAlias
99

1010
import pytest
1111

@@ -451,8 +451,6 @@ def __init__(self, name: StringLike) -> None: ...
451451

452452
def __add__(self, other: Int) -> Int: ...
453453

454-
NDArrayLike: TypeAlias = Union[Int, "NDArray"]
455-
456454
class NDArray(Expr):
457455
def __init__(self, name: StringLike) -> None: ...
458456

@@ -465,6 +463,8 @@ def to_int(self) -> Int: ...
465463
@classmethod
466464
def from_int(cls, other: Int) -> NDArray: ...
467465

466+
NDArrayLike: TypeAlias = NDArray | Int
467+
468468
converter(Int, NDArray, NDArray.from_int)
469469
converter(NDArray, Int, lambda a: a.to_int(), 100)
470470

python/tests/test_no_import_star.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import egglog as el
4+
from egglog import f64Like
45

56

67
def test_no_import_star():
@@ -11,7 +12,18 @@ def test_no_import_star():
1112
class Num(el.Expr):
1213
def __init__(self, value: el.i64Like) -> None: ...
1314

14-
Num(1) # gets an error "NameError: name 'i64' is not defined"
15+
Num(1)
16+
17+
18+
def test_f64_import():
19+
"""
20+
For some reason this wasn't working until we moved the union definition below the class
21+
"""
22+
23+
class Num(el.Expr):
24+
def __init__(self, value: f64Like) -> None: ...
25+
26+
Num(1.0)
1527

1628

1729
def test_no_import_star_rulesset():

0 commit comments

Comments
 (0)