From 6694cb2909145b0341f96d538867890e4c14fc67 Mon Sep 17 00:00:00 2001 From: Haoyu Yang Date: Sun, 8 Dec 2024 22:04:34 +0800 Subject: [PATCH] I decide to trust users and not assume text by default --- src/monty/io.py | 3 +-- tests/test_io.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/monty/io.py b/src/monty/io.py index 55a87c0ed..83c668e83 100644 --- a/src/monty/io.py +++ b/src/monty/io.py @@ -74,11 +74,10 @@ def zopen( warnings.warn( "We strongly discourage using implicit binary/text `mode`, " f"and this would not be allowed after {_deadline}. " - "I.e. you should pass t/b in `mode`, we would assume text mode for now", + "I.e. you should pass t/b in `mode`.", FutureWarning, stacklevel=2, ) - mode += "t" # assume text mode if not specified # Warn against default `encoding` in text mode if "t" in mode and kwargs.get("encoding", None) is None: diff --git a/tests/test_io.py b/tests/test_io.py index 78551a495..073596296 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1,6 +1,7 @@ from __future__ import annotations import os +import warnings from pathlib import Path from unittest.mock import patch @@ -237,13 +238,19 @@ def test_warnings(self, extension): f.write(content) # Implicit text/binary `mode` warning + warnings.filterwarnings( + "ignore", category=EncodingWarning, message="argument not specified" + ) with ( pytest.warns( FutureWarning, match="discourage using implicit binary/text" ), - zopen(filename, "r", encoding="utf-8") as f, + zopen(filename, "r") as f, ): - assert f.readline() == content + if extension == ".txt": + assert f.readline() == content + else: + assert f.readline().decode("utf-8") == content # Implicit `mode` warning with (