Skip to content

Commit 389b381

Browse files
authored
Fix: we can't inspect built-in routines to get their file (#2720)
1 parent bf1dc09 commit 389b381

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

sqlmesh/utils/metaprogramming.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,15 +347,18 @@ def serialize_env(env: t.Dict[str, t.Any], path: Path) -> t.Dict[str, Executable
347347
if callable(v):
348348
name = v.__name__
349349
name = k if name == "<lambda>" else name
350-
file_path = Path(inspect.getfile(v))
350+
351+
# We can't call getfile on built-in callables
352+
# https://docs.python.org/3/library/inspect.html#inspect.getfile
353+
file_path = Path(inspect.getfile(v)) if not inspect.isbuiltin(v) else None
351354

352355
if _is_relative_to(file_path, path):
353356
serialized[k] = Executable(
354357
name=name,
355358
payload=normalize_source(v),
356359
kind=ExecutableKind.DEFINITION,
357360
# Do `as_posix` to serialize windows path back to POSIX
358-
path=file_path.relative_to(path.absolute()).as_posix(),
361+
path=t.cast(Path, file_path).relative_to(path.absolute()).as_posix(),
359362
alias=k if name != k else None,
360363
)
361364
else:

tests/core/test_model.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,11 +1517,16 @@ def test_parse(assert_exp_eq):
15171517

15181518

15191519
def test_python_model(assert_exp_eq) -> None:
1520+
from functools import reduce
1521+
15201522
@model(name="my_model", kind="full", columns={'"COL"': "int"})
15211523
def my_model(context, **kwargs):
15221524
context.table("foo")
15231525
context.table(model_name=CONST + ".baz")
15241526

1527+
# This checks that built-in functions are serialized properly
1528+
a = reduce(lambda x, y: x + y, [1, 2, 3, 4]) # noqa: F841
1529+
15251530
m = model.get_registry()["my_model"].model(
15261531
module_path=Path("."),
15271532
path=Path("."),

0 commit comments

Comments
 (0)