Skip to content

Commit

Permalink
I decide to trust users and not assume text by default
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Dec 8, 2024
1 parent 39d622b commit 6694cb2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/monty/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 9 additions & 2 deletions tests/test_io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os
import warnings
from pathlib import Path
from unittest.mock import patch

Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit 6694cb2

Please sign in to comment.