diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 18496c1..e0bfe73 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 + rev: v5.0.0 hooks: - id: trailing-whitespace # Replaces or checks mixed line ending @@ -19,13 +19,18 @@ repos: - id: check-yaml files: .*\.(yaml|yml)$ - repo: https://opendev.org/openstack/hacking - rev: 6.1.0 + rev: 7.0.0 hooks: - id: hacking additional_dependencies: [] exclude: '^(doc|releasenotes|tools)/.*$' - repo: https://github.com/PyCQA/bandit - rev: 1.7.6 + rev: 1.7.10 hooks: - id: bandit args: ['-x', 'tests'] + - repo: https://github.com/asottile/pyupgrade + rev: v3.18.0 + hooks: + - id: pyupgrade + args: [--py3-only] diff --git a/doc/source/conf.py b/doc/source/conf.py index 78393ca..8481315 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright (C) 2020 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/oslo_middleware/base.py b/oslo_middleware/base.py index f494675..dee7f7d 100644 --- a/oslo_middleware/base.py +++ b/oslo_middleware/base.py @@ -32,7 +32,7 @@ class NoContentTypeRequest(webob.request.Request): ResponseClass = NoContentTypeResponse -class ConfigurableMiddleware(object): +class ConfigurableMiddleware: """Base WSGI middleware wrapper. These classes require an application to be initialized that will be called diff --git a/oslo_middleware/basic_auth.py b/oslo_middleware/basic_auth.py index 21af891..c020c9b 100644 --- a/oslo_middleware/basic_auth.py +++ b/oslo_middleware/basic_auth.py @@ -82,7 +82,7 @@ def authenticate(auth_file, username, password): line_prefix = username + ':' try: - with open(auth_file, 'r') as f: + with open(auth_file) as f: for line in f: entry = line.strip() if entry and entry.startswith(line_prefix): @@ -124,7 +124,7 @@ def validate_auth_file(auth_file): """ try: - with open(auth_file, 'r') as f: + with open(auth_file) as f: for line in f: entry = line.strip() if entry and ':' in entry: diff --git a/oslo_middleware/cors.py b/oslo_middleware/cors.py index 69fe3d6..adf81e0 100644 --- a/oslo_middleware/cors.py +++ b/oslo_middleware/cors.py @@ -74,9 +74,9 @@ def set_defaults(**kwargs): # there's no good way for a user to override only one option, because all # the others would be overridden to 'None'. - valid_params = set(k.name for k in CORS_OPTS - if k.name != 'allowed_origin') - passed_params = set(k for k in kwargs) + valid_params = {k.name for k in CORS_OPTS + if k.name != 'allowed_origin'} + passed_params = {k for k in kwargs} wrong_params = passed_params - valid_params if wrong_params: @@ -92,7 +92,7 @@ class InvalidOriginError(Exception): def __init__(self, origin): self.origin = origin - super(InvalidOriginError, self).__init__( + super().__init__( 'CORS request from origin \'%s\' not permitted.' % origin) @@ -117,7 +117,7 @@ class CORS(base.ConfigurableMiddleware): ] def __init__(self, application, *args, **kwargs): - super(CORS, self).__init__(application, *args, **kwargs) + super().__init__(application, *args, **kwargs) # Begin constructing our configuration hash. self.allowed_origins = {} self._init_conf() @@ -143,7 +143,7 @@ class CORS(base.ConfigurableMiddleware): 'oslo_config_project' not in local_conf): raise TypeError("allowed_origin or oslo_config_project " "is required") - return super(CORS, cls).factory(global_conf, **local_conf) + return super().factory(global_conf, **local_conf) def _init_conf(self): '''Initialize this middleware from an oslo.config instance.''' diff --git a/oslo_middleware/healthcheck/__init__.py b/oslo_middleware/healthcheck/__init__.py index b2b141d..cf7a3c0 100644 --- a/oslo_middleware/healthcheck/__init__.py +++ b/oslo_middleware/healthcheck/__init__.py @@ -388,7 +388,7 @@ Reason """ def __init__(self, *args, **kwargs): - super(Healthcheck, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) self.oslo_conf.register_opts(opts.HEALTHCHECK_OPTS, group='healthcheck') self._path = self._conf_get('path') @@ -430,14 +430,14 @@ Reason 'enabled at the same time.') def _conf_get(self, key, group='healthcheck'): - return super(Healthcheck, self)._conf_get(key, group=group) + return super()._conf_get(key, group=group) @removals.remove( message="The healthcheck middleware must now be configured as " "an application, not as a filter") @classmethod def factory(cls, global_conf, **local_conf): - return super(Healthcheck, cls).factory(global_conf, **local_conf) + return super().factory(global_conf, **local_conf) @classmethod def app_factory(cls, global_conf, **local_conf): @@ -539,7 +539,7 @@ Reason def _make_html_response(self, results, healthy): try: hostname = socket.gethostname() - except socket.error: + except OSError: hostname = None translated_results = [] for result in results: diff --git a/oslo_middleware/healthcheck/disable_by_file.py b/oslo_middleware/healthcheck/disable_by_file.py index cc99c08..3ff5fc2 100644 --- a/oslo_middleware/healthcheck/disable_by_file.py +++ b/oslo_middleware/healthcheck/disable_by_file.py @@ -44,7 +44,7 @@ class DisableByFilesPortsHealthcheck(pluginbase.HealthcheckBaseExtension): """ def __init__(self, *args, **kwargs): - super(DisableByFilesPortsHealthcheck, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) self.oslo_conf.register_opts(opts.DISABLE_BY_FILES_OPTS, group='healthcheck') self.status_files = {} @@ -99,7 +99,7 @@ class DisableByFileHealthcheck(pluginbase.HealthcheckBaseExtension): """ def __init__(self, *args, **kwargs): - super(DisableByFileHealthcheck, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) self.oslo_conf.register_opts(opts.DISABLE_BY_FILE_OPTS, group='healthcheck') diff --git a/oslo_middleware/healthcheck/enable_by_files.py b/oslo_middleware/healthcheck/enable_by_files.py index 727b6fd..0a27cc0 100644 --- a/oslo_middleware/healthcheck/enable_by_files.py +++ b/oslo_middleware/healthcheck/enable_by_files.py @@ -42,7 +42,7 @@ class EnableByFilesHealthcheck(pluginbase.HealthcheckBaseExtension): """ def __init__(self, *args, **kwargs): - super(EnableByFilesHealthcheck, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) self.oslo_conf.register_opts(opts.ENABLE_BY_FILES_OPTS, group='healthcheck') self.file_paths = self._conf_get('enable_by_file_paths') diff --git a/oslo_middleware/healthcheck/pluginbase.py b/oslo_middleware/healthcheck/pluginbase.py index ddfd940..af9f500 100644 --- a/oslo_middleware/healthcheck/pluginbase.py +++ b/oslo_middleware/healthcheck/pluginbase.py @@ -16,7 +16,7 @@ import abc -class HealthcheckResult(object): +class HealthcheckResult: """Result of a ``healthcheck`` method call should be this object.""" def __init__(self, available, reason, details=None): diff --git a/oslo_middleware/http_proxy_to_wsgi.py b/oslo_middleware/http_proxy_to_wsgi.py index 632f5ca..60740c3 100644 --- a/oslo_middleware/http_proxy_to_wsgi.py +++ b/oslo_middleware/http_proxy_to_wsgi.py @@ -1,4 +1,3 @@ -# -*- encoding: utf-8 -*- # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -33,7 +32,7 @@ class HTTPProxyToWSGI(base.ConfigurableMiddleware): """ def __init__(self, application, *args, **kwargs): - super(HTTPProxyToWSGI, self).__init__(application, *args, **kwargs) + super().__init__(application, *args, **kwargs) self.oslo_conf.register_opts(OPTS, group='oslo_middleware') @staticmethod diff --git a/oslo_middleware/sizelimit.py b/oslo_middleware/sizelimit.py index 4156449..59f131b 100644 --- a/oslo_middleware/sizelimit.py +++ b/oslo_middleware/sizelimit.py @@ -44,7 +44,7 @@ _opts = [ ] -class LimitingReader(object): +class LimitingReader: """Reader to limit the size of an incoming request.""" def __init__(self, data, limit): """Initiates LimitingReader object. @@ -83,7 +83,7 @@ class RequestBodySizeLimiter(base.ConfigurableMiddleware): """Limit the size of incoming requests.""" def __init__(self, application, conf=None): - super(RequestBodySizeLimiter, self).__init__(application, conf) + super().__init__(application, conf) self.oslo_conf.register_opts(_opts, group='oslo_middleware') @webob.dec.wsgify diff --git a/oslo_middleware/stats.py b/oslo_middleware/stats.py index 11c4789..c1424fb 100644 --- a/oslo_middleware/stats.py +++ b/oslo_middleware/stats.py @@ -64,7 +64,7 @@ class StatsMiddleware(base.ConfigurableMiddleware): """ def __init__(self, application, conf): - super(StatsMiddleware, self).__init__(application, conf) + super().__init__(application, conf) self.application = application self.stat_name = conf.get('name') if self.stat_name is None: diff --git a/oslo_middleware/tests/test_correlation_id.py b/oslo_middleware/tests/test_correlation_id.py index 738fa37..ecb926f 100644 --- a/oslo_middleware/tests/test_correlation_id.py +++ b/oslo_middleware/tests/test_correlation_id.py @@ -24,7 +24,7 @@ from oslo_middleware import correlation_id class CorrelationIdTest(test_base.BaseTestCase): def setUp(self): - super(CorrelationIdTest, self).setUp() + super().setUp() def test_process_request(self): app = mock.Mock() diff --git a/oslo_middleware/tests/test_cors.py b/oslo_middleware/tests/test_cors.py index c64c539..fe14663 100644 --- a/oslo_middleware/tests/test_cors.py +++ b/oslo_middleware/tests/test_cors.py @@ -56,7 +56,7 @@ class CORSTestBase(test_base.BaseTestCase): def setUp(self): """Setup the tests.""" - super(CORSTestBase, self).setUp() + super().setUp() # Set up the config fixture. self.config_fixture = self.useFixture(fixture.Config()) @@ -132,7 +132,7 @@ class CORSTestBase(test_base.BaseTestCase): class CORSTestDefaultOverrides(CORSTestBase): def setUp(self): - super(CORSTestDefaultOverrides, self).setUp() + super().setUp() fixture = self.config_fixture # Line length accommodation @@ -292,7 +292,7 @@ class CORSRegularRequestTest(CORSTestBase): def setUp(self): """Setup the tests.""" - super(CORSRegularRequestTest, self).setUp() + super().setUp() fixture = self.config_fixture # Line length accommodation fixture.load_raw_values(group='cors', @@ -613,7 +613,7 @@ class CORSPreflightRequestTest(CORSTestBase): """ def setUp(self): - super(CORSPreflightRequestTest, self).setUp() + super().setUp() fixture = self.config_fixture # Line length accommodation fixture.load_raw_values(group='cors', @@ -1146,7 +1146,7 @@ class CORSTestWildcard(CORSTestBase): """Test the CORS wildcard specification.""" def setUp(self): - super(CORSTestWildcard, self).setUp() + super().setUp() fixture = self.config_fixture # Line length accommodation fixture.load_raw_values(group='cors', diff --git a/oslo_middleware/tests/test_healthcheck.py b/oslo_middleware/tests/test_healthcheck.py index b453d2e..8da3b8a 100644 --- a/oslo_middleware/tests/test_healthcheck.py +++ b/oslo_middleware/tests/test_healthcheck.py @@ -54,7 +54,7 @@ class HealthcheckMainTests(test_base.BaseTestCase): class HealthcheckTests(test_base.BaseTestCase): def setUp(self): - super(HealthcheckTests, self).setUp() + super().setUp() self.useFixture(config.Config()) @staticmethod @@ -191,8 +191,9 @@ class HealthcheckTests(test_base.BaseTestCase): def test_disable_by_port_many_files(self): filename = self.create_tempfiles([('test', 'foobar')])[0] filename2 = self.create_tempfiles([('test2', 'foobar2')])[0] - conf = {'backends': 'disable_by_files_ports', - 'disable_by_file_paths': "80:%s,81:%s" % (filename, filename2)} + conf = { + 'backends': 'disable_by_files_ports', + 'disable_by_file_paths': "80:{},81:{}".format(filename, filename2)} self._do_test(conf, expected_code=webob.exc.HTTPServiceUnavailable.code, expected_body=b'DISABLED BY FILE') diff --git a/oslo_middleware/tests/test_http_proxy_to_wsgi.py b/oslo_middleware/tests/test_http_proxy_to_wsgi.py index d138021..5cf8e07 100644 --- a/oslo_middleware/tests/test_http_proxy_to_wsgi.py +++ b/oslo_middleware/tests/test_http_proxy_to_wsgi.py @@ -22,7 +22,7 @@ from oslo_middleware import http_proxy_to_wsgi class TestHTTPProxyToWSGI(test_base.BaseTestCase): def setUp(self): - super(TestHTTPProxyToWSGI, self).setUp() + super().setUp() @webob.dec.wsgify() def fake_app(req): @@ -139,7 +139,7 @@ class TestHTTPProxyToWSGI(test_base.BaseTestCase): class TestHTTPProxyToWSGIDisabled(test_base.BaseTestCase): def setUp(self): - super(TestHTTPProxyToWSGIDisabled, self).setUp() + super().setUp() @webob.dec.wsgify() def fake_app(req): diff --git a/oslo_middleware/tests/test_sizelimit.py b/oslo_middleware/tests/test_sizelimit.py index 5d547ac..da5186d 100644 --- a/oslo_middleware/tests/test_sizelimit.py +++ b/oslo_middleware/tests/test_sizelimit.py @@ -78,7 +78,7 @@ class TestLimitingReader(test_base.BaseTestCase): class TestRequestBodySizeLimiter(test_base.BaseTestCase): def setUp(self): - super(TestRequestBodySizeLimiter, self).setUp() + super().setUp() self.useFixture(config.Config()) @webob.dec.wsgify() diff --git a/oslo_middleware/tests/test_stats.py b/oslo_middleware/tests/test_stats.py index c6a1771..14ddc9c 100644 --- a/oslo_middleware/tests/test_stats.py +++ b/oslo_middleware/tests/test_stats.py @@ -47,7 +47,7 @@ class TestStaticMethods(test_base.BaseTestCase): class TestStatsMiddleware(test_base.BaseTestCase): def setUp(self): - super(TestStatsMiddleware, self).setUp() + super().setUp() self.patch(statsd, 'StatsClient', mock.MagicMock()) def make_stats_middleware(self, stat_name=None, stats_host=None, diff --git a/releasenotes/source/conf.py b/releasenotes/source/conf.py index 2d5b578..a846f50 100644 --- a/releasenotes/source/conf.py +++ b/releasenotes/source/conf.py @@ -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