From e6ae9fa831c1da9836a0a942e633c9e8b29fbbd9 Mon Sep 17 00:00:00 2001 From: Peter Bull Date: Thu, 14 Sep 2023 09:56:48 -0700 Subject: [PATCH] Update write_text to include newline variable (#362) (#363) * Update write_text to include newline variable * Remove old docstring link * Update cloudpathlib/cloudpath.py with black correction * Remove use of io module * update HISTORY.md Co-authored-by: Matthew Price --- HISTORY.md | 1 + cloudpathlib/cloudpath.py | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 9ec72101..25aee0e6 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,6 +3,7 @@ ## UNRELEASED - Add "CloudPath" as return type on `__init__` for mypy issues. ([Issue #179](https://github.com/drivendataorg/cloudpathlib/issues/179), [PR #342](https://github.com/drivendataorg/cloudpathlib/pull/342)) - Add `with_stem` to all path types when python version supports it (>=3.9). ([Issue #287](https://github.com/drivendataorg/cloudpathlib/issues/287), [PR #290](https://github.com/drivendataorg/cloudpathlib/pull/290), thanks to [@Gilthans](https://github.com/Gilthans)) + - Add `newline` parameter to the `write_text` method to align to `pathlib` functionality as of Python 3.10. [PR #362]https://github.com/drivendataorg/cloudpathlib/pull/362), thanks to [@pricemg](https://github.com/pricemg). ## v0.15.1 (2023-07-12) diff --git a/cloudpathlib/cloudpath.py b/cloudpathlib/cloudpath.py index 401f9775..b2241b61 100644 --- a/cloudpathlib/cloudpath.py +++ b/cloudpathlib/cloudpath.py @@ -626,15 +626,17 @@ def write_text( data: str, encoding: Optional[str] = None, errors: Optional[str] = None, + newline: Optional[str] = None, ) -> int: """Open the file in text mode, write to it, and close the file. NOTE: vendored from pathlib since we override open - https://github.com/python/cpython/blob/3.8/Lib/pathlib.py#L1244-L1252 + https://github.com/python/cpython/blob/3.10/Lib/pathlib.py#L1146-L1155 """ if not isinstance(data, str): raise TypeError("data must be str, not %s" % data.__class__.__name__) - with self.open(mode="w", encoding=encoding, errors=errors) as f: + + with self.open(mode="w", encoding=encoding, errors=errors, newline=newline) as f: return f.write(data) def read_bytes(self) -> bytes: