From 34e8cd3148951a2246a2530b55921c5668a28198 Mon Sep 17 00:00:00 2001 From: Adrian Moennich Date: Wed, 22 Nov 2023 18:19:25 +0100 Subject: [PATCH] Fix ruff warnings --- citadel/indico_citadel/backend.py | 15 ++++++++++----- citadel/indico_citadel/cli.py | 2 +- citadel/indico_citadel/models/id_map.py | 2 +- citadel/indico_citadel/schemas.py | 8 ++++---- citadel/indico_citadel/util_test.py | 12 ++++++------ livesync/indico_livesync/cli.py | 4 ++-- livesync/indico_livesync/models/queue.py | 7 +++++-- livesync/indico_livesync/uploader.py | 6 +++--- livesync/indico_livesync/util.py | 2 +- payment_paypal/tests/controllers_test.py | 1 - piwik/indico_piwik/reports.py | 2 +- .../indico_previewer_jupyter/cpp_highlighter.py | 2 +- prometheus/tests/endpoint_test.py | 4 ++-- storage_s3/indico_storage_s3/migrate.py | 6 +++--- storage_s3/indico_storage_s3/storage.py | 7 +++---- storage_s3/indico_storage_s3/task.py | 2 +- update-meta.py | 9 +++------ vc_zoom/indico_vc_zoom/cli.py | 3 +-- vc_zoom/indico_vc_zoom/forms.py | 2 +- vc_zoom/indico_vc_zoom/plugin.py | 4 ++-- 20 files changed, 51 insertions(+), 49 deletions(-) diff --git a/citadel/indico_citadel/backend.py b/citadel/indico_citadel/backend.py index 79762cb..291522c 100644 --- a/citadel/indico_citadel/backend.py +++ b/citadel/indico_citadel/backend.py @@ -121,7 +121,10 @@ class LiveSyncCitadelUploader(Uploader): # XXX this should no longer happen under any circumstances! # if we already have a mapping entry, delete the newly created record and # update the existing one in case something changed in the meantime - self.logger.error(f'{object_type.name.title()} %d already in citadel; deleting+updating', object_id) + self.logger.error( + f'{object_type.name.title()} %d already in citadel; deleting+updating', # noqa: G004 + object_id + ) db.session.rollback() self._citadel_delete(session, new_citadel_id, delete_mapping=False) existing_citadel_id = CitadelIdMap.get_citadel_id(object_type, object_id) @@ -313,12 +316,14 @@ class LiveSyncCitadelBackend(LiveSyncBackendBase): def process_queue(self, uploader, allowed_categories=()): super().process_queue(uploader, allowed_categories) uploader_name = type(uploader).__name__ - self.plugin.logger.info(f'{uploader_name} starting file upload') + self.plugin.logger.info(f'{uploader_name} starting file upload') # noqa: G004 total, errors, aborted = self.run_export_files(verbose=False) if aborted: - self.plugin.logger.info(f'{uploader_name} aborted after uploading %d files (%d failed)', total, errors) + self.plugin.logger.info(f'{uploader_name} aborted after uploading %d files (%d failed)', # noqa: G004 + total, errors) else: - self.plugin.logger.info(f'{uploader_name} finished uploading %d files (%d failed)', total, errors) + self.plugin.logger.info(f'{uploader_name} finished uploading %d files (%d failed)', # noqa: G004 + total, errors) def run_initial_export(self, batch_size, force=False, verbose=False): if not super().run_initial_export(batch_size, force, verbose): @@ -363,7 +368,7 @@ class LiveSyncCitadelBackend(LiveSyncBackendBase): lambda obj: re.sub(r'\s+', ' ', strip_control_chars(obj.attachment.title)), print_total_time=True) else: - self.plugin.logger.info(f'{total} files need to be uploaded') + self.plugin.logger.info('%d files need to be uploaded', total) total, errors, aborted = uploader.upload_files(attachments, initial=initial) return total, errors, aborted diff --git a/citadel/indico_citadel/cli.py b/citadel/indico_citadel/cli.py index 46df8bf..306b533 100644 --- a/citadel/indico_citadel/cli.py +++ b/citadel/indico_citadel/cli.py @@ -57,7 +57,7 @@ def upload(batch, force, max_size, retry): traceback.print_exc() print('Restarting in 2 seconds\a') time.sleep(2) - os.execl(sys.argv[0], *sys.argv) + os.execl(sys.argv[0], *sys.argv) # noqa: S606 return # exec doesn't return but just in case... if not errors and not aborted: diff --git a/citadel/indico_citadel/models/id_map.py b/citadel/indico_citadel/models/id_map.py index 1a071c8..ac02f6d 100644 --- a/citadel/indico_citadel/models/id_map.py +++ b/citadel/indico_citadel/models/id_map.py @@ -60,7 +60,7 @@ def _make_checks(): class CitadelIdMap(db.Model): __tablename__ = 'id_map' - __table_args__ = tuple(_make_checks()) + ({'schema': 'plugin_citadel'},) + __table_args__ = (*_make_checks(), {'schema': 'plugin_citadel'}) id = db.Column( db.Integer, diff --git a/citadel/indico_citadel/schemas.py b/citadel/indico_citadel/schemas.py index 9cc011f..06993f7 100644 --- a/citadel/indico_citadel/schemas.py +++ b/citadel/indico_citadel/schemas.py @@ -62,9 +62,9 @@ class ACLSchema: manager_list = set(linked_object.get_manager_list(recursive=True)) if attachment.is_self_protected: - return _get_identifiers({e for e in attachment.acl} | manager_list) + return _get_identifiers(set(attachment.acl) | manager_list) elif attachment.is_inheriting and attachment.folder.is_self_protected: - return _get_identifiers({e for e in attachment.folder.acl} | manager_list) + return _get_identifiers(set(attachment.folder.acl) | manager_list) else: return self._get_acl(linked_object) @@ -84,7 +84,7 @@ class ACLSchema: elif isinstance(object, EventNote): obj_acl = self._get_acl(object.object) else: - raise ValueError(f'unknown object {object}') + raise TypeError(f'unknown object {object}') acl = { 'owner': [default_acl], @@ -92,7 +92,7 @@ class ACLSchema: 'delete': [default_acl] } if obj_acl is not None: - acl['read'] = [default_acl] + obj_acl + acl['read'] = [default_acl, *obj_acl] return acl diff --git a/citadel/indico_citadel/util_test.py b/citadel/indico_citadel/util_test.py index 6520d8f..70f7c21 100644 --- a/citadel/indico_citadel/util_test.py +++ b/citadel/indico_citadel/util_test.py @@ -10,7 +10,7 @@ import pytest from indico_citadel.util import _flatten, format_aggregations, format_query, remove_none_entries -@pytest.mark.parametrize(('query', 'expected'), [ +@pytest.mark.parametrize(('query', 'expected'), ( ('title:"my event" ola person:john ola some:yes ola', 'title:"my event" ola person:john ola some\\:yes ola'), ('title:"my title:something"', 'title:"my title\\:something"'), @@ -24,29 +24,29 @@ from indico_citadel.util import _flatten, format_aggregations, format_query, rem ('"section meeting" OR "group meeting"', '"section meeting" OR "group meeting"'), ('title:meeting AND "indico"', 'title:meeting AND "indico"'), ('title:valid stringtitle:valid foo:bar', 'title:valid stringtitle\\:valid foo\\:bar') -]) +)) def test_query_placeholders(query, expected): placeholders = {'title': 'title', 'person': 'person', 'file': 'file'} assert format_query(query, placeholders) == expected -@pytest.mark.parametrize(('val', 'expected'), [ +@pytest.mark.parametrize(('val', 'expected'), ( ({'a': 0, 'b': None, 'c': {'c1': None, 'c2': 0, 'c3': {'c3a': None}}}, {'a': 0, 'c': {'c2': 0, 'c3': {}}}), ({'a': 0, 'b': [None, {'b1': None, 'b2': 'test'}]}, {'a': 0, 'b': [None, {'b2': 'test'}]}), (None, None), -]) +)) def test_remove_none_entries(val, expected): assert remove_none_entries(val) == expected -@pytest.mark.parametrize(('val', 'expected'), [ +@pytest.mark.parametrize(('val', 'expected'), ( ({'person': {'name': {'buckets': []}, 'affiliation': {'buckets': []}}, 'other': {'other': {'buckets': []}}}, {'person_name', 'person_affiliation', 'other_other'}), ({'other': {'other': {'other': {'other': {'buckets': []}}}}}, {'other_other_other_other'}) -]) +)) def test_flatten(val, expected): assert {key for key, _ in _flatten(val)} == expected diff --git a/livesync/indico_livesync/cli.py b/livesync/indico_livesync/cli.py index ba29ecb..588598b 100644 --- a/livesync/indico_livesync/cli.py +++ b/livesync/indico_livesync/cli.py @@ -107,7 +107,7 @@ def initial_export(agent_id, batch, force, verbose, retry): traceback.print_exc() print('Restarting in 2 seconds') time.sleep(2) - os.execl(sys.argv[0], *sys.argv) + os.execl(sys.argv[0], *sys.argv) # noqa: S606 return # exec doesn't return but just in case... agent.initial_data_exported = True @@ -197,7 +197,7 @@ def reset(agent_id): for i in range(5): print(cformat('\rResetting in %{white!}{}%{reset}s (CTRL+C to abort)').format(5 - i), end='') time.sleep(1) - print('') + print() backend.reset() db.session.commit() diff --git a/livesync/indico_livesync/models/queue.py b/livesync/indico_livesync/models/queue.py index c75a691..30b2fce 100644 --- a/livesync/indico_livesync/models/queue.py +++ b/livesync/indico_livesync/models/queue.py @@ -64,7 +64,7 @@ def _make_checks(): class LiveSyncQueueEntry(db.Model): __tablename__ = 'queues' - __table_args__ = tuple(_make_checks()) + ({'schema': 'plugin_livesync'},) + __table_args__ = (*_make_checks(), {'schema': 'plugin_livesync'}) #: Entry ID id = db.Column( @@ -267,7 +267,7 @@ class LiveSyncQueueEntry(db.Model): note_id=None, attachment_id=None) @classmethod - def create(cls, changes, ref, excluded_categories=set()): + def create(cls, changes, ref, excluded_categories=None): """Create a new change in all queues. :param changes: the change types, an iterable containing @@ -277,6 +277,9 @@ class LiveSyncQueueEntry(db.Model): :param excluded_categories: set of categories (IDs) whose items will not be tracked """ + if excluded_categories is None: + excluded_categories = set() + ref = dict(ref) obj = obj_deref(ref) diff --git a/livesync/indico_livesync/uploader.py b/livesync/indico_livesync/uploader.py index 6279cbb..85a4f0b 100644 --- a/livesync/indico_livesync/uploader.py +++ b/livesync/indico_livesync/uploader.py @@ -35,17 +35,17 @@ class Uploader: if self.from_cli: simplified = self._make_verbose(simplified, total) try: - self.logger.info(f'{self_name} uploading %d changes from %d records', total, len(records)) + self.logger.info(f'{self_name} uploading %d changes from %d records', total, len(records)) # noqa: G004 if not self.upload_records(simplified): self.logger.warning('uploader indicated a failure') return except Exception: - self.logger.exception(f'{self_name} failed') + self.logger.exception(f'{self_name} failed') # noqa: G004 if self.from_cli: raise return self.processed_records(records) - self.logger.info(f'{self_name} finished (%d total changes from %d records)', total, len(records)) + self.logger.info(f'{self_name} finished (%d total changes from %d records)', total, len(records)) # noqa: G004 def run_initial(self, records, total): """Runs the initial batch upload diff --git a/livesync/indico_livesync/util.py b/livesync/indico_livesync/util.py index d254b70..5d52a93 100644 --- a/livesync/indico_livesync/util.py +++ b/livesync/indico_livesync/util.py @@ -39,7 +39,7 @@ def obj_ref(obj): elif isinstance(obj, Attachment): ref = {'type': EntryType.attachment, 'attachment_id': obj.id} else: - raise ValueError(f'Unexpected object: {obj.__class__.__name__}') + raise TypeError(f'Unexpected object: {obj.__class__.__name__}') return ImmutableDict(ref) diff --git a/payment_paypal/tests/controllers_test.py b/payment_paypal/tests/controllers_test.py index 1ee01ef..06962b1 100644 --- a/payment_paypal/tests/controllers_test.py +++ b/payment_paypal/tests/controllers_test.py @@ -39,7 +39,6 @@ def test_ipn_verify_business(formdata, expected, dummy_event): ('13.37', 'EUR', True), ('13.37', 'CHF', False), ('10.00', 'CHF', False), - ('10.00', 'CHF', False), )) def test_ipn_verify_amount(mocker, amount, currency, expected): nai = mocker.patch('indico_payment_paypal.controllers.notify_amount_inconsistency') diff --git a/piwik/indico_piwik/reports.py b/piwik/indico_piwik/reports.py index 7a4d28f..b640a9a 100644 --- a/piwik/indico_piwik/reports.py +++ b/piwik/indico_piwik/reports.py @@ -70,7 +70,7 @@ class ReportBase: if self.end_date is None: today = now_utc().date() end_date = self.event.end_dt.date() - self.end_date = end_date if end_date < today else today + self.end_date = min(today, end_date) if self.start_date is None: self.start_date = self.end_date - timedelta(days=ReportBase.default_report_interval) diff --git a/previewer_jupyter/indico_previewer_jupyter/cpp_highlighter.py b/previewer_jupyter/indico_previewer_jupyter/cpp_highlighter.py index 6804a70..5b2bdcc 100644 --- a/previewer_jupyter/indico_previewer_jupyter/cpp_highlighter.py +++ b/previewer_jupyter/indico_previewer_jupyter/cpp_highlighter.py @@ -59,7 +59,7 @@ class CppHighlighter(Preprocessor): try: if nb.metadata.kernelspec.language == 'c++': self.preprocess_cell = self._preprocess_cell_cpp - except Exception: + except Exception: # noqa: S110 - no idea what exception's being caught here :( # if no language metadata, keep python as default pass return super().preprocess(nb, resources) diff --git a/prometheus/tests/endpoint_test.py b/prometheus/tests/endpoint_test.py index acdbe9a..f0d2156 100644 --- a/prometheus/tests/endpoint_test.py +++ b/prometheus/tests/endpoint_test.py @@ -46,7 +46,7 @@ def test_endpoint_works(get_metrics): @pytest.mark.usefixtures('db', 'enable_plugin') def test_endpoint_empty(get_metrics): - metrics, _ = get_metrics() + metrics = get_metrics()[0] assert metrics['indico_num_users'] == 1.0 assert metrics['indico_num_active_users'] == 0.0 @@ -77,7 +77,7 @@ def test_endpoint_returning_data(get_metrics, create_event): # create an event create_event(title='Test event #1') - metrics, _ = get_metrics() + metrics = get_metrics()[0] assert metrics['indico_num_users'] == 2.0 assert metrics['indico_num_active_users'] == 0.0 assert metrics['indico_num_events'] == 1.0 diff --git a/storage_s3/indico_storage_s3/migrate.py b/storage_s3/indico_storage_s3/migrate.py index 0605dd6..67526e5 100644 --- a/storage_s3/indico_storage_s3/migrate.py +++ b/storage_s3/indico_storage_s3/migrate.py @@ -135,7 +135,7 @@ class S3Importer: for model, total in models.items()} max_length = max(len(x) for x in labels.values()) labels = {model: label.ljust(max_length) for model, label in labels.items()} - for model, total in sorted(list(models.items()), key=itemgetter(1)): + for model, total in sorted(models.items(), key=itemgetter(1)): with click.progressbar(self.query_chunked(model, 1000), length=total, label=labels[model], show_percent=True, show_pos=True) as objects: for obj in self.flush_rclone_iterator(objects, 1000): @@ -342,7 +342,7 @@ def apply_changes(data_file, revert=False): for model, total in models.items()} max_length = max(len(x) for x in labels.values()) labels = {model: label.ljust(max_length) for model, label in labels.items()} - for model, total in sorted(list(models.items()), key=itemgetter(1)): + for model, total in sorted(models.items(), key=itemgetter(1)): pks = inspect(model).primary_key with click.progressbar(data[model.__name__], length=total, label=labels[model], show_percent=True, show_pos=True) as entries: @@ -469,7 +469,7 @@ def copy(source_backend_names, bucket_names, static_bucket_name, s3_endpoint, s3 code.append(' backend = backend.replace("", dt.strftime("%W"))') code.append(' return bucket, backend') d = {} - exec('\n'.join(code), d) + exec('\n'.join(code), d) # noqa: S102 if not source_backend_names: source_backend_names = [x for x in config.STORAGE_BACKENDS if not isinstance(get_storage(x), S3StorageBase)] if rclone: diff --git a/storage_s3/indico_storage_s3/storage.py b/storage_s3/indico_storage_s3/storage.py index 7f88c8d..e1a52a5 100644 --- a/storage_s3/indico_storage_s3/storage.py +++ b/storage_s3/indico_storage_s3/storage.py @@ -13,6 +13,7 @@ from contextlib import contextmanager from datetime import date from enum import Enum from io import BytesIO +from pathlib import Path from tempfile import NamedTemporaryFile from urllib.parse import quote @@ -172,8 +173,7 @@ class S3StorageBase(Storage): if self.bucket_versioning: self.client.put_bucket_versioning(Bucket=name, VersioningConfiguration={'Status': 'Enabled'}) if self.bucket_policy_file: - with open(self.bucket_policy_file) as f: - policy = f.read() + policy = Path(self.bucket_policy_file).read_text() self.client.put_bucket_policy(Bucket=name, Policy=policy) def _bucket_exists(self, name): @@ -248,8 +248,7 @@ class DynamicS3Storage(S3StorageBase): def _replace_bucket_placeholders(self, name, date): name = name.replace('', date.strftime('%Y')) name = name.replace('', date.strftime('%m')) - name = name.replace('', date.strftime('%W')) - return name + return name.replace('', date.strftime('%W')) def save(self, name, content_type, filename, fileobj): try: diff --git a/storage_s3/indico_storage_s3/task.py b/storage_s3/indico_storage_s3/task.py index 9fe966f..7ed49cb 100644 --- a/storage_s3/indico_storage_s3/task.py +++ b/storage_s3/indico_storage_s3/task.py @@ -32,7 +32,7 @@ def create_bucket(): bucket_date = today + relativedelta(weeks=1) bucket = storage._get_bucket_name(bucket_date) storage._create_bucket(bucket) - elif placeholders == {'', ''} or placeholders == {''}: + elif placeholders in ({'', ''}, {''}): if '' in placeholders or today.month == 12: bucket_date = today + relativedelta(months=1) bucket = storage._get_bucket_name(bucket_date) diff --git a/update-meta.py b/update-meta.py index 60b9f88..d6a57d8 100644 --- a/update-meta.py +++ b/update-meta.py @@ -40,7 +40,7 @@ def _find_plugins(): def _get_config(): rv = {'extras': {}, 'skip': []} try: - f = open('_meta/meta.yaml') + f = open('_meta/meta.yaml') # noqa: SIM115 except OSError as exc: if exc.errno != errno.ENOENT: raise @@ -84,17 +84,14 @@ def cli(nextver): else: plugins_require.append(pkgspec) - output = [] - for entry in plugins_require: - output.append(f' {entry}') + output = [f' {entry}' for entry in plugins_require] if extras_require: if output: output.append('') output.append('[options.extras_require]') for extra, pkgspecs in sorted(extras_require.items()): output.append(f'{extra} =') - for pkg in sorted(pkgspecs): - output.append(f' {pkg}') + output.extend(f' {pkg}' for pkg in sorted(pkgspecs)) if _update_meta('\n'.join(output)): click.secho('Updated meta package', fg='green') diff --git a/vc_zoom/indico_vc_zoom/cli.py b/vc_zoom/indico_vc_zoom/cli.py index 3435dde..5012bc1 100644 --- a/vc_zoom/indico_vc_zoom/cli.py +++ b/vc_zoom/indico_vc_zoom/cli.py @@ -28,8 +28,7 @@ def rooms(status=None): if status: room_query = room_query.filter(VCRoom.status == VCRoomStatus.get(status)) - for room in room_query: - table_data.append([str(room.id), room.name, room.status.name, str(room.data['zoom_id'])]) + table_data.extend([str(room.id), room.name, room.status.name, str(room.data['zoom_id'])] for room in room_query) table = AsciiTable(table_data) for col in (0, 3, 4): diff --git a/vc_zoom/indico_vc_zoom/forms.py b/vc_zoom/indico_vc_zoom/forms.py index b96dbea..95b9624 100644 --- a/vc_zoom/indico_vc_zoom/forms.py +++ b/vc_zoom/indico_vc_zoom/forms.py @@ -95,7 +95,7 @@ class VCRoomForm(VCRoomFormBase): allow_webinars = current_plugin.settings.get('allow_webinars') if allow_webinars: - for field_name in {'mute_audio', 'mute_participant_video', 'waiting_room'}: + for field_name in ('mute_audio', 'mute_participant_video', 'waiting_room'): inject_validators(self, field_name, [HiddenUnless('meeting_type', 'regular')]) super().__init__(*args, **kwargs) diff --git a/vc_zoom/indico_vc_zoom/plugin.py b/vc_zoom/indico_vc_zoom/plugin.py index e830f94..634a465 100644 --- a/vc_zoom/indico_vc_zoom/plugin.py +++ b/vc_zoom/indico_vc_zoom/plugin.py @@ -256,7 +256,7 @@ class ZoomPlugin(VCPluginMixin, IndicoPlugin): if room_assoc.link_object.start_dt else {}) meeting = fetch_zoom_meeting(vc_room) - current_schedule_args = {k: meeting[k] for k in {'start_time', 'duration'} if k in meeting} + current_schedule_args = {k: meeting[k] for k in ('start_time', 'duration') if k in meeting} # check whether the start time / duration of the scheduled meeting differs if new_schedule_args != current_schedule_args: @@ -521,7 +521,7 @@ class ZoomPlugin(VCPluginMixin, IndicoPlugin): def get_vc_room_attach_form_defaults(self, event): defaults = super().get_vc_room_attach_form_defaults(event) - defaults['password_visibility'] = 'logged_in' + defaults['password_visibility'] = 'logged_in' # noqa: S105 return defaults def can_manage_vc_room(self, user, room):