From 7e23a1d7a5fc6412926e6e72be00e5479e86fec4 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 27 Jul 2024 06:30:29 -0400 Subject: [PATCH] Removed deprecated behaviors. --- newsfragments/+3aebd9d5.removal.rst | 1 + newsfragments/+c9d42aa7.removal.rst | 1 + path/__init__.py | 142 +++------------------------- path/__init__.pyi | 11 +-- test_path.py | 46 +-------- 5 files changed, 21 insertions(+), 180 deletions(-) create mode 100644 newsfragments/+3aebd9d5.removal.rst create mode 100644 newsfragments/+c9d42aa7.removal.rst diff --git a/newsfragments/+3aebd9d5.removal.rst b/newsfragments/+3aebd9d5.removal.rst new file mode 100644 index 0000000..2349ab5 --- /dev/null +++ b/newsfragments/+3aebd9d5.removal.rst @@ -0,0 +1 @@ +Removed deprecated support for passing ``bytes`` to ``write_text`` and ``write_lines(linesep=)`` parameter. diff --git a/newsfragments/+c9d42aa7.removal.rst b/newsfragments/+c9d42aa7.removal.rst new file mode 100644 index 0000000..e1a684a --- /dev/null +++ b/newsfragments/+c9d42aa7.removal.rst @@ -0,0 +1 @@ +Removed deprecated methods ``getcwd``, ``abspath``, ``ext``, ``listdir``, ``isdir``, ``isfile``, and ``text``. diff --git a/path/__init__.py b/path/__init__.py index 55532ed..fc002f8 100644 --- a/path/__init__.py +++ b/path/__init__.py @@ -231,15 +231,6 @@ def cwd(cls): """ return cls(os.getcwd()) - @classmethod - def getcwd(cls): - warnings.warn( - ".getcwd is deprecated; use cwd", - DeprecationWarning, - stacklevel=2, - ) - return cls.cwd() - @classmethod def home(cls) -> Path: return cls(os.path.expanduser('~')) @@ -251,14 +242,6 @@ def absolute(self): """.. seealso:: :func:`os.path.abspath`""" return self._next_class(self.module.abspath(self)) - def abspath(self): - warnings.warn( - ".abspath is deprecated; use absolute", - DeprecationWarning, - stacklevel=2, - ) - return self.absolute() - def normcase(self): """.. seealso:: :func:`os.path.normcase`""" return self._next_class(self.module.normcase(self)) @@ -320,15 +303,6 @@ def suffix(self): f, suffix = self.module.splitext(self) return suffix - @property - def ext(self): # pragma: no cover - warnings.warn( - ".ext is deprecated; use suffix", - DeprecationWarning, - stacklevel=2, - ) - return self.suffix - def with_suffix(self, suffix): """Return a new path with the file suffix changed (or added, if none) @@ -545,14 +519,6 @@ def iterdir(self, match=None): match = matchers.load(match) return filter(match, (self / child for child in os.listdir(self))) - def listdir(self, match=None): - warnings.warn( - ".listdir is deprecated; use iterdir", - DeprecationWarning, - stacklevel=2, - ) - return list(self.iterdir(match=match)) - def dirs(self, *args, **kwargs): """List of this directory's subdirectories. @@ -774,41 +740,14 @@ def read_bytes(self): with self.open(mode='rb') as f: return f.read() - def text(self, encoding=None, errors='strict'): - r"""Legacy function to read text. - - Converts all newline sequences to ``\n``. - """ - warnings.warn( - ".text is deprecated; use read_text", - DeprecationWarning, - stacklevel=2, - ) - return U_NEWLINE.sub('\n', self.read_text(encoding, errors)) - - @overload def write_text( self, text: str, - encoding: str | None = ..., - errors: str = ..., - linesep: str | None = ..., - append: bool = ..., - ) -> None: ... - - @overload - def write_text( - self, - text: builtins.bytes, - encoding: None = ..., - errors: str = ..., - linesep: str | None = ..., - append: bool = ..., - ) -> None: ... - - def write_text( - self, text, encoding=None, errors='strict', linesep=os.linesep, append=False - ): + encoding: str | None = None, + errors: str = 'strict', + linesep: str | None = os.linesep, + append: bool = False, + ) -> None: r"""Write the given text to this file. The default behavior is to overwrite any existing file; @@ -820,7 +759,7 @@ def write_text( Parameters: - `text` - str/bytes - The text to be written. + `text` - str - The text to be written. `encoding` - str - The text encoding used. @@ -855,29 +794,14 @@ def write_text( --- Unicode - If `text` isn't Unicode, then apart from newline handling, the - bytes are written verbatim to the file. The `encoding` and - `errors` arguments are not used and must be omitted. - - If `text` is Unicode, it is first converted to :func:`bytes` using the + `text` is written using the specified `encoding` (or the default encoding if `encoding` isn't specified). The `errors` argument applies only to this conversion. """ - if isinstance(text, str): - if linesep is not None: - text = U_NEWLINE.sub(linesep, text) - bytes = text.encode(encoding or sys.getdefaultencoding(), errors) - else: - warnings.warn( - "Writing bytes in write_text is deprecated", - DeprecationWarning, - stacklevel=1, - ) - assert encoding is None - if linesep is not None: - text = B_NEWLINE.sub(linesep.encode(), text) - bytes = text + if linesep is not None: + text = U_NEWLINE.sub(linesep, text) + bytes = text.encode(encoding or sys.getdefaultencoding(), errors) self.write_bytes(bytes, append=append) def lines(self, encoding=None, errors=None, retain=True): @@ -894,8 +818,6 @@ def lines(self, encoding=None, errors=None, retain=True): but translate all newline characters to ``\n``. If ``False``, newline characters are omitted. - - .. seealso:: :meth:`text` """ text = U_NEWLINE.sub('\n', self.read_text(encoding, errors)) return text.splitlines(retain) @@ -905,15 +827,14 @@ def write_lines( lines, encoding=None, errors='strict', - linesep=_default_linesep, + *, append=False, ): r"""Write the given lines of text to this file. By default this overwrites any existing file at this path. - This puts a platform-specific newline sequence on every line. - See `linesep` below. + Puts a platform-specific newline sequence on every line. `lines` - A list of strings. @@ -923,33 +844,16 @@ def write_lines( `errors` - How to handle errors in Unicode encoding. This also applies only to Unicode strings. - linesep - (deprecated) The desired line-ending. This line-ending is - applied to every line. If a line already has any - standard line ending (``'\r'``, ``'\n'``, ``'\r\n'``, - ``u'\x85'``, ``u'\r\x85'``, ``u'\u2028'``), that will - be stripped off and this will be used instead. The - default is os.linesep, which is platform-dependent - (``'\r\n'`` on Windows, ``'\n'`` on Unix, etc.). - Specify ``None`` to write the lines as-is, like - ``.writelines`` on a file object. - Use the keyword argument ``append=True`` to append lines to the file. The default is to overwrite the file. """ mode = 'a' if append else 'w' with self.open(mode, encoding=encoding, errors=errors, newline='') as f: - f.writelines(self._replace_linesep(lines, linesep)) + f.writelines(self._replace_linesep(lines)) @staticmethod - def _replace_linesep(lines, linesep): - if linesep != _default_linesep: - warnings.warn("linesep is deprecated", DeprecationWarning, stacklevel=3) - else: - linesep = os.linesep - if linesep is None: - return lines - - return (line + linesep for line in _strip_newlines(lines)) + def _replace_linesep(lines): + return (line + os.linesep for line in _strip_newlines(lines)) def read_md5(self): """Calculate the md5 hash for this file. @@ -1009,26 +913,10 @@ def exists(self): """.. seealso:: :func:`os.path.exists`""" return self.module.exists(self) - def isdir(self): # pragma: no cover - warnings.warn( - "isdir is deprecated; use is_dir", - DeprecationWarning, - stacklevel=2, - ) - return self.is_dir() - def is_dir(self): """.. seealso:: :func:`os.path.isdir`""" return self.module.isdir(self) - def isfile(self): # pragma: no cover - warnings.warn( - "isfile is deprecated; use is_file", - DeprecationWarning, - stacklevel=2, - ) - return self.is_file() - def is_file(self): """.. seealso:: :func:`os.path.isfile`""" return self.module.isfile(self) diff --git a/path/__init__.pyi b/path/__init__.pyi index 1cf626c..2dba216 100644 --- a/path/__init__.pyi +++ b/path/__init__.pyi @@ -59,10 +59,7 @@ class Path(str): exc_val: BaseException | None, exc_tb: TracebackType | None, ) -> None: ... - @classmethod - def getcwd(cls: type[Self]) -> Self: ... def absolute(self: Self) -> Self: ... - def abspath(self: Self) -> Self: ... def normcase(self: Self) -> Self: ... def normpath(self: Self) -> Self: ... def realpath(self: Self) -> Self: ... @@ -76,8 +73,6 @@ class Path(str): def with_stem(self: Self, stem: str) -> Self: ... @property def suffix(self: Self) -> Self: ... - @property - def ext(self) -> str: ... def with_suffix(self: Self, suffix: str) -> Self: ... @property def drive(self: Self) -> Self: ... @@ -101,7 +96,6 @@ class Path(str): # --- Listing, searching, walking, and matching def iterdir(self: Self, match: _Match = ...) -> Iterator[Self]: ... - def listdir(self: Self, match: _Match = ...) -> list[Self]: ... def dirs(self: Self, match: _Match = ...) -> list[Self]: ... def files(self: Self, match: _Match = ...) -> list[Self]: ... def walk( @@ -132,7 +126,6 @@ class Path(str): self, encoding: str | None = ..., errors: str | None = ... ) -> str: ... def read_bytes(self) -> builtins.bytes: ... - def text(self, encoding: str | None = ..., errors: str = ...) -> str: ... def lines( self, encoding: str | None = ..., @@ -144,7 +137,7 @@ class Path(str): lines: list[str], encoding: str | None = ..., errors: str = ..., - linesep: str | None = ..., + *, append: bool = ..., ) -> None: ... def read_md5(self) -> builtins.bytes: ... @@ -152,9 +145,7 @@ class Path(str): def read_hexhash(self, hash_name: str) -> str: ... def isabs(self) -> bool: ... def exists(self) -> bool: ... - def isdir(self) -> bool: ... def is_dir(self) -> bool: ... - def isfile(self) -> bool: ... def is_file(self) -> bool: ... def islink(self) -> bool: ... def ismount(self) -> bool: ... diff --git a/test_path.py b/test_path.py index f1e92cc..724dbeb 100644 --- a/test_path.py +++ b/test_path.py @@ -318,11 +318,6 @@ def test_read_write(self, tmpdir): assert file.read_text(encoding='utf-8') == 'hello world' assert file.read_bytes() == b'hello world' - @pytest.mark.filterwarnings('ignore:Writing bytes in write_text') - def test_write_text_bytes(self, tmpdir): - file = path.Path(tmpdir) / 'filename' - file.write_text(b'hello world') - class TestPerformance: @staticmethod @@ -767,11 +762,9 @@ def test_unicode(self, tmpdir, encoding): with open(p, 'wb') as strm: strm.write(given.encode(encoding)) - # test all 3 path read-fully functions, including + # test read-fully functions, including # path.lines() in unicode mode. - assert p.bytes() == given.encode(encoding) - with pytest.deprecated_call(): - assert p.text(encoding) == clean + assert p.read_bytes() == given.encode(encoding) assert p.lines(encoding) == expectedLines assert p.lines(encoding, retain=False) == stripped @@ -793,8 +786,7 @@ def test_unicode(self, tmpdir, encoding): expectedLinesNoHanging = expectedLines[:] expectedLinesNoHanging[-1] += '\n' assert p.bytes() == expectedBytes - with pytest.deprecated_call(): - assert p.text(encoding) == 2 * cleanNoHanging + assert p.read_text(encoding) == 2 * cleanNoHanging assert p.lines(encoding) == 2 * expectedLinesNoHanging assert p.lines(encoding, retain=False) == 2 * stripped @@ -813,34 +805,6 @@ def test_unicode(self, tmpdir, encoding): # Check the result. assert p.bytes() == expectedBytes - # Same test, using newline sequences that are different - # from the platform default. - def testLinesep(eol): - p.write_lines(givenLines, encoding, linesep=eol) - p.write_lines(givenLines, encoding, linesep=eol, append=True) - expected = 2 * cleanNoHanging.replace('\n', eol).encode(encoding) - assert p.bytes() == expected - - with pytest.deprecated_call(): - testLinesep('\n') - testLinesep('\r') - testLinesep('\r\n') - testLinesep('\x0d\x85') - - # Again, but with linesep=None. - with pytest.deprecated_call(): - p.write_lines(givenLines, encoding, linesep=None) - p.write_lines(givenLines, encoding, linesep=None, append=True) - # Check the result. - expectedBytes = 2 * given.encode(encoding) - assert p.bytes() == expectedBytes - with pytest.deprecated_call(): - assert p.text(encoding) == 2 * clean - expectedResultLines = expectedLines[:] - expectedResultLines[-1] += expectedLines[0] - expectedResultLines += expectedLines[1:] - assert p.lines(encoding) == expectedResultLines - def test_chunks(self, tmpdir): p = (TempDir() / 'test.txt').touch() txt = "0123456789" @@ -1005,10 +969,6 @@ def test_nested(self): self.subdir_a.merge_tree(self.subdir_b) assert self.subdir_b.joinpath('subsub').is_dir() - def test_listdir(self): - with pytest.deprecated_call(): - Path().listdir() - class TestChdir: def test_chdir_or_cd(self, tmpdir):