Skip to content

Commit

Permalink
Removed deprecated behaviors.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jul 27, 2024
1 parent fb54581 commit 7e23a1d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 180 deletions.
1 change: 1 addition & 0 deletions newsfragments/+3aebd9d5.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Removed deprecated support for passing ``bytes`` to ``write_text`` and ``write_lines(linesep=)`` parameter.
1 change: 1 addition & 0 deletions newsfragments/+c9d42aa7.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Removed deprecated methods ``getcwd``, ``abspath``, ``ext``, ``listdir``, ``isdir``, ``isfile``, and ``text``.
142 changes: 15 additions & 127 deletions path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('~'))
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 1 addition & 10 deletions path/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand All @@ -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: ...
Expand All @@ -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(
Expand Down Expand Up @@ -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 = ...,
Expand All @@ -144,17 +137,15 @@ class Path(str):
lines: list[str],
encoding: str | None = ...,
errors: str = ...,
linesep: str | None = ...,
*,
append: bool = ...,
) -> None: ...
def read_md5(self) -> builtins.bytes: ...
def read_hash(self, hash_name: str) -> builtins.bytes: ...
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: ...
Expand Down
46 changes: 3 additions & 43 deletions test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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"
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 7e23a1d

Please sign in to comment.