Skip to content

Commit 3e7ff42

Browse files
ymurongyanchao murongjschoedl
authored
fix: Non-ASCII characters (e.g. accented letters) in a data contract … (#1650)
* fix: Non-ASCII characters (e.g. accented letters) in a data contract file were garbled on Windows * test: cover the fetch_schema UTF-8 reads under a cp1252 default encoding The non_utf8_default_encoding fixture now mirrors open()'s signature (a positional buffering argument no longer lands in encoding) and also patches io.open, so Path.open and importlib.resources reads default to cp1252 too. New tests check that fetch_schema decodes both the bundled and a local schema file as UTF-8. * fix: read and write import sources, init templates and config files as UTF-8 Importers (avro, bigquery, dbml, dcs, iceberg, jsonschema, protobuf, sql, unity), datacontract init, the config file and the saved API response of datacontract test used the platform default encoding. On Windows, non-ASCII characters in these files were garbled, or a protobuf source crashed with UnicodeDecodeError. * test: remove duplicate Excel encoding test The accented-characters test in test_export_excel.py is redundant — the core encoding fix is already well-covered by test_lint_files.py, which exercises the same code path under a cp1252 default encoding via the non_utf8_default_encoding fixture. There is no Excel-specific behavior to test here. --------- Co-authored-by: yanchao murong <yamur@Mac.lan> Co-authored-by: Jakob Schödl <jakob.schoedl@entropy-data.com>
1 parent bfdb4f1 commit 3e7ff42

19 files changed

Lines changed: 66 additions & 18 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
### Fixed
2323
- Loading a DCS contract dropped quality rule `arguments`, so `invalidValues` and `missingValues` rules lost their configuration
2424
- `datacontract test --dry-run`: a check that could not be planned no longer reports the run as `skipped`
25+
- Non-ASCII characters in data contract and import source files are now decoded as UTF-8, fixing garbling on Windows (#1650 @ymurong)
2526
- `datacontract import spark` preserves `varchar(...)` and `char(...)` types nested inside `struct`, `array` and `map` columns instead of widening them to `string` (#1634)
2627

2728
## [1.2.1] - 2026-09-22

‎datacontract/command_init.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ def init(
2828
console.print("File already exists, use --overwrite to overwrite")
2929
raise typer.Exit(code=1)
3030
template_str = get_init_template(template)
31-
with open(location, "w") as f:
31+
with open(location, "w", encoding="utf-8") as f:
3232
f.write(template_str)
3333
console.print("📄 data contract written to " + location)

‎datacontract/config/settings.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def from_yaml(cls, path: "str | Path") -> "Config":
356356

357357
path = Path(path)
358358
try:
359-
data = yaml.safe_load(path.read_text())
359+
data = yaml.safe_load(path.read_text(encoding="utf-8"))
360360
except yaml.YAMLError as e:
361361
raise ValueError(f"Config file {path} is not valid YAML: {e}")
362362
if data is None:

‎datacontract/engines/data_contract_test.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ def process_api_response(run, server, config: Config | None = None):
450450
reason=f"Failed to fetch API response from {server.location}: {e}",
451451
engine="datacontract-cli",
452452
)
453-
with open(f"{tmp_dir.name}/api_response.json", "w") as f:
453+
with open(f"{tmp_dir.name}/api_response.json", "w", encoding="utf-8") as f:
454454
f.write(response.text)
455455
run.log_info(f"Saved API response to {tmp_dir.name}/api_response.json")
456456
new_server = Server(

‎datacontract/imports/avro_importer.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def import_source(self, source: str, import_args: dict) -> OpenDataContractStand
3636
def import_avro(source: str) -> OpenDataContractStandard:
3737
"""Import an Avro schema from a file."""
3838
try:
39-
with open(source, "r") as file:
39+
with open(source, "r", encoding="utf-8") as file:
4040
avro_schema = avro.schema.parse(file.read())
4141
except Exception as e:
4242
raise DataContractException(

‎datacontract/imports/bigquery_importer.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def import_source(self, source: str, import_args: dict) -> OpenDataContractStand
2828

2929
def import_bigquery_from_json(source: str) -> OpenDataContractStandard:
3030
try:
31-
with open(source, "r") as file:
31+
with open(source, "r", encoding="utf-8") as file:
3232
bigquery_schema = json.loads(file.read())
3333
except json.JSONDecodeError as e:
3434
raise DataContractException(

‎datacontract/imports/dbml_importer.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def import_dbml_from_source(
3030
import_tables: List[str],
3131
) -> OpenDataContractStandard:
3232
try:
33-
with open(source, "r") as file:
33+
with open(source, "r", encoding="utf-8") as file:
3434
dbml_schema = PyDBML(file)
3535
except ParseException as e:
3636
raise DataContractException(

‎datacontract/imports/dcs_importer.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def _resolve_file_ref(ref_path: str) -> Optional[Dict[str, Any]]:
347347
return None
348348

349349
# Read the file
350-
with open(file_path, "r") as f:
350+
with open(file_path, "r", encoding="utf-8") as f:
351351
content = f.read()
352352

353353
data = yaml.safe_load(content)

‎datacontract/imports/iceberg_importer.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def import_from_catalog(import_args: dict, config: "Config", catalog_url: str) -
6666

6767

6868
def load_and_validate_iceberg_schema(source: str) -> Schema:
69-
with open(source, "r") as file:
69+
with open(source, "r", encoding="utf-8") as file:
7070
try:
7171
return Schema.model_validate_json(file.read())
7272
except ValidationError as e:

‎datacontract/imports/jsonschema_importer.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def import_jsonschema(source: str) -> OpenDataContractStandard:
4848
def load_and_validate_json_schema(source: str) -> dict:
4949
"""Load and validate a JSON Schema file."""
5050
try:
51-
with open(source, "r") as file:
51+
with open(source, "r", encoding="utf-8") as file:
5252
json_schema = json.loads(file.read())
5353

5454
validator = fastjsonschema.compile({})

0 commit comments

Comments
 (0)