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 add the pyupgrade hook to pre-commit to avoid merging additional
Python 2 syntaxes.

Change-Id: I8df72bc1f372b2ec5663a4662325a5ba353f65f0
This commit is contained in:
Takashi Kajinami 2024-10-19 23:18:10 +09:00
parent 0026f6c515
commit f58c67d40f
6 changed files with 19 additions and 17 deletions

View File

@ -1,6 +1,6 @@
repos: repos:
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0 rev: v5.0.0
hooks: hooks:
- id: trailing-whitespace - id: trailing-whitespace
# Replaces or checks mixed line ending # Replaces or checks mixed line ending
@ -19,7 +19,7 @@ repos:
- id: check-yaml - id: check-yaml
files: .*\.(yaml|yml)$ files: .*\.(yaml|yml)$
- repo: https://github.com/PyCQA/bandit - repo: https://github.com/PyCQA/bandit
rev: 1.7.5 rev: 1.7.10
hooks: hooks:
- id: bandit - id: bandit
args: ['-x', 'tests'] args: ['-x', 'tests']
@ -34,7 +34,7 @@ repos:
files: '^.*\.py$' files: '^.*\.py$'
exclude: '^(doc|releasenotes|tools)/.*$' exclude: '^(doc|releasenotes|tools)/.*$'
- repo: https://github.com/pre-commit/mirrors-mypy - repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.4.1 rev: v1.12.1
hooks: hooks:
- id: mypy - id: mypy
exclude: | exclude: |
@ -43,3 +43,8 @@ repos:
| doc/.* | doc/.*
| releasenotes/.* | releasenotes/.*
) )
- 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 -*-
# Copyright (C) 2020 Red Hat, Inc. # Copyright (C) 2020 Red Hat, Inc.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");

View File

@ -170,7 +170,7 @@ _renamed_kwarg = functools.partial(renames.renamed_kwarg,
replace=True) replace=True)
class RequestContext(object): class RequestContext:
"""Helper class to represent useful information about a request context. """Helper class to represent useful information about a request context.

View File

@ -25,7 +25,7 @@ class ClearRequestContext(fixtures.Fixture):
""" """
def setUp(self) -> None: def setUp(self) -> None:
super(ClearRequestContext, self).setUp() super().setUp()
# we need to clear both when we start, and when we finish, # we need to clear both when we start, and when we finish,
# because there might be other tests running that don't handle # because there might be other tests running that don't handle
# this correctly. # this correctly.

View File

@ -1,4 +1,3 @@
# -*- encoding: utf-8 -*-
# Copyright 2011 OpenStack Foundation. # Copyright 2011 OpenStack Foundation.
# All Rights Reserved. # All Rights Reserved.
# #
@ -32,12 +31,12 @@ def generate_id(name):
class WarningsFixture(fixtures.Fixture): class WarningsFixture(fixtures.Fixture):
def __init__(self, action="always", category=DeprecationWarning): def __init__(self, action="always", category=DeprecationWarning):
super(WarningsFixture, self).__init__() super().__init__()
self.action = action self.action = action
self.category = category self.category = category
def setUp(self): def setUp(self):
super(WarningsFixture, self).setUp() super().setUp()
self._w = warnings.catch_warnings(record=True) self._w = warnings.catch_warnings(record=True)
self.log = self._w.__enter__() self.log = self._w.__enter__()
self.addCleanup(self._w.__exit__) self.addCleanup(self._w.__exit__)
@ -50,7 +49,7 @@ class WarningsFixture(fixtures.Fixture):
return self.log[item] return self.log[item]
class Object(object): class Object:
pass pass
@ -63,11 +62,11 @@ class TestContext(context.RequestContext):
FROM_DICT_EXTRA_KEYS = ['auth_token_info'] FROM_DICT_EXTRA_KEYS = ['auth_token_info']
def __init__(self, auth_token_info=None, **kwargs): def __init__(self, auth_token_info=None, **kwargs):
super(TestContext, self).__init__(**kwargs) super().__init__(**kwargs)
self.auth_token_info = auth_token_info self.auth_token_info = auth_token_info
def to_dict(self): def to_dict(self):
d = super(TestContext, self).to_dict() d = super().to_dict()
d['auth_token_info'] = self.auth_token_info d['auth_token_info'] = self.auth_token_info
return d return d
@ -75,7 +74,7 @@ class TestContext(context.RequestContext):
class ContextTest(test_base.BaseTestCase): class ContextTest(test_base.BaseTestCase):
def setUp(self): def setUp(self):
super(ContextTest, self).setUp() super().setUp()
self.warnings = self.useFixture(WarningsFixture()) self.warnings = self.useFixture(WarningsFixture())
self.useFixture(fixture.ClearRequestContext()) self.useFixture(fixture.ClearRequestContext())
@ -404,7 +403,7 @@ class ContextTest(test_base.BaseTestCase):
def test_values(self): def test_values(self):
auth_token = "token1" auth_token = "token1"
# test unicode support # test unicode support
user_name = u"John Gāo" user_name = "John Gāo"
user_id = generate_id(user_name) user_id = generate_id(user_name)
project_name = 'tenant1' project_name = 'tenant1'
project_id = generate_id(project_name) project_id = generate_id(project_name)
@ -485,8 +484,8 @@ class ContextTest(test_base.BaseTestCase):
self.assertEqual(show_deleted, d['show_deleted']) self.assertEqual(show_deleted, d['show_deleted'])
self.assertEqual(request_id, d['request_id']) self.assertEqual(request_id, d['request_id'])
self.assertEqual(resource_uuid, d['resource_uuid']) self.assertEqual(resource_uuid, d['resource_uuid'])
user_identity = "%s %s %s %s %s" % (user_id, project_id, domain_id, user_identity = "{} {} {} {} {}".format(
user_domain_id, project_domain_id) user_id, project_id, domain_id, user_domain_id, project_domain_id)
self.assertEqual(user_identity, d['user_identity']) self.assertEqual(user_identity, d['user_identity'])
self.assertEqual([], d['roles']) self.assertEqual([], d['roles'])

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2020 Red Hat, Inc. # Copyright (C) 2020 Red Hat, Inc.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");