Skip to content

Commit ac68e4c

Browse files
committed
chore: make path optional
[ci skip] - more correctly represent path being optional
1 parent d4dfe0f commit ac68e4c

File tree

11 files changed

+50
-24
lines changed

11 files changed

+50
-24
lines changed

sqlmesh/core/context.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -929,10 +929,15 @@ def config_for_path(self, path: Path) -> t.Tuple[Config, Path]:
929929

930930
def config_for_node(self, node: str | Model | Audit) -> Config:
931931
if isinstance(node, str):
932-
return self.config_for_path(self.get_snapshot(node, raise_if_missing=True).node._path)[
933-
0
934-
] # type: ignore
935-
return self.config_for_path(node._path)[0] # type: ignore
932+
path = self.get_snapshot(node, raise_if_missing=True).node._path
933+
if path is None:
934+
raise SQLMeshError(f"Cannot find config for '{node}'")
935+
config = self.config_for_path(path)
936+
return config[0]
937+
path = node._path
938+
if path is None:
939+
raise SQLMeshError(f"Node '{node}' does not have a path associated with it.")
940+
return self.config_for_path(path)[0] # type: ignore
936941

937942
@property
938943
def models(self) -> MappingProxyType[str, Model]:

sqlmesh/core/macros.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def __init__(
171171
resolve_tables: t.Optional[t.Callable[[exp.Expression], exp.Expression]] = None,
172172
snapshots: t.Optional[t.Dict[str, Snapshot]] = None,
173173
default_catalog: t.Optional[str] = None,
174-
path: Path = Path(),
174+
path: t.Optional[Path] = None,
175175
environment_naming_info: t.Optional[EnvironmentNamingInfo] = None,
176176
model_fqn: t.Optional[str] = None,
177177
):
@@ -1384,7 +1384,7 @@ def normalize_macro_name(name: str) -> str:
13841384
def call_macro(
13851385
func: t.Callable,
13861386
dialect: DialectType,
1387-
path: Path,
1387+
path: t.Optional[Path],
13881388
provided_args: t.Tuple[t.Any, ...],
13891389
provided_kwargs: t.Dict[str, t.Any],
13901390
**optional_kwargs: t.Any,
@@ -1431,7 +1431,7 @@ def _coerce(
14311431
expr: t.Any,
14321432
typ: t.Any,
14331433
dialect: DialectType,
1434-
path: Path,
1434+
path: t.Optional[Path] = None,
14351435
strict: bool = False,
14361436
) -> t.Any:
14371437
"""Coerces the given expression to the specified type on a best-effort basis."""

sqlmesh/core/model/definition.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,8 @@ def is_seed(self) -> bool:
16371637
def seed_path(self) -> Path:
16381638
seed_path = Path(self.kind.path)
16391639
if not seed_path.is_absolute():
1640+
if self._path is None:
1641+
raise SQLMeshError(f"Seed model '{self.name}' has no path")
16401642
return self._path.parent / seed_path
16411643
return seed_path
16421644

@@ -2002,7 +2004,7 @@ def load_sql_based_model(
20022004
expressions: t.List[exp.Expression],
20032005
*,
20042006
defaults: t.Optional[t.Dict[str, t.Any]] = None,
2005-
path: Path = Path(),
2007+
path: t.Optional[Path] = None,
20062008
module_path: Path = Path(),
20072009
time_column_format: str = c.DEFAULT_TIME_COLUMN_FORMAT,
20082010
macros: t.Optional[MacroRegistry] = None,
@@ -2152,6 +2154,8 @@ def load_sql_based_model(
21522154
# The name of the model will be inferred from its path relative to `models/`, if it's not explicitly specified
21532155
name = meta_fields.pop("name", "")
21542156
if not name and infer_names:
2157+
if path is None:
2158+
raise ValueError("Model must have a name", path)
21552159
name = get_model_name(path)
21562160

21572161
if not name:
@@ -2549,7 +2553,7 @@ def _create_model(
25492553

25502554
def _split_sql_model_statements(
25512555
expressions: t.List[exp.Expression],
2552-
path: Path,
2556+
path: t.Optional[Path],
25532557
dialect: t.Optional[str] = None,
25542558
) -> t.Tuple[
25552559
t.Optional[exp.Expression],
@@ -2670,7 +2674,7 @@ def _refs_to_sql(values: t.Any) -> exp.Expression:
26702674
def render_meta_fields(
26712675
fields: t.Dict[str, t.Any],
26722676
module_path: Path,
2673-
path: Path,
2677+
path: t.Optional[Path],
26742678
jinja_macros: t.Optional[JinjaMacroRegistry],
26752679
macros: t.Optional[MacroRegistry],
26762680
dialect: DialectType,
@@ -2754,7 +2758,7 @@ def render_field_value(value: t.Any) -> t.Any:
27542758
def render_model_defaults(
27552759
defaults: t.Dict[str, t.Any],
27562760
module_path: Path,
2757-
path: Path,
2761+
path: t.Optional[Path],
27582762
jinja_macros: t.Optional[JinjaMacroRegistry],
27592763
macros: t.Optional[MacroRegistry],
27602764
dialect: DialectType,
@@ -2804,7 +2808,7 @@ def parse_defaults_properties(
28042808
def render_expression(
28052809
expression: exp.Expression,
28062810
module_path: Path,
2807-
path: Path,
2811+
path: t.Optional[Path],
28082812
jinja_macros: t.Optional[JinjaMacroRegistry] = None,
28092813
macros: t.Optional[MacroRegistry] = None,
28102814
dialect: DialectType = None,

sqlmesh/core/node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class _Node(PydanticModel):
196196
interval_unit_: t.Optional[IntervalUnit] = Field(alias="interval_unit", default=None)
197197
tags: t.List[str] = []
198198
stamp: t.Optional[str] = None
199-
_path: Path = Path()
199+
_path: t.Optional[Path] = Path()
200200
_data_hash: t.Optional[str] = None
201201
_metadata_hash: t.Optional[str] = None
202202

sqlmesh/core/renderer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __init__(
4343
expression: exp.Expression,
4444
dialect: DialectType,
4545
macro_definitions: t.List[d.MacroDef],
46-
path: Path = Path(),
46+
path: t.Optional[Path] = None,
4747
jinja_macro_registry: t.Optional[JinjaMacroRegistry] = None,
4848
python_env: t.Optional[t.Dict[str, Executable]] = None,
4949
only_execution_time: bool = False,

sqlmesh/core/snapshot/definition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2168,7 +2168,7 @@ def _check_ready_intervals(
21682168
context: ExecutionContext,
21692169
python_env: t.Dict[str, Executable],
21702170
dialect: DialectType = None,
2171-
path: Path = Path(),
2171+
path: t.Optional[Path] = None,
21722172
kwargs: t.Optional[t.Dict] = None,
21732173
) -> Intervals:
21742174
checked_intervals: Intervals = []

sqlmesh/lsp/reference.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ def get_model_definitions_for_a_path(
139139
else:
140140
return []
141141

142+
if file_path is None:
143+
return []
144+
142145
# Find all possible references
143146
references = []
144147

@@ -205,8 +208,10 @@ def get_model_definitions_for_a_path(
205208
if referenced_model is None:
206209
continue
207210
referenced_model_path = referenced_model._path
211+
if referenced_model_path is None:
212+
continue
208213
# Check whether the path exists
209-
if not referenced_model_path.is_file():
214+
if referenced_model_path is None or not referenced_model_path.is_file():
210215
continue
211216
referenced_model_uri = URI.from_path(referenced_model_path)
212217

@@ -348,6 +353,9 @@ def get_macro_definitions_for_a_path(
348353
else:
349354
return []
350355

356+
if file_path is None:
357+
return []
358+
351359
references = []
352360
config_for_model, config_path = lsp_context.context.config_for_path(
353361
file_path,

sqlmesh/magics.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,9 @@ def model(self, context: Context, line: str, sql: t.Optional[str] = None) -> Non
235235
if loaded.name == args.model:
236236
model = loaded
237237
else:
238-
with open(model._path, "r", encoding="utf-8") as file:
239-
expressions = parse(file.read(), default_dialect=config.dialect)
238+
if model._path:
239+
with open(model._path, "r", encoding="utf-8") as file:
240+
expressions = parse(file.read(), default_dialect=config.dialect)
240241

241242
formatted = format_model_expressions(
242243
expressions,
@@ -255,8 +256,9 @@ def model(self, context: Context, line: str, sql: t.Optional[str] = None) -> Non
255256
replace=True,
256257
)
257258

258-
with open(model._path, "w", encoding="utf-8") as file:
259-
file.write(formatted)
259+
if model._path:
260+
with open(model._path, "w", encoding="utf-8") as file:
261+
file.write(formatted)
260262

261263
if sql:
262264
context.console.log_success(f"Model `{args.model}` updated")

tests/integrations/github/cicd/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def _make_function(username: str, state: str, **kwargs) -> PullRequestReview:
4949
github_client.requester,
5050
{},
5151
{
52-
# Name is whatever they provide in their GitHub profile or login as fallback. Always use login.
52+
# Name is whatever they provide in their GitHub profile or login as a fallback. Always use login.
5353
"user": AttributeDict(name="Unrelated", login=username),
5454
"state": state,
5555
**kwargs,

web/server/api/endpoints/models.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,18 @@ def serialize_model(context: Context, model: Model, render_query: bool = False)
121121
)
122122
sql = query.sql(pretty=True, dialect=model.dialect)
123123

124+
path = model._path
125+
if not path:
126+
raise HTTPException(
127+
status_code=HTTP_404_NOT_FOUND,
128+
detail=f"Model {model.name} does not have a file path.",
129+
)
130+
124131
return models.Model(
125132
name=model.name,
126133
fqn=model.fqn,
127-
path=str(model._path.absolute().relative_to(context.path).as_posix()),
128-
full_path=str(model._path.absolute().as_posix()),
134+
path=str(path.absolute().relative_to(context.path).as_posix()),
135+
full_path=str(path.absolute().as_posix()),
129136
dialect=dialect,
130137
columns=columns,
131138
details=details,

0 commit comments

Comments
 (0)