Skip to content

Commit 2be6d82

Browse files
Increase Type Checking (#26)
* chore: update project configuration and clean up files - Add new directories to .gitignore for Dagster project storage, logs, history, and schedules. - Modify Makefile to support Windows environments and add new clean commands for Dagster and database. - Update pyproject.toml to change version to 0.8.0 and add new dependencies for FastAPI and SSE-Starlette. - Refactor type hints across multiple files to use modern syntax and improve consistency. - Enhance console and event handling in dagster_sqlmesh for better logging and event management. - Clean up imports and ensure proper typing throughout the codebase. - Update sample project definitions for compatibility with new changes. * refactor: improve type hints and clean up imports in conftest.py - Update type hints to use modern syntax throughout the file. - Clean up and reorganize import statements for better clarity. - Enhance function signatures with appropriate return types. - Ensure consistency in typing for fixtures and context classes. * chore: simplify Makefile by removing OS-specific checks - Unified command definitions for Python, SQLMESH, and UV to streamline setup. - Removed Windows-specific command handling for a cleaner and more consistent configuration. - Simplified activation and deactivation commands for the virtual environment. * refactor(test): improve assertion readability in test_restating_models - Simplified assertion statements in the test_restating_models function for better clarity. - Enhanced readability by formatting assertions with inline messages. - Maintained existing test logic and functionality. --------- Co-authored-by: Kevin Longe <[email protected]>
1 parent b328d77 commit 2be6d82

22 files changed

+450
-286
lines changed

.gitignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,9 @@ dbt_packages/
6161
# Python
6262
*.pyc
6363

64-
*.db
64+
*.db
65+
66+
sample/dagster_project/storage/
67+
sample/dagster_project/logs/
68+
sample/dagster_project/history/
69+
sample/dagster_project/schedules/

Makefile

+15-2
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,26 @@ pyright:
4545
pnpm pyright
4646

4747
# Sample project commands
48+
clean-dagster:
49+
rm -rf sample/dagster_project/storage sample/dagster_project/logs sample/dagster_project/history
50+
51+
clean-db:
52+
$(PYTHON_CMD) -c "import duckdb; conn = duckdb.connect('db.db'); [conn.execute(cmd[0]) for cmd in conn.execute(\"\"\"SELECT 'DROP TABLE ' || table_schema || '.' || table_name || ' CASCADE;' as drop_cmd FROM information_schema.tables WHERE table_schema != 'sources' AND table_schema != 'information_schema' AND table_type = 'BASE TABLE'\"\"\").fetchall()]; [conn.execute(cmd[0]) for cmd in conn.execute(\"\"\"SELECT 'DROP VIEW ' || table_schema || '.' || table_name || ' CASCADE;' as drop_cmd FROM information_schema.tables WHERE table_schema != 'sources' AND table_schema != 'information_schema' AND table_type = 'VIEW'\"\"\").fetchall()]; conn.close()"
4853

4954
dagster-dev: clean-dagster
50-
DAGSTER_HOME=$(CURDIR)/sample/dagster_project $(PYTHON_CMD) -m dagster dev -h 0.0.0.0 -w
55+
@DAGSTER_HOME="$(subst \,/,$(CURDIR))/sample/dagster_project" "$(PYTHON_CMD)" -m dagster dev -f sample/dagster_project/definitions.py -h 0.0.0.0
5156

5257
dev: dagster-dev # Alias for dagster-dev
5358

5459
dagster-materialize:
5560
$(PYTHON_CMD) -m dagster asset materialize -f sample/dagster_project/definitions.py --select '*'
5661

57-
.PHONY: init init-python install-python-deps upgrade-python-deps clean test mypy check-pnpm install-node-deps upgrade-node-deps sample-dev dagster-dev dagster-materialize clean-dagster
62+
sqlmesh-plan:
63+
cd sample/sqlmesh_project && $(SQLMESH_CMD) plan
64+
65+
sqlmesh-run:
66+
cd sample/sqlmesh_project && $(SQLMESH_CMD) run
67+
68+
clean-dev: clean-db clean-dagster dev
69+
70+
.PHONY: init init-python install-python-deps upgrade-python-deps clean test mypy check-pnpm install-node-deps upgrade-node-deps sample-dev dagster-dev dagster-materialize clean-dagster clean-db clean-dev

dagster_sqlmesh/asset.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import typing as t
21
import logging
2+
import typing as t
33

44
from dagster import (
5-
multi_asset,
5+
AssetsDefinition,
66
RetryPolicy,
7+
multi_asset,
78
)
89

910
from dagster_sqlmesh.controller import DagsterSQLMeshController
@@ -19,15 +20,15 @@ def sqlmesh_assets(
1920
*,
2021
environment: str,
2122
config: SQLMeshContextConfig,
22-
name: t.Optional[str] = None,
23-
dagster_sqlmesh_translator: t.Optional[SQLMeshDagsterTranslator] = None,
23+
name: str | None = None,
24+
dagster_sqlmesh_translator: SQLMeshDagsterTranslator | None = None,
2425
compute_kind: str = "sqlmesh",
25-
op_tags: t.Optional[t.Mapping[str, t.Any]] = None,
26-
required_resource_keys: t.Optional[t.Set[str]] = None,
27-
retry_policy: t.Optional[RetryPolicy] = None,
26+
op_tags: t.Mapping[str, t.Any] | None = None,
27+
required_resource_keys: set[str] | None = None,
28+
retry_policy: RetryPolicy | None = None,
2829
# For now we don't set this by default
2930
enabled_subsetting: bool = False,
30-
):
31+
) -> t.Callable[[t.Callable[..., t.Any]], AssetsDefinition]:
3132
controller = DagsterSQLMeshController.setup_with_config(config)
3233
if not dagster_sqlmesh_translator:
3334
dagster_sqlmesh_translator = SQLMeshDagsterTranslator()

dagster_sqlmesh/config.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
from typing import Optional, Dict, Any
21
from dataclasses import dataclass
2+
from typing import Any
33

44
from dagster import Config
5-
from sqlmesh.core.config import Config as MeshConfig
65
from pydantic import Field
6+
from sqlmesh.core.config import Config as MeshConfig
77

88

99
@dataclass
1010
class ConfigOverride:
11-
config_as_dict: Dict
11+
config_as_dict: dict[str, Any]
1212

13-
def dict(self):
13+
def dict(self) -> dict[str, Any]:
1414
return self.config_as_dict
1515

1616

@@ -24,10 +24,10 @@ class SQLMeshContextConfig(Config):
2424

2525
path: str
2626
gateway: str
27-
config_override: Optional[Dict[str, Any]] = Field(default_factory=lambda: None)
27+
config_override: dict[str, Any] | None = Field(default_factory=lambda: None)
2828

2929
@property
30-
def sqlmesh_config(self):
30+
def sqlmesh_config(self) -> MeshConfig | None:
3131
if self.config_override:
3232
return MeshConfig.parse_obj(self.config_override)
3333
return None

dagster_sqlmesh/conftest.py

+24-19
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
11
import logging
2+
import os
3+
import shutil
24
import sys
35
import tempfile
4-
import shutil
5-
import os
6-
from dataclasses import dataclass
76
import typing as t
7+
from dataclasses import dataclass
88

9-
import pytest
109
import duckdb
1110
import polars
12-
from sqlmesh.utils.date import TimeLike
13-
from sqlmesh.core.console import get_console
11+
import pytest
1412
from sqlmesh.core.config import (
1513
Config as SQLMeshConfig,
16-
GatewayConfig,
1714
DuckDBConnectionConfig,
15+
GatewayConfig,
1816
ModelDefaultsConfig,
1917
)
18+
from sqlmesh.core.console import get_console
19+
from sqlmesh.utils.date import TimeLike
2020

2121
from dagster_sqlmesh.config import SQLMeshContextConfig
22-
from dagster_sqlmesh.events import ConsoleRecorder
22+
from dagster_sqlmesh.console import ConsoleEvent
2323
from dagster_sqlmesh.controller.base import PlanOptions, RunOptions
2424
from dagster_sqlmesh.controller.dagster import DagsterSQLMeshController
25+
from dagster_sqlmesh.events import ConsoleRecorder
2526

2627
logger = logging.getLogger(__name__)
2728

2829

2930
@pytest.fixture(scope="session", autouse=True)
30-
def setup_debug_logging_for_tests():
31+
def setup_debug_logging_for_tests() -> None:
3132
root_logger = logging.getLogger(__name__.split(".")[0])
3233
root_logger.setLevel(logging.DEBUG)
3334

3435
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
3536

3637

3738
@pytest.fixture
38-
def sample_sqlmesh_project():
39+
def sample_sqlmesh_project() -> t.Iterator[str]:
3940
"""Creates a temporary sqlmesh project by copying the sample project"""
4041
with tempfile.TemporaryDirectory() as tmp_dir:
4142
project_dir = shutil.copytree(
@@ -56,19 +57,21 @@ class SQLMeshTestContext:
5657
db_path: str
5758
context_config: SQLMeshContextConfig
5859

59-
def create_controller(self, enable_debug_console: bool = False):
60+
def create_controller(
61+
self, enable_debug_console: bool = False
62+
) -> DagsterSQLMeshController:
6063
console = None
6164
if enable_debug_console:
6265
console = get_console()
6366
return DagsterSQLMeshController.setup_with_config(
6467
self.context_config, debug_console=console
6568
)
6669

67-
def query(self, *args, **kwargs):
70+
def query(self, *args: t.Any, **kwargs: t.Any) -> t.Any:
6871
conn = duckdb.connect(self.db_path)
6972
return conn.sql(*args, **kwargs).fetchall()
7073

71-
def initialize_test_source(self):
74+
def initialize_test_source(self) -> None:
7275
conn = duckdb.connect(self.db_path)
7376
conn.sql(
7477
"""
@@ -102,14 +105,14 @@ def plan_and_run(
102105
self,
103106
*,
104107
environment: str,
105-
execution_time: t.Optional[TimeLike] = None,
108+
execution_time: TimeLike | None = None,
106109
enable_debug_console: bool = False,
107-
start: t.Optional[TimeLike] = None,
108-
end: t.Optional[TimeLike] = None,
109-
select_models: t.Optional[t.List[str]] = None,
110+
start: TimeLike | None = None,
111+
end: TimeLike | None = None,
112+
select_models: list[str] | None = None,
110113
restate_selected: bool = False,
111114
skip_run: bool = False,
112-
):
115+
) -> t.Iterator[ConsoleEvent] | None:
113116
"""Runs plan and run on SQLMesh with the given configuration and record all of the generated events.
114117
115118
Args:
@@ -152,7 +155,9 @@ def plan_and_run(
152155

153156

154157
@pytest.fixture
155-
def sample_sqlmesh_test_context(sample_sqlmesh_project: str):
158+
def sample_sqlmesh_test_context(
159+
sample_sqlmesh_project: str,
160+
) -> t.Iterator[SQLMeshTestContext]:
156161
db_path = os.path.join(sample_sqlmesh_project, "db.db")
157162
config = SQLMeshConfig(
158163
gateways={

0 commit comments

Comments
 (0)