Do extra code style checks with flake8-bugbear

Flake8 Bugbear checks for some extra things that aren’t code style
errors, but are likely to introduce bugs or unexpected behaviour. A
good example is having mutable default function arguments, which get
shared between every call to the function and therefore mutating a value
in one place can unexpectedly cause it to change in another.

This commit enables all the extra warnings provided by Flake8 Bugbear,
except for:
- the line length one (because we already lint for that separately)
- B903 Data class should either be immutable or use `__slots__` because
  this seems to false-positive on some of our custom exceptions
- B902 Invalid first argument 'cls' used for instance method because
  some SQLAlchemy decorators (eg `declared_attr`) make things that
  aren’t formally class methods take a class not an instance as their
  first argument

It disables:
- _B306: BaseException.message is removed in Python 3_ because I think
  our exceptions have a custom structure that means the `.message`
  attribute is still present

Matches the work done in other repos:
- https://github.com/alphagov/notifications-admin/pull/3172/files
This commit is contained in:
Chris Hill-Scott
2020-12-22 15:46:31 +00:00
parent 7152ac7cba
commit 3b0b96834d
29 changed files with 88 additions and 68 deletions

View File

@@ -185,7 +185,7 @@ def test_persist_notification_with_optionals(sample_job, sample_api_key):
assert NotificationHistory.query.count() == 0
persisted_notification = Notification.query.all()[0]
assert persisted_notification.id == n_id
persisted_notification.job_id == sample_job.id
assert persisted_notification.job_id == sample_job.id
assert persisted_notification.job_row_number == 10
assert persisted_notification.created_at == created_at

View File

@@ -385,7 +385,7 @@ def test_returns_ok_to_firetext_if_mismatched_sms_sender(notify_db_session, clie
assert not InboundSms.query.all()
assert result['status'] == 'ok'
mocked.call_count == 0
assert mocked.call_count == 0
@pytest.mark.parametrize(

View File

@@ -118,7 +118,7 @@ def test_should_set_cache_value_as_value_from_database_if_cache_not_set(
):
serialised_service = SerialisedService.from_id(sample_service.id)
with freeze_time("2016-01-01 12:00:00.000000"):
for x in range(5):
for _ in range(5):
create_notification(sample_template)
mocker.patch('app.notifications.validators.redis_store.get', return_value=None)
mocker.patch('app.notifications.validators.redis_store.set')
@@ -149,7 +149,7 @@ def test_check_service_message_limit_over_message_limit_fails(key_type, sample_s
template = create_template(sample_service)
serialised_service = SerialisedService.from_id(sample_service.id)
for x in range(5):
for _ in range(5):
create_notification(template)
with pytest.raises(TooManyRequestsError) as e:
check_service_over_daily_message_limit(key_type, serialised_service)