Skip to content

Commit 7caa5fa

Browse files
committed
chore: make path optional
- more correctly represent path being optional
1 parent d734869 commit 7caa5fa

File tree

9 files changed

+27
-19
lines changed

9 files changed

+27
-19
lines changed

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
):
177177
self.dialect = dialect
@@ -1364,7 +1364,7 @@ def normalize_macro_name(name: str) -> str:
13641364
def call_macro(
13651365
func: t.Callable,
13661366
dialect: DialectType,
1367-
path: Path,
1367+
path: t.Optional[Path],
13681368
provided_args: t.Tuple[t.Any, ...],
13691369
provided_kwargs: t.Dict[str, t.Any],
13701370
**optional_kwargs: t.Any,
@@ -1411,7 +1411,7 @@ def _coerce(
14111411
expr: t.Any,
14121412
typ: t.Any,
14131413
dialect: DialectType,
1414-
path: Path,
1414+
path: t.Optional[Path] = None,
14151415
strict: bool = False,
14161416
) -> t.Any:
14171417
"""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
@@ -1635,6 +1635,8 @@ def is_seed(self) -> bool:
16351635
def seed_path(self) -> Path:
16361636
seed_path = Path(self.kind.path)
16371637
if not seed_path.is_absolute():
1638+
if self._path is None:
1639+
raise SQLMeshError(f"Seed model '{self.name}' has no path")
16381640
return self._path.parent / seed_path
16391641
return seed_path
16401642

@@ -2000,7 +2002,7 @@ def load_sql_based_model(
20002002
expressions: t.List[exp.Expression],
20012003
*,
20022004
defaults: t.Optional[t.Dict[str, t.Any]] = None,
2003-
path: Path = Path(),
2005+
path: t.Optional[Path] = None,
20042006
module_path: Path = Path(),
20052007
time_column_format: str = c.DEFAULT_TIME_COLUMN_FORMAT,
20062008
macros: t.Optional[MacroRegistry] = None,
@@ -2143,6 +2145,8 @@ def load_sql_based_model(
21432145
# The name of the model will be inferred from its path relative to `models/`, if it's not explicitly specified
21442146
name = meta_fields.pop("name", "")
21452147
if not name and infer_names:
2148+
if path is None:
2149+
raise ValueError("Model must have a name", path)
21462150
name = get_model_name(path)
21472151

21482152
if not name:
@@ -2546,7 +2550,7 @@ def _create_model(
25462550

25472551
def _split_sql_model_statements(
25482552
expressions: t.List[exp.Expression],
2549-
path: Path,
2553+
path: t.Optional[Path],
25502554
dialect: t.Optional[str] = None,
25512555
) -> t.Tuple[
25522556
t.Optional[exp.Expression],
@@ -2680,7 +2684,7 @@ def _refs_to_sql(values: t.Any) -> exp.Expression:
26802684
def render_meta_fields(
26812685
fields: t.Dict[str, t.Any],
26822686
module_path: Path,
2683-
path: Path,
2687+
path: t.Optional[Path],
26842688
jinja_macros: t.Optional[JinjaMacroRegistry],
26852689
macros: t.Optional[MacroRegistry],
26862690
dialect: DialectType,
@@ -2764,7 +2768,7 @@ def render_field_value(value: t.Any) -> t.Any:
27642768
def render_model_defaults(
27652769
defaults: t.Dict[str, t.Any],
27662770
module_path: Path,
2767-
path: Path,
2771+
path: t.Optional[Path],
27682772
jinja_macros: t.Optional[JinjaMacroRegistry],
27692773
macros: t.Optional[MacroRegistry],
27702774
dialect: DialectType,
@@ -2814,7 +2818,7 @@ def parse_defaults_properties(
28142818
def render_expression(
28152819
expression: exp.Expression,
28162820
module_path: Path,
2817-
path: Path,
2821+
path: t.Optional[Path],
28182822
jinja_macros: t.Optional[JinjaMacroRegistry] = None,
28192823
macros: t.Optional[MacroRegistry] = None,
28202824
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ def get_model_definitions_for_a_path(
132132
if len(tables) == 0:
133133
return []
134134

135+
if file_path is None:
136+
return []
135137
with open(file_path, "r", encoding="utf-8") as file:
136138
read_file = file.readlines()
137139

@@ -161,7 +163,7 @@ def get_model_definitions_for_a_path(
161163
continue
162164
referenced_model_path = referenced_model._path
163165
# Check whether the path exists
164-
if not referenced_model_path.is_file():
166+
if not referenced_model_path or not referenced_model_path.is_file():
165167
continue
166168
referenced_model_uri = f"file://{referenced_model_path}"
167169

sqlmesh/magics.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,9 @@ def model(self, context: Context, line: str, sql: t.Optional[str] = None) -> Non
229229
if loaded.name == args.model:
230230
model = loaded
231231
else:
232-
with open(model._path, "r", encoding="utf-8") as file:
233-
expressions = parse(file.read(), default_dialect=config.dialect)
232+
if model._path:
233+
with open(model._path, "r", encoding="utf-8") as file:
234+
expressions = parse(file.read(), default_dialect=config.dialect)
234235

235236
formatted = format_model_expressions(
236237
expressions,
@@ -249,8 +250,9 @@ def model(self, context: Context, line: str, sql: t.Optional[str] = None) -> Non
249250
replace=True,
250251
)
251252

252-
with open(model._path, "w", encoding="utf-8") as file:
253-
file.write(formatted)
253+
if model._path:
254+
with open(model._path, "w", encoding="utf-8") as file:
255+
file.write(formatted)
254256

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

web/server/api/endpoints/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ def serialize_model(context: Context, model: Model, render_query: bool = False)
124124
return models.Model(
125125
name=model.name,
126126
fqn=model.fqn,
127-
path=str(model._path.absolute().relative_to(context.path)),
128-
full_path=str(model._path.absolute()),
127+
path=str(model._path.absolute().relative_to(context.path)) if model._path else None,
128+
full_path=str(model._path.absolute()) if model._path else None,
129129
dialect=dialect,
130130
columns=columns,
131131
details=details,

web/server/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def _get_loaded_context(path: str | Path, config: str, gateway: str) -> Context:
8080

8181
@lru_cache()
8282
def _get_path_to_model_mapping(context: Context) -> dict[Path, Model]:
83-
return {model._path: model for model in context._models.values()}
83+
return {model._path: model for model in context._models.values() if model._path}
8484

8585

8686
def get_path_to_model_mapping(

0 commit comments

Comments
 (0)