Skip to content

Commit 34ad12d

Browse files
authored
chore: refactor out targets as early as possible (#4126)
1 parent cc0d42b commit 34ad12d

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

sqlmesh/core/context.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import abc
3737
import collections
38+
from itertools import chain
3839
import logging
3940
import sys
4041
import time
@@ -1076,15 +1077,20 @@ def format(
10761077
**kwargs: t.Any,
10771078
) -> bool:
10781079
"""Format all SQL models and audits."""
1080+
filtered_targets = [
1081+
target
1082+
for target in chain(self._models.values(), self._audits.values())
1083+
if target._path is not None
1084+
and target._path.suffix == ".sql"
1085+
and (not paths or any(target._path.samefile(p) for p in paths))
1086+
]
10791087
unformatted_file_paths = []
1080-
format_targets = {**self._models, **self._audits}
10811088

1082-
for target in format_targets.values():
1083-
if target._path is None or target._path.suffix != ".sql":
1084-
continue
1085-
if paths and not any(target._path.samefile(p) for p in paths):
1089+
for target in filtered_targets:
1090+
if (
1091+
target._path is None
1092+
): # introduced to satisfy type checker as still want to pull filter out as many targets as possible before loop
10861093
continue
1087-
10881094
with open(target._path, "r+", encoding="utf-8") as file:
10891095
before = file.read()
10901096
expressions = parse(before, default_dialect=self.config_for_node(target).dialect)
@@ -1125,7 +1131,6 @@ def format(
11251131
if unformatted_file_paths:
11261132
for path in unformatted_file_paths:
11271133
self.console.log_status_update(f"{path} needs reformatting.")
1128-
11291134
self.console.log_status_update(
11301135
f"\n{len(unformatted_file_paths)} file(s) need reformatting."
11311136
)

0 commit comments

Comments
 (0)