Skip to content

Commit 4b06000

Browse files
committed
chore: make path optional
[ci skip]
1 parent ca73536 commit 4b06000

File tree

10 files changed

+27
-19
lines changed

10 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
@@ -1303,7 +1303,7 @@ def normalize_macro_name(name: str) -> str:
13031303
def call_macro(
13041304
func: t.Callable,
13051305
dialect: DialectType,
1306-
path: Path,
1306+
path: t.Optional[Path],
13071307
provided_args: t.Tuple[t.Any, ...],
13081308
provided_kwargs: t.Dict[str, t.Any],
13091309
**optional_kwargs: t.Any,
@@ -1350,7 +1350,7 @@ def _coerce(
13501350
expr: t.Any,
13511351
typ: t.Any,
13521352
dialect: DialectType,
1353-
path: Path,
1353+
path: t.Optional[Path] = None,
13541354
strict: bool = False,
13551355
) -> t.Any:
13561356
"""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
@@ -1626,6 +1626,8 @@ def is_seed(self) -> bool:
16261626
def seed_path(self) -> Path:
16271627
seed_path = Path(self.kind.path)
16281628
if not seed_path.is_absolute():
1629+
if self._path is None:
1630+
raise SQLMeshError(f"Seed model '{self.name}' has no path")
16291631
return self._path.parent / seed_path
16301632
return seed_path
16311633

@@ -1985,7 +1987,7 @@ def load_sql_based_model(
19851987
expressions: t.List[exp.Expression],
19861988
*,
19871989
defaults: t.Optional[t.Dict[str, t.Any]] = None,
1988-
path: Path = Path(),
1990+
path: t.Optional[Path] = None,
19891991
module_path: Path = Path(),
19901992
time_column_format: str = c.DEFAULT_TIME_COLUMN_FORMAT,
19911993
macros: t.Optional[MacroRegistry] = None,
@@ -2128,6 +2130,8 @@ def load_sql_based_model(
21282130
# The name of the model will be inferred from its path relative to `models/`, if it's not explicitly specified
21292131
name = meta_fields.pop("name", "")
21302132
if not name and infer_names:
2133+
if path is None:
2134+
raise ValueError("Model must have a name", path)
21312135
name = get_model_name(path)
21322136

21332137
if not name:
@@ -2517,7 +2521,7 @@ def _create_model(
25172521

25182522
def _split_sql_model_statements(
25192523
expressions: t.List[exp.Expression],
2520-
path: Path,
2524+
path: t.Optional[Path],
25212525
dialect: t.Optional[str] = None,
25222526
) -> t.Tuple[
25232527
t.Optional[exp.Expression],
@@ -2651,7 +2655,7 @@ def _refs_to_sql(values: t.Any) -> exp.Expression:
26512655
def render_meta_fields(
26522656
fields: t.Dict[str, t.Any],
26532657
module_path: Path,
2654-
path: Path,
2658+
path: t.Optional[Path],
26552659
jinja_macros: t.Optional[JinjaMacroRegistry],
26562660
macros: t.Optional[MacroRegistry],
26572661
dialect: DialectType,
@@ -2735,7 +2739,7 @@ def render_field_value(value: t.Any) -> t.Any:
27352739
def render_model_defaults(
27362740
defaults: t.Dict[str, t.Any],
27372741
module_path: Path,
2738-
path: Path,
2742+
path: t.Optional[Path],
27392743
jinja_macros: t.Optional[JinjaMacroRegistry],
27402744
macros: t.Optional[MacroRegistry],
27412745
dialect: DialectType,
@@ -2785,7 +2789,7 @@ def parse_defaults_properties(
27852789
def render_expression(
27862790
expression: exp.Expression,
27872791
module_path: Path,
2788-
path: Path,
2792+
path: t.Optional[Path],
27892793
jinja_macros: t.Optional[JinjaMacroRegistry] = None,
27902794
macros: t.Optional[MacroRegistry] = None,
27912795
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
@@ -49,6 +49,8 @@ def get_model_definitions_for_a_path(
4949
if len(tables) == 0:
5050
return []
5151

52+
if model._path is None:
53+
return []
5254
read_file = open(model._path, "r").readlines()
5355

5456
for table in tables:
@@ -72,7 +74,7 @@ def get_model_definitions_for_a_path(
7274
continue
7375
referenced_model_path = referenced_model._path
7476
# Check whether the path exists
75-
if not referenced_model_path.is_file():
77+
if not referenced_model_path or not referenced_model_path.is_file():
7678
continue
7779
referenced_model_uri = f"file://{referenced_model_path}"
7880

sqlmesh/magics.py

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

230231
formatted = format_model_expressions(
231232
expressions,
@@ -244,8 +245,9 @@ def model(self, context: Context, line: str, sql: t.Optional[str] = None) -> Non
244245
replace=True,
245246
)
246247

247-
with open(model._path, "w", encoding="utf-8") as file:
248-
file.write(formatted)
248+
if model._path:
249+
with open(model._path, "w", encoding="utf-8") as file:
250+
file.write(formatted)
249251

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

web/server/api/endpoints/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ 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)),
127+
path=str(model._path.absolute().relative_to(context.path)) if model._path else None,
128128
dialect=dialect,
129129
columns=columns,
130130
details=details,

web/server/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class Column(PydanticModel):
171171
class Model(PydanticModel):
172172
name: str
173173
fqn: str
174-
path: str
174+
path: t.Optional[str] = None
175175
dialect: str
176176
type: ModelType
177177
columns: t.List[Column]

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)