Skip to content

Commit de089e8

Browse files
committed
Emit warning if a string is passed to PathPlus.write_lines
1 parent da61843 commit de089e8

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

domdf_python_tools/paths.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import sys
5757
import tempfile
5858
import urllib.parse
59+
import warnings
5960
from collections import defaultdict, deque
6061
from operator import methodcaller
6162
from typing import (
@@ -532,6 +533,12 @@ def write_lines(
532533
.. versionchanged:: 2.4.0 Added the ``trailing_whitespace`` option.
533534
"""
534535

536+
if isinstance(data, str):
537+
warnings.warn(
538+
"Passing a string to PathPlus.write_lines writes each character to its own line.\n"
539+
"That probably isn't what you intended."
540+
)
541+
535542
if trailing_whitespace:
536543
data = list(data)
537544
if data[-1].strip():

tests/test_paths.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,11 @@ def test_write_lines(tmp_pathplus):
521521
content = tmp_file.read_text()
522522
assert content == "this\nis\na\nlist\nof\nwords\nto\nwrite\nto\nthe\nfile\n"
523523

524+
with pytest.warns(UserWarning, match="Passing a string to PathPlus.write_lines writes each character to its own line."):
525+
tmp_file.write_lines("abcdefg")
526+
527+
assert tmp_file.read_text() == "a\nb\nc\nd\ne\nf\ng\n"
528+
524529

525530
def test_write_lines_trailing_whitespace(tmp_pathplus: PathPlus):
526531
tmp_file = tmp_pathplus / "test.txt"

0 commit comments

Comments
 (0)