mirror of
https://github.com/esphome/esphome.git
synced 2026-08-22 22:26:21 +00:00
[ci] Enable the ruff rule requiring explicit encoding on text file I/O (#17897)
This commit is contained in:
@@ -58,7 +58,7 @@ def wrapped_load_pem_private_key(value, password):
|
|||||||
|
|
||||||
def read_relative_config_path(value):
|
def read_relative_config_path(value):
|
||||||
# pylint: disable=unspecified-encoding
|
# pylint: disable=unspecified-encoding
|
||||||
return Path(CORE.relative_config_path(value)).read_text()
|
return Path(CORE.relative_config_path(value)).read_text(encoding="utf-8")
|
||||||
|
|
||||||
|
|
||||||
def _validate_load_certificate(value):
|
def _validate_load_certificate(value):
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ def run_extra_script(
|
|||||||
script shouldn't block the build.
|
script shouldn't block the build.
|
||||||
"""
|
"""
|
||||||
env = _FakeSConsEnv(board_mcu=idf_target, pio_env=f"esphome_{idf_target}")
|
env = _FakeSConsEnv(board_mcu=idf_target, pio_env=f"esphome_{idf_target}")
|
||||||
code = compile(script_path.read_text(), str(script_path), "exec")
|
code = compile(script_path.read_text(encoding="utf-8"), str(script_path), "exec")
|
||||||
old_cwd = Path.cwd()
|
old_cwd = Path.cwd()
|
||||||
try:
|
try:
|
||||||
os.chdir(library_dir)
|
os.chdir(library_dir)
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ def _find_app_partition_size(partitions_csv: Path) -> int:
|
|||||||
"""
|
"""
|
||||||
if not partitions_csv.is_file():
|
if not partitions_csv.is_file():
|
||||||
raise ValueError(f"partitions.csv not found at {partitions_csv}")
|
raise ValueError(f"partitions.csv not found at {partitions_csv}")
|
||||||
for row in csv.reader(partitions_csv.read_text().splitlines()):
|
for row in csv.reader(partitions_csv.read_text(encoding="utf-8").splitlines()):
|
||||||
cells = [c.strip() for c in row]
|
cells = [c.strip() for c in row]
|
||||||
if not cells or cells[0].startswith("#") or len(cells) < 5:
|
if not cells or cells[0].startswith("#") or len(cells) < 5:
|
||||||
continue
|
continue
|
||||||
@@ -89,7 +89,7 @@ def print_summary(size_json: Path, partitions_csv: Path | None) -> None:
|
|||||||
_LOGGER.debug("Skipping size summary: %s not found", size_json)
|
_LOGGER.debug("Skipping size summary: %s not found", size_json)
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
data = json.loads(size_json.read_text())
|
data = json.loads(size_json.read_text(encoding="utf-8"))
|
||||||
except (OSError, json.JSONDecodeError) as e:
|
except (OSError, json.JSONDecodeError) as e:
|
||||||
_LOGGER.debug("Skipping size summary: %s", e)
|
_LOGGER.debug("Skipping size summary: %s", e)
|
||||||
return
|
return
|
||||||
|
|||||||
+6
-2
@@ -110,8 +110,12 @@ def prepare(
|
|||||||
CONF_CLIENT_CERTIFICATE_KEY
|
CONF_CLIENT_CERTIFICATE_KEY
|
||||||
):
|
):
|
||||||
with (
|
with (
|
||||||
tempfile.NamedTemporaryFile(mode="w+", delete=False) as cert_file,
|
tempfile.NamedTemporaryFile(
|
||||||
tempfile.NamedTemporaryFile(mode="w+", delete=False) as key_file,
|
encoding="utf-8", mode="w+", delete=False
|
||||||
|
) as cert_file,
|
||||||
|
tempfile.NamedTemporaryFile(
|
||||||
|
encoding="utf-8", mode="w+", delete=False
|
||||||
|
) as key_file,
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
cert_file.write(config[CONF_MQTT].get(CONF_CLIENT_CERTIFICATE))
|
cert_file.write(config[CONF_MQTT].get(CONF_CLIENT_CERTIFICATE))
|
||||||
|
|||||||
@@ -110,6 +110,10 @@ target-version = "py312"
|
|||||||
exclude = ['generated']
|
exclude = ['generated']
|
||||||
|
|
||||||
[tool.ruff.lint]
|
[tool.ruff.lint]
|
||||||
|
# Preview mode is scoped: with explicit-preview-rules only rules named in
|
||||||
|
# select run in preview, prefixes like "PL" keep their stable set.
|
||||||
|
preview = true
|
||||||
|
explicit-preview-rules = true
|
||||||
select = [
|
select = [
|
||||||
"B", # flake8-bugbear
|
"B", # flake8-bugbear
|
||||||
"BLE", # flake8-blind-except
|
"BLE", # flake8-blind-except
|
||||||
@@ -131,6 +135,7 @@ select = [
|
|||||||
"PGH", # pygrep-hooks
|
"PGH", # pygrep-hooks
|
||||||
"PIE", # flake8-pie
|
"PIE", # flake8-pie
|
||||||
"PL", # pylint
|
"PL", # pylint
|
||||||
|
"PLW1514", # require explicit encoding on text file I/O (Windows defaults to cp1252)
|
||||||
"PTH", # flake8-use-pathlib
|
"PTH", # flake8-use-pathlib
|
||||||
"PYI", # flake8-pyi
|
"PYI", # flake8-pyi
|
||||||
"Q", # flake8-quotes
|
"Q", # flake8-quotes
|
||||||
|
|||||||
@@ -194,7 +194,7 @@ def cmd_update(args: argparse.Namespace) -> int:
|
|||||||
|
|
||||||
|
|
||||||
def cmd_har_only(args: argparse.Namespace) -> int:
|
def cmd_har_only(args: argparse.Namespace) -> int:
|
||||||
Path(args.har).write_text(run_waterfall(TARGET_MODULE))
|
Path(args.har).write_text(run_waterfall(TARGET_MODULE), encoding="utf-8")
|
||||||
print(f"Wrote waterfall HAR to {args.har}")
|
print(f"Wrote waterfall HAR to {args.har}")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|||||||
@@ -269,7 +269,7 @@ def main() -> int:
|
|||||||
if args.output_build_dir and build_dir:
|
if args.output_build_dir and build_dir:
|
||||||
build_dir_path = Path(args.output_build_dir)
|
build_dir_path = Path(args.output_build_dir)
|
||||||
build_dir_path.parent.mkdir(parents=True, exist_ok=True)
|
build_dir_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
build_dir_path.write_text(build_dir)
|
build_dir_path.write_text(build_dir, encoding="utf-8")
|
||||||
print(f"Wrote build directory to {args.output_build_dir}", file=sys.stderr)
|
print(f"Wrote build directory to {args.output_build_dir}", file=sys.stderr)
|
||||||
|
|
||||||
# Run detailed analysis if build directory available
|
# Run detailed analysis if build directory available
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ def _read_codspeed_version(cmake_path: Path) -> str:
|
|||||||
"""Extract CODSPEED_VERSION from core/CMakeLists.txt."""
|
"""Extract CODSPEED_VERSION from core/CMakeLists.txt."""
|
||||||
if not cmake_path.exists():
|
if not cmake_path.exists():
|
||||||
return "0.0.0"
|
return "0.0.0"
|
||||||
for line in cmake_path.read_text().splitlines():
|
for line in cmake_path.read_text(encoding="utf-8").splitlines():
|
||||||
if line.startswith("set(CODSPEED_VERSION"):
|
if line.startswith("set(CODSPEED_VERSION"):
|
||||||
return line.split()[1].rstrip(")")
|
return line.split()[1].rstrip(")")
|
||||||
return "0.0.0"
|
return "0.0.0"
|
||||||
|
|||||||
@@ -367,7 +367,7 @@ def run_esphome_test(
|
|||||||
output_file = build_dir / f"{component}.{test_name}.{platform_with_version}.yaml"
|
output_file = build_dir / f"{component}.{test_name}.{platform_with_version}.yaml"
|
||||||
|
|
||||||
# Copy base file and substitute component test file reference
|
# Copy base file and substitute component test file reference
|
||||||
base_content = base_file.read_text()
|
base_content = base_file.read_text(encoding="utf-8")
|
||||||
# Get relative path from build dir to test file
|
# Get relative path from build dir to test file
|
||||||
repo_root = Path(__file__).parent.parent
|
repo_root = Path(__file__).parent.parent
|
||||||
component_test_ref = f"../../{test_file.relative_to(repo_root / 'tests')}"
|
component_test_ref = f"../../{test_file.relative_to(repo_root / 'tests')}"
|
||||||
@@ -524,7 +524,7 @@ def run_grouped_test(
|
|||||||
|
|
||||||
# Create test file that includes merged config
|
# Create test file that includes merged config
|
||||||
output_file = build_dir / f"test_{group_name}.{platform_with_version}.yaml"
|
output_file = build_dir / f"test_{group_name}.{platform_with_version}.yaml"
|
||||||
base_content = base_file.read_text()
|
base_content = base_file.read_text(encoding="utf-8")
|
||||||
merged_ref = merged_config_file.name
|
merged_ref = merged_config_file.name
|
||||||
output_content = base_content.replace("$component_test_file", merged_ref)
|
output_content = base_content.replace("$component_test_file", merged_ref)
|
||||||
output_file.write_text(output_content)
|
output_file.write_text(output_content)
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ def _make_build_dir(tmp_path: Path, name: str = "mydevice") -> Path:
|
|||||||
|
|
||||||
def _touch(path: Path) -> Path:
|
def _touch(path: Path) -> Path:
|
||||||
path.parent.mkdir(parents=True, exist_ok=True)
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
path.write_text("")
|
path.write_text("", encoding="utf-8")
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -74,13 +74,13 @@ def _write_storage(
|
|||||||
"framework": "arduino",
|
"framework": "arduino",
|
||||||
"core_platform": core_platform,
|
"core_platform": core_platform,
|
||||||
}
|
}
|
||||||
storage_path.write_text(json.dumps(data))
|
storage_path.write_text(json.dumps(data), encoding="utf-8")
|
||||||
|
|
||||||
|
|
||||||
def _write_cache(cache_path: Path, body: str = _VALIDATED_CONFIG_YAML) -> Path:
|
def _write_cache(cache_path: Path, body: str = _VALIDATED_CONFIG_YAML) -> Path:
|
||||||
"""Write the cache file and return it."""
|
"""Write the cache file and return it."""
|
||||||
cache_path.parent.mkdir(parents=True, exist_ok=True)
|
cache_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
cache_path.write_text(body)
|
cache_path.write_text(body, encoding="utf-8")
|
||||||
return cache_path
|
return cache_path
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user