Skip to content

Commit

Permalink
Add ruff rules A,B,C4,ISC,PGH,RET,RUF,S,SIM,SLF
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Feb 26, 2025
1 parent 727e91c commit 18c1640
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 39 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ select = [
"DJ", # flake8-django
"DTZ", # flake8-datetimez
"E", # pycodestyle errors
"EM", # flake8-errmsg
"F", # Pyflakes
"FIX", # flake8-fixme
"FLY", # flynt
Expand All @@ -92,7 +91,6 @@ select = [
"ISC", # flake8-implicit-str-concat
"NPY", # NumPy-specific rules
"PD", # pandas-vet
"PERF", # Perflint
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PL", # Pylint
Expand All @@ -116,12 +114,14 @@ select = [
# "COM", # flake8-commas
# "CPY", # flake8-copyright
# "D", # pydocstyle
# "EM", # flake8-errmsg
# "ERA", # eradicate
# "EXE", # flake8-executable
# "FA", # flake8-future-annotations
# "FBT", # flake8-boolean-trap
# "I", # isort
# "N", # pep8-naming
# "PERF", # Perflint
# "PT", # flake8-pytest-style
# "PTH", # flake8-use-pathlib
# "Q", # flake8-quotes
Expand Down
8 changes: 4 additions & 4 deletions waffle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def get_waffle_model(setting_name: str) -> (
try:
return django_apps.get_model(flag_model_name)
except ValueError as ve:
msg = f"WAFFLE_{setting_name} must be of the form 'app_label.model_name'"
raise ImproperlyConfigured(msg) from ve
raise ImproperlyConfigured(f"WAFFLE_{setting_name} must be of the form 'app_label.model_name'") from ve
except LookupError as le:
msg = f"WAFFLE_{setting_name} refers to model '{flag_model_name}' that has not been installed"
raise ImproperlyConfigured(msg) from le
raise ImproperlyConfigured(
f"WAFFLE_{setting_name} refers to model '{flag_model_name}' that has not been installed"
) from le
2 changes: 1 addition & 1 deletion waffle/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def label_and_url_for_value(self, values: Any) -> tuple[str, str]:
.using(self.db) \
.get(**{key: value})
names.append(escape(str(name)))
except self.rel.model.DoesNotExist: # noqa: PERF203
except self.rel.model.DoesNotExist:
names.append('<missing>')
return "(" + ", ".join(names) + ")", ""

Expand Down
16 changes: 6 additions & 10 deletions waffle/management/commands/waffle_flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ def handle(self, *args: Any, **options: Any) -> None:
flag_name = options['name']

if not flag_name:
msg = 'You need to specify a flag name.'
raise CommandError(msg)
raise CommandError('You need to specify a flag name.')

if options['create']:
flag, created = get_waffle_flag_model().objects.get_or_create(name=flag_name)
Expand All @@ -122,8 +121,7 @@ def handle(self, *args: Any, **options: Any) -> None:
try:
flag = get_waffle_flag_model().objects.get(name=flag_name)
except get_waffle_flag_model().DoesNotExist as dne:
msg = 'This flag does not exist.'
raise CommandError(msg) from dne
raise CommandError('This flag does not exist.') from dne

# Group isn't an attribute on the Flag, but a related Many-to-Many
# field, so we handle it a bit differently by looking up groups and
Expand All @@ -135,9 +133,8 @@ def handle(self, *args: Any, **options: Any) -> None:
try:
group_instance = Group.objects.get(name=group)
group_hash[group_instance.name] = group_instance.id
except Group.DoesNotExist as dne: # noqa: PERF203
msg = f'Group {group} does not exist'
raise CommandError(msg) from dne
except Group.DoesNotExist as dne:
raise CommandError(f'Group {group} does not exist') from dne
# If 'append' was not passed, we clear related groups
if not options_append:
flag.groups.clear()
Expand All @@ -155,9 +152,8 @@ def handle(self, *args: Any, **options: Any) -> None:
| Q(**{UserModel.EMAIL_FIELD: username})
)
user_hash.add(user_instance)
except UserModel.DoesNotExist as dne: # noqa: PERF203
msg = f'User {username} does not exist'
raise CommandError(msg) from dne
except UserModel.DoesNotExist as dne:
raise CommandError(f'User {username} does not exist') from dne
# If 'append' was not passed, we clear related users
if not options_append:
flag.users.clear()
Expand Down
13 changes: 6 additions & 7 deletions waffle/management/commands/waffle_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ def handle(self, *args: Any, **options: Any) -> None:
percent = options['percent']

if not (sample_name and percent):
msg = 'You need to specify a sample name and percentage.'
raise CommandError(msg)
raise CommandError(
'You need to specify a sample name and percentage.'
)

try:
percent = float(percent)
if not (0.0 <= percent <= 100.0):
raise ValueError
except ValueError as ve:
msg = 'You need to enter a valid percentage value.'
raise CommandError(msg) from ve
except ValueError as e:
raise CommandError('You need to enter a valid percentage value.') from e

if options['create']:
sample, created = get_waffle_sample_model().objects.get_or_create(
Expand All @@ -61,8 +61,7 @@ def handle(self, *args: Any, **options: Any) -> None:
try:
sample = get_waffle_sample_model().objects.get(name=sample_name)
except get_waffle_sample_model().DoesNotExist as dne:
msg = 'This sample does not exist.'
raise CommandError(msg) from dne
raise CommandError('This sample does not exist.') from dne

sample.percent = percent
sample.save()
12 changes: 5 additions & 7 deletions waffle/management/commands/waffle_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@


def on_off_bool(string: str) -> bool:
if string not in {'on', 'off'}:
msg = f"invalid choice: {string!r} (choose from 'on', 'off')"
raise ArgumentTypeError(msg)
if string not in ['on', 'off']:
raise ArgumentTypeError(f"invalid choice: {string!r} (choose from 'on', "
"'off')")
return string == 'on'


Expand Down Expand Up @@ -53,8 +53,7 @@ def handle(self, *args: Any, **options: Any) -> None:
state = options['state']

if not (switch_name and state is not None):
msg = 'You need to specify a switch name and state.'
raise CommandError(msg)
raise CommandError('You need to specify a switch name and state.')

if options["create"]:
switch, created = get_waffle_switch_model().objects.get_or_create(
Expand All @@ -66,8 +65,7 @@ def handle(self, *args: Any, **options: Any) -> None:
try:
switch = get_waffle_switch_model().objects.get(name=switch_name)
except get_waffle_switch_model().DoesNotExist as dne:
msg = 'This switch does not exist.'
raise CommandError(msg) from dne
raise CommandError('This switch does not exist.') from dne

switch.active = state
switch.save()
3 changes: 1 addition & 2 deletions waffle/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ def validate_waffle(self, waffle, func):
return func(waffle)

def invalid_waffle(self):
msg = 'Inactive waffle'
raise Http404(msg)
raise Http404('Inactive waffle')


class WaffleFlagMixin(BaseWaffleMixin):
Expand Down
3 changes: 1 addition & 2 deletions waffle/templatetags/waffle_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def render(self, context):
def handle_token(cls, parser, token, kind, condition):
bits = token.split_contents()
if len(bits) < 2:
msg = f"{bits[0]!r} tag requires an argument"
raise template.TemplateSyntaxError(msg)
raise template.TemplateSyntaxError(f"{bits[0]!r} tag requires an argument")

name = bits[1]
compiled_name = parser.compile_filter(name)
Expand Down
6 changes: 2 additions & 4 deletions waffle/tests/test_testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ def test_restores_after_exception(self):

def inner():
with override_switch('foo', active=False):
msg = "Trying to break"
raise RuntimeError(msg)
raise RuntimeError("Trying to break")

with self.assertRaises(RuntimeError):
inner()
Expand All @@ -79,8 +78,7 @@ def test_restores_after_exception_in_decorator(self):

@override_switch('foo', active=False)
def inner():
msg = "Trying to break"
raise RuntimeError(msg)
raise RuntimeError("Trying to break")

with self.assertRaises(RuntimeError):
inner()
Expand Down

0 comments on commit 18c1640

Please sign in to comment.