Skip to content

Commit

Permalink
Enforce pyupgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Jing Wang committed Jul 8, 2023
1 parent 81a15b9 commit 26f7ddc
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 21 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
strategy:
matrix:
# https://devguide.python.org/versions/#supported-versions
# also see pyupgrade in test_tools.py
python-version: [3.8, 3.9, '3.10', 3.11]
env:
- VERSION=2.10.2 MD5=003704f56d7d5dc72e7a04fe8147ff10
Expand Down
1 change: 1 addition & 0 deletions dev_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mypy
pytest
pytest-cov
pytest-random
pyupgrade
sphinx
types-requests
types-simplejson
2 changes: 1 addition & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

project = "PyHDFS"
author = "Jing Wang"
copyright = "{}, {}".format(datetime.datetime.now().year, author)
copyright = f"{datetime.datetime.now().year}, {author}"

extensions = [
"sphinx.ext.autodoc",
Expand Down
7 changes: 4 additions & 3 deletions generate_class_from_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ def main() -> None:
print("class {}(_BoilerplateClass):".format(js["name"]))
print(' """')
for k, v in js["properties"][name]["properties"].items():
print(" :param {}: {}".format(k, v.get("description", "")))
print(" :type {}: {}".format(k, to_py_type(v)))
description = v.get("description", "")
print(f" :param {k}: {description}")
print(f" :type {k}: {to_py_type(v)}")
print(' """')
print()
for k, v in js["properties"][name]["properties"].items():
print(" {}: {}".format(k, to_py_type(v)))
print(f" {k}: {to_py_type(v)}")


if __name__ == "__main__":
Expand Down
27 changes: 12 additions & 15 deletions pyhdfs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def __init__(self, **kwargs: object) -> None:
self.__dict__ = self

def __repr__(self) -> str:
kvs = ["{}={!r}".format(k, v) for k, v in self.items()]
kvs = [f"{k}={v!r}" for k, v in self.items()]
return "{}({})".format(self.__class__.__name__, ", ".join(kvs))

def __eq__(self, other: object) -> bool:
Expand Down Expand Up @@ -286,7 +286,7 @@ class FileStatus(_BoilerplateClass):
childrenNum: int


class HdfsClient(object):
class HdfsClient:
"""HDFS client backed by WebHDFS.
All functions take arbitrary query parameters to pass to WebHDFS, in addition to any documented
Expand Down Expand Up @@ -334,9 +334,9 @@ def __init__(
) -> None:
"""Create a new HDFS client"""
if max_tries < 1:
raise ValueError("Invalid max_tries: {}".format(max_tries))
raise ValueError(f"Invalid max_tries: {max_tries}")
if retry_delay < 0:
raise ValueError("Invalid retry_delay: {}".format(retry_delay))
raise ValueError(f"Invalid retry_delay: {retry_delay}")
self.randomize_hosts = randomize_hosts
self.hosts = self._parse_hosts(hosts)
if not self.hosts:
Expand All @@ -357,13 +357,13 @@ def __init__(
self._requests_kwargs = requests_kwargs or {}
for k in ("method", "url", "data", "timeout", "stream", "params"):
if k in self._requests_kwargs:
raise ValueError("Cannot override requests argument {}".format(k))
raise ValueError(f"Cannot override requests argument {k}")

def _parse_hosts(self, hosts: Union[str, Iterable[str]]) -> List[str]:
host_list = re.split(r",|;", hosts) if isinstance(hosts, str) else list(hosts)
for i, host in enumerate(host_list):
if ":" not in host:
host_list[i] = "{:s}:{:d}".format(host, DEFAULT_PORT)
host_list[i] = f"{host:s}:{DEFAULT_PORT:d}"
if self.randomize_hosts:
random.shuffle(host_list)
return host_list
Expand Down Expand Up @@ -393,7 +393,7 @@ def _request(
"""
hosts = self.hosts
if not posixpath.isabs(path):
raise ValueError("Path must be absolute, was given {}".format(path))
raise ValueError(f"Path must be absolute, was given {path}")
_transform_user_name_key(kwargs)
kwargs.setdefault("user.name", self.user_name)

Expand Down Expand Up @@ -815,7 +815,7 @@ def get_xattrs(
assert v.startswith("0s")
result[k] = base64.b64decode(v[2:])
else:
warnings.warn("Unexpected encoding {}".format(encoding))
warnings.warn(f"Unexpected encoding {encoding}")
result[k] = v
return result

Expand Down Expand Up @@ -887,7 +887,7 @@ def listdir(self, path: str, **kwargs: _PossibleArgumentTypes) -> List[str]:
and statuses[0].pathSuffix == ""
and statuses[0].type == "FILE"
):
raise NotADirectoryError("Not a directory: {!r}".format(path))
raise NotADirectoryError(f"Not a directory: {path!r}")
return [f.pathSuffix for f in statuses]

def exists(self, path: str, **kwargs: _PossibleArgumentTypes) -> bool:
Expand Down Expand Up @@ -920,14 +920,13 @@ def walk(
elif f.type == "FILE":
filenames.append(f.pathSuffix)
else: # pragma: no cover
raise AssertionError("Unexpected type {}".format(f.type))
raise AssertionError(f"Unexpected type {f.type}")

if topdown:
yield top, dirnames, filenames
for name in dirnames:
new_path = posixpath.join(top, name)
for x in self.walk(new_path, topdown, onerror, **kwargs):
yield x
yield from self.walk(new_path, topdown, onerror, **kwargs)
if not topdown:
yield top, dirnames, filenames

Expand Down Expand Up @@ -984,9 +983,7 @@ def _json(response: requests.Response) -> Dict[str, Any]:
assert isinstance(js, dict), type(js)
return js
except simplejson.scanner.JSONDecodeError:
raise HdfsException(
"Expected JSON. Is WebHDFS enabled? Got {!r}".format(response.text)
)
raise HdfsException(f"Expected JSON. Is WebHDFS enabled? Got {response.text!r}")


def _check_response(
Expand Down
3 changes: 1 addition & 2 deletions test_pyhdfs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: utf-8
import logging
import os
import posixpath
Expand Down Expand Up @@ -282,7 +281,7 @@ def mock_request(method: str, url: str, **kwargs: object) -> requests.Response:
active_count[0] += 1
return original_request(method, url, **kwargs) # type: ignore[arg-type]
else:
self.fail("Unexpected url {}".format(url)) # pragma: no cover
self.fail(f"Unexpected url {url}") # pragma: no cover

with mock.patch("requests.api.request", mock_request):
client.get_file_status("/")
Expand Down
8 changes: 8 additions & 0 deletions test_tools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import subprocess
from pathlib import Path


def test_black() -> None:
Expand All @@ -18,3 +19,10 @@ def test_isort() -> None:

def test_mypy() -> None:
subprocess.check_call(["mypy", os.path.dirname(__file__)])


def test_pyupgrade() -> None:
# also see python-version in .github/workflows/ci.yml
subprocess.check_call(
["pyupgrade", "--py38-plus"] + list(Path(__file__).parent.glob("**/*.py"))
)

0 comments on commit 26f7ddc

Please sign in to comment.