Skip to content

Commit a2c75cd

Browse files
committed
Adjust import order
1 parent 7780b8c commit a2c75cd

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

sqlmesh_dbt/error.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def wrapper(*args: t.List[t.Any], **kwargs: t.Any) -> t.Any:
1616
return func(*args, **kwargs)
1717
except Exception as ex:
1818
# these imports are deliberately deferred to avoid the penalty of importing the `sqlmesh`
19-
# package for every CLI command
19+
# package up front for every CLI command
2020
from sqlmesh.utils.errors import SQLMeshError
2121
from sqlglot.errors import SqlglotError
2222

@@ -26,13 +26,16 @@ def wrapper(*args: t.List[t.Any], **kwargs: t.Any) -> t.Any:
2626
else:
2727
raise
2828
finally:
29-
from sqlmesh import Context
30-
3129
context_or_obj = args[0]
3230
sqlmesh_context = (
3331
context_or_obj.obj if isinstance(context_or_obj, click.Context) else context_or_obj
3432
)
35-
if isinstance(sqlmesh_context, Context):
36-
sqlmesh_context.close()
33+
if sqlmesh_context is not None:
34+
# important to import this only if a context was created
35+
# otherwise something like `sqlmesh_dbt run --help` will trigger this import because it's in the finally: block
36+
from sqlmesh import Context
37+
38+
if isinstance(sqlmesh_context, Context):
39+
sqlmesh_context.close()
3740

3841
return wrapper

0 commit comments

Comments
 (0)