Skip to content

Commit 254d73d

Browse files
authored
Fix: set utf-8 encoding in most open calls (#2503)
1 parent a36ad90 commit 254d73d

File tree

17 files changed

+62
-62
lines changed

17 files changed

+62
-62
lines changed

sqlmesh/core/loader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def _load_scripts(self) -> t.Tuple[MacroRegistry, JinjaMacroRegistry]:
247247
if macros_max_mtime
248248
else macro_file_mtime
249249
)
250-
with open(path, mode="r", encoding="utf-8") as file:
250+
with open(path, "r", encoding="utf-8") as file:
251251
jinja_macros.add_macros(extractor.extract(file.read()))
252252

253253
self._macros_max_mtime = macros_max_mtime

sqlmesh/core/model/seed.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,5 @@ def reader(self, dialect: str = "", settings: t.Optional[CsvSettings] = None) ->
102102

103103

104104
def create_seed(path: str | Path) -> Seed:
105-
with open(Path(path), "r") as fd:
105+
with open(Path(path), "r", encoding="utf-8") as fd:
106106
return Seed(content=fd.read())

sqlmesh/integrations/github/cicd/controller.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def from_obj(cls, obj: t.Dict[str, t.Any]) -> GithubEvent:
215215

216216
@classmethod
217217
def from_path(cls, path: t.Union[str, pathlib.Path]) -> GithubEvent:
218-
with open(pathlib.Path(path)) as f:
218+
with open(pathlib.Path(path), "r", encoding="utf-8") as f:
219219
return cls.from_obj(json.load(f))
220220

221221
@classmethod
@@ -435,7 +435,7 @@ def _append_output(cls, key: str, value: str) -> None:
435435
Appends the given key/value to output so they can be read by following steps
436436
"""
437437
logger.debug(f"Setting output. Key: {key}, Value: {value}")
438-
with open(os.environ["GITHUB_OUTPUT"], "a") as fh:
438+
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as fh:
439439
print(f"{key}={value}", file=fh)
440440

441441
def get_plan_summary(self, plan: Plan) -> str:

sqlmesh/magics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ def dag(self, context: Context, line: str) -> None:
528528
args = parse_argstring(self.dag, line)
529529
dag = context.get_dag(args.select_model)
530530
if args.file:
531-
with open(args.file, "w") as file:
531+
with open(args.file, "w", encoding="utf-8") as file:
532532
file.write(str(dag))
533533
# TODO: Have this go through console instead of calling display directly
534534
self.display(dag)

sqlmesh/schedulers/airflow/operators/databricks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def execute(self, context: Context) -> None:
9191
command_payload = self._target.serialized_command_payload(context)
9292
with tempfile.TemporaryDirectory() as tmp:
9393
local_payload_path = os.path.join(tmp, commands.COMMAND_PAYLOAD_FILE_NAME)
94-
with open(local_payload_path, "w") as payload_fd:
94+
with open(local_payload_path, "w", encoding="utf-8") as payload_fd:
9595
payload_fd.write(command_payload)
9696
remote_payload_path = os.path.join(
9797
self._dbfs_location, commands.COMMAND_PAYLOAD_FILE_NAME

sqlmesh/schedulers/airflow/operators/spark_submit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def execute(self, context: Context) -> None:
7676
command_payload = self._target.serialized_command_payload(context)
7777
with tempfile.TemporaryDirectory() as tmp:
7878
payload_file_path = os.path.join(tmp, commands.COMMAND_PAYLOAD_FILE_NAME)
79-
with open(payload_file_path, "w") as payload_fd:
79+
with open(payload_file_path, "w", encoding="utf-8") as payload_fd:
8080
payload_fd.write(command_payload)
8181

8282
if self._hook is None:

tests/cli/test_cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def create_example_project(temp_dir) -> None:
3636
- Overwriting the config.yaml file so the duckdb database file will be created in the temp_dir directory
3737
"""
3838
init_example_project(temp_dir, "duckdb")
39-
with open(temp_dir / "config.yaml", "w") as f:
39+
with open(temp_dir / "config.yaml", "w", encoding="utf-8") as f:
4040
f.write(
4141
f"""gateways:
4242
local:
@@ -53,7 +53,7 @@ def create_example_project(temp_dir) -> None:
5353

5454

5555
def update_incremental_model(temp_dir) -> None:
56-
with open(temp_dir / "models" / "incremental_model.sql", "w") as f:
56+
with open(temp_dir / "models" / "incremental_model.sql", "w", encoding="utf-8") as f:
5757
f.write(
5858
"""
5959
MODEL (
@@ -80,7 +80,7 @@ def update_incremental_model(temp_dir) -> None:
8080

8181

8282
def update_full_model(temp_dir) -> None:
83-
with open(temp_dir / "models" / "full_model.sql", "w") as f:
83+
with open(temp_dir / "models" / "full_model.sql", "w", encoding="utf-8") as f:
8484
f.write(
8585
"""
8686
MODEL (

tests/core/test_config.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
@pytest.fixture(scope="session")
2929
def yaml_config_path(tmp_path_factory) -> Path:
3030
config_path = tmp_path_factory.mktemp("yaml_config") / "config.yaml"
31-
with open(config_path, "w") as fd:
31+
with open(config_path, "w", encoding="utf-8") as fd:
3232
fd.write(
3333
"""
3434
gateways:
@@ -47,7 +47,7 @@ def yaml_config_path(tmp_path_factory) -> Path:
4747
@pytest.fixture(scope="session")
4848
def python_config_path(tmp_path_factory) -> Path:
4949
config_path = tmp_path_factory.mktemp("python_config") / "config.py"
50-
with open(config_path, "w") as fd:
50+
with open(config_path, "w", encoding="utf-8") as fd:
5151
fd.write(
5252
"""from sqlmesh.core.config import Config, DuckDBConnectionConfig, GatewayConfig, ModelDefaultsConfig
5353
config = Config(gateways=GatewayConfig(connection=DuckDBConnectionConfig()), model_defaults=ModelDefaultsConfig(dialect=''))
@@ -163,11 +163,11 @@ def test_load_config_from_paths(yaml_config_path: Path, python_config_path: Path
163163

164164
def test_load_config_multiple_config_files_in_folder(tmp_path):
165165
config_a_path = tmp_path / "config.yaml"
166-
with open(config_a_path, "w") as fd:
166+
with open(config_a_path, "w", encoding="utf-8") as fd:
167167
fd.write("project: project_a")
168168

169169
config_b_path = tmp_path / "config.yml"
170-
with open(config_b_path, "w") as fd:
170+
with open(config_b_path, "w", encoding="utf-8") as fd:
171171
fd.write("project: project_b")
172172

173173
with pytest.raises(ConfigError, match=r"^Multiple configuration files found in folder.*"):
@@ -285,7 +285,7 @@ def test_load_config_from_env_invalid_variable_name():
285285

286286
def test_load_config_from_python_module_missing_config(tmp_path):
287287
config_path = tmp_path / "missing_config.py"
288-
with open(config_path, "w") as fd:
288+
with open(config_path, "w", encoding="utf-8") as fd:
289289
fd.write("from sqlmesh.core.config import Config")
290290

291291
with pytest.raises(ConfigError, match="Config 'config' was not found."):
@@ -294,7 +294,7 @@ def test_load_config_from_python_module_missing_config(tmp_path):
294294

295295
def test_load_config_from_python_module_invalid_config_object(tmp_path):
296296
config_path = tmp_path / "invalid_config.py"
297-
with open(config_path, "w") as fd:
297+
with open(config_path, "w", encoding="utf-8") as fd:
298298
fd.write("config = None")
299299

300300
with pytest.raises(
@@ -306,7 +306,7 @@ def test_load_config_from_python_module_invalid_config_object(tmp_path):
306306

307307
def test_cloud_composer_scheduler_config(tmp_path_factory):
308308
config_path = tmp_path_factory.mktemp("yaml_config") / "config.yaml"
309-
with open(config_path, "w") as fd:
309+
with open(config_path, "w", encoding="utf-8") as fd:
310310
fd.write(
311311
"""
312312
gateways:
@@ -365,7 +365,7 @@ def test_cloud_composer_scheduler_config(tmp_path_factory):
365365
)
366366
def test_environment_catalog_mapping(tmp_path_factory, mapping, expected, dialect, raise_error):
367367
config_path = tmp_path_factory.mktemp("yaml_config") / "config.yaml"
368-
with open(config_path, "w") as fd:
368+
with open(config_path, "w", encoding="utf-8") as fd:
369369
fd.write(
370370
f"""
371371
gateways:
@@ -395,7 +395,7 @@ def test_environment_catalog_mapping(tmp_path_factory, mapping, expected, dialec
395395

396396
def test_load_feature_flag(tmp_path_factory):
397397
config_path = tmp_path_factory.mktemp("yaml_config") / "config.yaml"
398-
with open(config_path, "w") as fd:
398+
with open(config_path, "w", encoding="utf-8") as fd:
399399
fd.write(
400400
"""
401401
gateways:

tests/core/test_connection_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def snowflake_key_no_passphrase_path() -> str:
3737

3838
@pytest.fixture
3939
def snowflake_key_no_passphrase_pem(snowflake_key_no_passphrase_path) -> str:
40-
with open(snowflake_key_no_passphrase_path, "r") as key:
40+
with open(snowflake_key_no_passphrase_path, "r", encoding="utf-8") as key:
4141
return key.read()
4242

4343

@@ -58,7 +58,7 @@ def snowflake_key_passphrase_path() -> str:
5858

5959
@pytest.fixture
6060
def snowflake_key_passphrase_pem(snowflake_key_passphrase_path) -> str:
61-
with open(snowflake_key_passphrase_path, "r") as key:
61+
with open(snowflake_key_passphrase_path, "r", encoding="utf-8") as key:
6262
return key.read()
6363

6464

tests/core/test_model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -763,14 +763,14 @@ def test_seed_model_diff(tmp_path):
763763
model_a_csv_path = (tmp_path / "model_a.csv").absolute()
764764
model_b_csv_path = (tmp_path / "model_b.csv").absolute()
765765

766-
with open(model_a_csv_path, "w") as fd:
766+
with open(model_a_csv_path, "w", encoding="utf-8") as fd:
767767
fd.write(
768768
"""key,value
769769
1,value_a
770770
"""
771771
)
772772

773-
with open(model_b_csv_path, "w") as fd:
773+
with open(model_b_csv_path, "w", encoding="utf-8") as fd:
774774
fd.write(
775775
"""key,value
776776
2,value_b
@@ -860,7 +860,7 @@ def test_seed_pre_statements_only():
860860
def test_seed_model_custom_types(tmp_path):
861861
model_csv_path = (tmp_path / "model.csv").absolute()
862862

863-
with open(model_csv_path, "w") as fd:
863+
with open(model_csv_path, "w", encoding="utf-8") as fd:
864864
fd.write(
865865
"""key,ds_date,ds_timestamp,b_a,b_b,i,i_str
866866
123,2022-01-01,2022-01-01,false,0,321,321

0 commit comments

Comments
 (0)