Run pyupgrade to clean up Python 2 syntaxes

Update all .py source files by
 $ pyupgrade --py3-only $(git ls-files | grep ".py$")
to modernize the code according to Python 3 syntaxes.

Also introduce pre-commit and its pyupgrade hook to avoid merging
additional Python 2 syntaxes.

Change-Id: If182808de2e565028998b3ed45947d267d6e021c
This commit is contained in:
Takashi Kajinami
2025-01-13 13:44:35 +09:00
parent 9c88eca350
commit 24f0feb79c
6 changed files with 43 additions and 12 deletions

30
.pre-commit-config.yaml Normal file
View File

@@ -0,0 +1,30 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
# Replaces or checks mixed line ending
- id: mixed-line-ending
args: ['--fix', 'lf']
exclude: '.*\.(svg)$'
# Forbid files which have a UTF-8 byte-order marker
- id: check-byte-order-marker
# Checks that non-binary executables have a proper shebang
- id: check-executables-have-shebangs
# Check for files that contain merge conflict strings.
- id: check-merge-conflict
# Check for debugger imports and py37+ breakpoint()
# calls in python source
- id: debug-statements
- id: check-yaml
files: .*\.(yaml|yml)$
- repo: https://opendev.org/openstack/hacking
rev: 7.0.0
hooks:
- id: hacking
additional_dependencies: []
- repo: https://github.com/asottile/pyupgrade
rev: v3.18.0
hooks:
- id: pyupgrade
args: [--py3-only]

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

View File

@@ -38,7 +38,7 @@ def str_split(text):
return text.split()
class GoogleShortener(object):
class GoogleShortener:
"""Shortener that uses google shortening service (requires api key).
See: https://developers.google.com/url-shortener/v1/
@@ -58,7 +58,7 @@ class GoogleShortener(object):
def safe_shorten(self, long_url):
try:
return self.shorten(long_url)
except (IOError, ValueError, TypeError):
except (OSError, ValueError, TypeError):
self.log.exception("Failed shortening due to unexpected error, "
" providing back long url.")
return long_url
@@ -78,12 +78,12 @@ class GoogleShortener(object):
headers={'content-type': 'application/json'},
timeout=self.timeout)
except requests.Timeout:
raise IOError("Unable to shorten '%s' url"
raise OSError("Unable to shorten '%s' url"
" due to http request timeout being"
" reached" % (long_url))
else:
if req.status_code != http_client.OK:
raise IOError("Unable to shorten '%s' url due to http"
raise OSError("Unable to shorten '%s' url due to http"
" error '%s' (%s)" % (long_url, req.reason,
req.status_code))
try:
@@ -91,7 +91,7 @@ class GoogleShortener(object):
self.cache[long_url] = small_url
return small_url
except (KeyError, ValueError, TypeError) as e:
raise IOError("Unable to shorten '%s' url due to request"
raise OSError("Unable to shorten '%s' url due to request"
" extraction error: %s" % (long_url, e))
@@ -159,7 +159,7 @@ class OsloBotPlugin(BotPlugin):
if not configuration:
configuration = {}
configuration.update(copy.deepcopy(self.DEF_CONFIG))
super(OsloBotPlugin, self).configure(configuration)
super().configure(configuration)
self.log.debug("Bot configuration: %s", self.config)
self.executor = None
@@ -392,12 +392,12 @@ class OsloBotPlugin(BotPlugin):
return copy.deepcopy(self.DEF_CONFIG)
def deactivate(self):
super(OsloBotPlugin, self).deactivate()
super().deactivate()
if self.executor is not None:
self.executor.shutdown()
def activate(self):
super(OsloBotPlugin, self).activate()
super().activate()
self.executor = futures.ThreadPoolExecutor(
max_workers=self.DEF_FETCH_WORKERS)
try:

View File

@@ -1 +0,0 @@
hacking>=6.1.0,<6.2.0 # Apache-2.0

View File

@@ -29,7 +29,7 @@ def main():
gov_repo = os.path.expanduser(os.path.join(conf.repo_root,
'governance'))
project_input = os.path.join(gov_repo, 'reference/projects.yaml')
with open(project_input, 'r') as f:
with open(project_input) as f:
project = yaml.safe_load(f.read())
repos = []

View File

@@ -13,7 +13,10 @@ commands =
{toxinidir}/tools/list_oslo_projects.py {posargs}
[testenv:pep8]
commands = flake8
deps =
pre-commit
commands =
pre-commit run -a
[testenv:bashate]
deps = bashate