Skip to content

Commit 3522b41

Browse files
authored
CI/TST: Run tests in development mode with EncodingWarnings (#54472)
1 parent c334f42 commit 3522b41

File tree

13 files changed

+27
-25
lines changed

13 files changed

+27
-25
lines changed

ci/run_tests.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ echo PYTHONHASHSEED=$PYTHONHASHSEED
1010

1111
COVERAGE="-s --cov=pandas --cov-report=xml --cov-append --cov-config=pyproject.toml"
1212

13-
PYTEST_CMD="MESONPY_EDITABLE_VERBOSE=1 pytest -r fEs -n $PYTEST_WORKERS --dist=loadfile $TEST_ARGS $COVERAGE $PYTEST_TARGET"
13+
PYTEST_CMD="MESONPY_EDITABLE_VERBOSE=1 PYTHONDEVMODE=1 PYTHONWARNDEFAULTENCODING=1 pytest -r fEs -n $PYTEST_WORKERS --dist=loadfile $TEST_ARGS $COVERAGE $PYTEST_TARGET"
1414

1515
if [[ "$PATTERN" ]]; then
1616
PYTEST_CMD="$PYTEST_CMD -m \"$PATTERN\""

pandas/_testing/contexts.py

+2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ def ensure_clean(
128128
encoding = kwargs.pop("encoding", None)
129129
if return_filelike:
130130
kwargs.setdefault("mode", "w+b")
131+
if encoding is None and "b" not in kwargs["mode"]:
132+
encoding = "utf-8"
131133
handle_or_str = open(path, encoding=encoding, **kwargs)
132134

133135
try:

pandas/io/common.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,8 @@ class _BufferedWriter(BytesIO, ABC): # type: ignore[misc]
935935
This wrapper writes to the underlying buffer on close.
936936
"""
937937

938+
buffer = BytesIO()
939+
938940
@abstractmethod
939941
def write_to_buffer(self) -> None:
940942
...
@@ -943,15 +945,13 @@ def close(self) -> None:
943945
if self.closed:
944946
# already closed
945947
return
946-
if self.getvalue():
948+
if self.getbuffer().nbytes:
947949
# write to buffer
948950
self.seek(0)
949-
# error: "_BufferedWriter" has no attribute "buffer"
950-
with self.buffer: # type: ignore[attr-defined]
951+
with self.buffer:
951952
self.write_to_buffer()
952953
else:
953-
# error: "_BufferedWriter" has no attribute "buffer"
954-
self.buffer.close() # type: ignore[attr-defined]
954+
self.buffer.close()
955955
super().close()
956956

957957

@@ -967,13 +967,12 @@ def __init__(
967967
super().__init__()
968968
self.archive_name = archive_name
969969
self.name = name
970-
# error: Argument "fileobj" to "open" of "TarFile" has incompatible
971-
# type "Union[ReadBuffer[bytes], WriteBuffer[bytes], None]"; expected
972-
# "Optional[IO[bytes]]"
973-
self.buffer = tarfile.TarFile.open(
970+
# error: Incompatible types in assignment (expression has type "TarFile",
971+
# base class "_BufferedWriter" defined the type as "BytesIO")
972+
self.buffer: tarfile.TarFile = tarfile.TarFile.open( # type: ignore[assignment]
974973
name=name,
975974
mode=self.extend_mode(mode),
976-
fileobj=fileobj, # type: ignore[arg-type]
975+
fileobj=fileobj,
977976
**kwargs,
978977
)
979978

@@ -1023,10 +1022,9 @@ def __init__(
10231022
self.archive_name = archive_name
10241023

10251024
kwargs.setdefault("compression", zipfile.ZIP_DEFLATED)
1026-
# error: No overload variant of "ZipFile" matches argument types "str |
1027-
# PathLike[str] | ReadBuffer[bytes] | WriteBuffer[bytes]", "str", "dict[str,
1028-
# Any]"
1029-
self.buffer = zipfile.ZipFile( # type: ignore[call-overload]
1025+
# error: Incompatible types in assignment (expression has type "ZipFile",
1026+
# base class "_BufferedWriter" defined the type as "BytesIO")
1027+
self.buffer: zipfile.ZipFile = zipfile.ZipFile( # type: ignore[assignment]
10301028
file, mode, **kwargs
10311029
)
10321030

pandas/tests/arrays/categorical/test_warnings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ async def test_tab_complete_warning(self, ip):
1717

1818
# GH 31324 newer jedi version raises Deprecation warning;
1919
# appears resolved 2021-02-02
20-
with tm.assert_produces_warning(None):
20+
with tm.assert_produces_warning(None, raise_on_extra_warnings=False):
2121
with provisionalcompleter("ignore"):
2222
list(ip.Completer.completions("c.", 1))

pandas/tests/frame/test_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ async def test_tab_complete_warning(self, ip, frame_or_series):
303303

304304
# GH 31324 newer jedi version raises Deprecation warning;
305305
# appears resolved 2021-02-02
306-
with tm.assert_produces_warning(None):
306+
with tm.assert_produces_warning(None, raise_on_extra_warnings=False):
307307
with provisionalcompleter("ignore"):
308308
list(ip.Completer.completions("obj.", 1))
309309

pandas/tests/indexes/test_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ async def test_tab_complete_warning(self, ip):
12261226

12271227
# GH 31324 newer jedi version raises Deprecation warning;
12281228
# appears resolved 2021-02-02
1229-
with tm.assert_produces_warning(None):
1229+
with tm.assert_produces_warning(None, raise_on_extra_warnings=False):
12301230
with provisionalcompleter("ignore"):
12311231
list(ip.Completer.completions("idx.", 4))
12321232

pandas/tests/io/formats/test_format.py

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ def assert_filepath_or_buffer_equals(
9595
"""
9696
Assertion helper for checking filepath_or_buffer.
9797
"""
98+
if encoding is None:
99+
encoding = "utf-8"
98100

99101
def _assert_filepath_or_buffer_equals(expected):
100102
if filepath_or_buffer_id == "string":

pandas/tests/io/parser/common/test_common_basic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ def test_read_seek(all_parsers):
856856
prefix = "### DATA\n"
857857
content = "nkey,value\ntables,rectangular\n"
858858
with tm.ensure_clean() as path:
859-
Path(path).write_text(prefix + content)
859+
Path(path).write_text(prefix + content, encoding="utf-8")
860860
with open(path, encoding="utf-8") as file:
861861
file.readline()
862862
actual = parser.read_csv(file)

pandas/tests/io/parser/test_encoding.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def test_not_readable(all_parsers, mode):
310310
content = b"abcd"
311311
if "t" in mode:
312312
content = "abcd"
313-
with tempfile.SpooledTemporaryFile(mode=mode) as handle:
313+
with tempfile.SpooledTemporaryFile(mode=mode, encoding="utf-8") as handle:
314314
handle.write(content)
315315
handle.seek(0)
316316
df = parser.read_csv(handle)

pandas/tests/io/parser/test_read_fwf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ def test_binary_mode():
702702
[["bba", "bab", "b a"]], columns=["aaa", "aaa.1", "aaa.2"], index=[0]
703703
)
704704
with tm.ensure_clean() as path:
705-
Path(path).write_text(data)
705+
Path(path).write_text(data, encoding="utf-8")
706706
with open(path, "rb") as file:
707707
df = read_fwf(file)
708708
file.seek(0)

pandas/tests/io/parser/test_unsupported.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def test_close_file_handle_on_invalid_usecols(all_parsers):
182182
pytest.skip("GH#45547 causing timeouts on windows/mac builds 2022-01-22")
183183

184184
with tm.ensure_clean("test.csv") as fname:
185-
Path(fname).write_text("col1,col2\na,b\n1,2")
185+
Path(fname).write_text("col1,col2\na,b\n1,2", encoding="utf-8")
186186
with tm.assert_produces_warning(False):
187187
with pytest.raises(error, match="col3"):
188188
parser.read_csv(fname, usecols=["col1", "col2", "col3"])

pandas/tests/io/test_sql.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def create_and_load_iris_sqlite3(conn: sqlite3.Connection, iris_file: Path):
140140
"Name" TEXT
141141
)"""
142142
cur.execute(stmt)
143-
with iris_file.open(newline=None) as csvfile:
143+
with iris_file.open(newline=None, encoding="utf-8") as csvfile:
144144
reader = csv.reader(csvfile)
145145
next(reader)
146146
stmt = "INSERT INTO iris VALUES(?, ?, ?, ?, ?)"
@@ -153,7 +153,7 @@ def create_and_load_iris(conn, iris_file: Path, dialect: str):
153153

154154
iris = iris_table_metadata(dialect)
155155

156-
with iris_file.open(newline=None) as csvfile:
156+
with iris_file.open(newline=None, encoding="utf-8") as csvfile:
157157
reader = csv.reader(csvfile)
158158
header = next(reader)
159159
params = [dict(zip(header, row)) for row in reader]

pandas/tests/resample/test_resampler_grouper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async def test_tab_complete_ipython6_warning(ip):
4141

4242
# GH 31324 newer jedi version raises Deprecation warning;
4343
# appears resolved 2021-02-02
44-
with tm.assert_produces_warning(None):
44+
with tm.assert_produces_warning(None, raise_on_extra_warnings=False):
4545
with provisionalcompleter("ignore"):
4646
list(ip.Completer.completions("rs.", 1))
4747

0 commit comments

Comments
 (0)