Skip to content

Commit

Permalink
Ran pyupgrade for Python 3.8+ followed by ruff --fix and black
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jul 6, 2023
1 parent 1bcceaa commit 0fd55a4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 33 deletions.
23 changes: 10 additions & 13 deletions path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import functools
import re
import contextlib
import io
import importlib
import itertools
import datetime
Expand Down Expand Up @@ -168,11 +167,11 @@ def _next_class(cls):
# --- Special Python methods.

def __repr__(self):
return '%s(%s)' % (type(self).__name__, super(Path, self).__repr__())
return '{}({})'.format(type(self).__name__, super().__repr__())

# Adding a Path and a string yields a Path.
def __add__(self, more):
return self._next_class(super(Path, self).__add__(more))
return self._next_class(super().__add__(more))

def __radd__(self, other):
return self._next_class(other.__add__(self))
Expand Down Expand Up @@ -296,7 +295,7 @@ def with_suffix(self, suffix):
ValueError: Invalid suffix 'zip'
"""
if not suffix.startswith('.'):
raise ValueError("Invalid suffix {suffix!r}".format(**locals()))
raise ValueError(f"Invalid suffix {suffix!r}")

return self.stripext() + suffix

Expand Down Expand Up @@ -549,8 +548,7 @@ def walk(self, match=None, errors='strict'):
continue

if do_traverse:
for item in child.walk(errors=errors, match=match):
yield item
yield from child.walk(errors=errors, match=match)

def walkdirs(self, *args, **kwargs):
"""Iterator over subdirs, recursively."""
Expand Down Expand Up @@ -623,7 +621,7 @@ def open(self, *args, **kwargs):
Keyword arguments work as in :func:`io.open`. If the file cannot be
opened, an :class:`OSError` is raised.
"""
return io.open(self, *args, **kwargs)
return open(self, *args, **kwargs)

def bytes(self):
"""Open this file, read all bytes, return them as a string."""
Expand All @@ -645,8 +643,7 @@ def chunks(self, size, *args, **kwargs):
This will read the file by chunks of 8192 bytes.
"""
with self.open(*args, **kwargs) as f:
for chunk in iter(lambda: f.read(size) or None, None):
yield chunk
yield from iter(lambda: f.read(size) or None, None)

def write_bytes(self, bytes, append=False):
"""Open this file and write the given bytes to it.
Expand Down Expand Up @@ -1402,7 +1399,7 @@ def in_place(
backup_fn = self + (backup_extension or os.extsep + 'bak')
backup_fn.remove_p()
self.rename(backup_fn)
readable = io.open(
readable = open(
backup_fn,
mode,
buffering=buffering,
Expand All @@ -1424,7 +1421,7 @@ def in_place(
os_mode = os.O_CREAT | os.O_WRONLY | os.O_TRUNC
os_mode |= getattr(os, 'O_BINARY', 0)
fd = os.open(self, os_mode, perm)
writable = io.open(
writable = open(
fd,
"w" + mode.replace('r', ''),
buffering=buffering,
Expand Down Expand Up @@ -1556,7 +1553,7 @@ def get_dir(self, scope, class_):
Return the callable function from appdirs, but with the
result wrapped in self.path_class
"""
prop_name = '{scope}_{class_}_dir'.format(**locals())
prop_name = f'{scope}_{class_}_dir'
value = getattr(self.wrapper, prop_name)
MultiPath = Multi.for_class(self.path_class)
return MultiPath.detect(value)
Expand Down Expand Up @@ -1617,7 +1614,7 @@ def _next_class(cls):

def __new__(cls, *args, **kwargs):
dirname = tempfile.mkdtemp(*args, **kwargs)
return super(TempDir, cls).__new__(cls, dirname)
return super().__new__(cls, dirname)

def __init__(self, *args, **kwargs):
pass
Expand Down
1 change: 0 additions & 1 deletion path/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ from __future__ import annotations
import builtins
import contextlib
import os
import shutil
import sys
from io import (
BufferedRandom,
Expand Down
37 changes: 18 additions & 19 deletions test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
time on files.
"""

import io
import os
import sys
import shutil
Expand Down Expand Up @@ -94,7 +93,7 @@ def test_string_compatibility(self):
"""Test compatibility with ordinary strings."""
x = Path('xyzzy')
assert x == 'xyzzy'
assert x == str('xyzzy')
assert x == 'xyzzy'

# sorting
items = [Path('fhj'), Path('fgh'), 'E', Path('d'), 'A', Path('B'), 'c']
Expand Down Expand Up @@ -316,7 +315,7 @@ def get_command_time(cmd):
args = [sys.executable, '-m', 'timeit', '-n', '1', '-r', '1', '-u', 'usec'] + [
cmd
]
res = subprocess.check_output(args, universal_newlines=True)
res = subprocess.check_output(args, text=True)
dur = re.search(r'(\d+) usec per loop', res).group(1)
return datetime.timedelta(microseconds=int(dur))

Expand Down Expand Up @@ -474,7 +473,7 @@ def test_touch(self, tmpdir):

time.sleep(threshold * 2)
fobj = open(f, 'ab')
fobj.write('some bytes'.encode('utf-8'))
fobj.write(b'some bytes')
fobj.close()

time.sleep(threshold * 2)
Expand Down Expand Up @@ -550,7 +549,7 @@ def test_listing(self, tmpdir):

@pytest.fixture
def bytes_filename(self, tmpdir):
name = r'r\xe9\xf1emi'.encode('latin-1')
name = br'r\xe9\xf1emi'
base = str(tmpdir).encode('ascii')
try:
with open(os.path.join(base, name), 'wb'):
Expand Down Expand Up @@ -739,7 +738,7 @@ def test_unicode(self, tmpdir, encoding):
stripped = [line.replace('\n', '') for line in expectedLines]

# write bytes manually to file
with io.open(p, 'wb') as strm:
with open(p, 'wb') as strm:
strm.write(given.encode(encoding))

# test all 3 path read-fully functions, including
Expand Down Expand Up @@ -913,18 +912,20 @@ def check_link(self):
def test_with_nonexisting_dst_kwargs(self):
self.subdir_a.merge_tree(self.subdir_b, symlinks=True)
assert self.subdir_b.isdir()
expected = set(
(self.subdir_b / self.test_file.name, self.subdir_b / self.test_link.name)
)
expected = {
self.subdir_b / self.test_file.name,
self.subdir_b / self.test_link.name,
}
assert set(self.subdir_b.listdir()) == expected
self.check_link()

def test_with_nonexisting_dst_args(self):
self.subdir_a.merge_tree(self.subdir_b, True)
assert self.subdir_b.isdir()
expected = set(
(self.subdir_b / self.test_file.name, self.subdir_b / self.test_link.name)
)
expected = {
self.subdir_b / self.test_file.name,
self.subdir_b / self.test_link.name,
}
assert set(self.subdir_b.listdir()) == expected
self.check_link()

Expand All @@ -941,13 +942,11 @@ def test_with_existing_dst(self):
self.subdir_a.merge_tree(self.subdir_b, True)

assert self.subdir_b.isdir()
expected = set(
(
self.subdir_b / self.test_file.name,
self.subdir_b / self.test_link.name,
self.subdir_b / test_new.name,
)
)
expected = {
self.subdir_b / self.test_file.name,
self.subdir_b / self.test_link.name,
self.subdir_b / test_new.name,
}
assert set(self.subdir_b.listdir()) == expected
self.check_link()
assert len(Path(self.subdir_b / self.test_file.name).bytes()) == 5000
Expand Down

0 comments on commit 0fd55a4

Please sign in to comment.