Skip to content

Commit

Permalink
Update ruff to >=0.9
Browse files Browse the repository at this point in the history
Update `ruff` and code for the "2025 style guide"
  • Loading branch information
robhudson committed Jan 16, 2025
1 parent 95a5abb commit eefdbfa
Show file tree
Hide file tree
Showing 21 changed files with 65 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ repos:
- --comment-style
- "|#|"
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.6
rev: v0.9.1
hooks:
- id: ruff
- id: ruff-format
Expand Down
4 changes: 1 addition & 3 deletions bedrock/base/tests/test_accepted_locales.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ def setup_class(cls):
"""
if os.path.exists(cls.locale_bkp):
raise Exception(
"A backup of locale/ exists at %s which might "
"mean that previous tests didn't end cleanly. "
"Skipping the test suite." % cls.locale_bkp
"A backup of locale/ exists at %s which might mean that previous tests didn't end cleanly. Skipping the test suite." % cls.locale_bkp
)
cls.DEV = settings.DEV
cls.PROD_LANGUAGES = settings.PROD_LANGUAGES
Expand Down
4 changes: 2 additions & 2 deletions bedrock/careers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

def generate_position_meta_description(position):
suffix = "n" if position.position_type.lower().startswith(("a", "e", "i", "o", "u")) else ""
meta = f"Mozilla is hiring a{suffix} " f"{position.position_type.lower()} {position.title} in "
meta = f"Mozilla is hiring a{suffix} {position.position_type.lower()} {position.title} in "

if len(position.location_list) > 1:
meta = meta + f'{", ".join(position.location_list[:-1])} and {position.location_list[-1]}'
meta = meta + f"{', '.join(position.location_list[:-1])} and {position.location_list[-1]}"
else:
meta = meta + position.location_list[0]

Expand Down
8 changes: 4 additions & 4 deletions bedrock/contentful/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def _get_card_image_url(image, width, aspect):


def _get_product_class(product):
return f'mzp-t-product-{PRODUCT_THEMES.get(product, "")}'
return f"mzp-t-product-{PRODUCT_THEMES.get(product, '')}"


def _get_layout_class(layout):
Expand All @@ -148,11 +148,11 @@ def _get_abbr_from_width(width):


def _get_aspect_ratio_class(aspect_ratio):
return f'mzp-has-aspect-{ASPECT_RATIOS.get(aspect_ratio, "")}'
return f"mzp-has-aspect-{ASPECT_RATIOS.get(aspect_ratio, '')}"


def _get_width_class(width):
return f'mzp-t-content-{WIDTHS.get(width, "")}' if width else ""
return f"mzp-t-content-{WIDTHS.get(width, '')}" if width else ""


def _get_theme_class(theme):
Expand Down Expand Up @@ -208,7 +208,7 @@ def _make_cta_button(entry):
# TODO, only add on Firefox themed pages
"mzp-t-product" if action != "Get Mozilla VPN" and action != "Get MDN Plus" else "",
"mzp-t-secondary" if fields.get("theme") == "Secondary" else "",
f'mzp-t-{WIDTHS.get(fields.get("size"), "")}' if fields.get("size") else "",
f"mzp-t-{WIDTHS.get(fields.get('size'), '')}" if fields.get("size") else "",
]
data = {
"action": action,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class Migration(migrations.Migration):
blank=True,
default="",
help_text=(
"Some pages may have custom fields on them, distinct "
"from their content type - eg: pagePageResourceCenter has a 'Product' field"
"Some pages may have custom fields on them, distinct from their content type - eg: pagePageResourceCenter has a 'Product' field"
),
max_length=255,
),
Expand Down
2 changes: 1 addition & 1 deletion bedrock/contentful/tests/test_contentful_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def test_LinkRenderer__mozilla_link__existing_utm(mock_get_current_request):
mozilla_mock_hyperlink_node = deepcopy(mock_hyperlink_node)
mozilla_mock_hyperlink_node["data"]["uri"] = "https://mozilla.org/test/page/?utm_source=UTMTEST"
output = LinkRenderer({"text": TextRenderer}).render(mozilla_mock_hyperlink_node)
expected = '<a href="https://mozilla.org/test/page/?utm_source=UTMTEST" ' 'data-cta-text="Example" rel="external noopener">Example</a>'
expected = '<a href="https://mozilla.org/test/page/?utm_source=UTMTEST" data-cta-text="Example" rel="external noopener">Example</a>'

assert output == expected

Expand Down
4 changes: 2 additions & 2 deletions bedrock/contentful/tests/test_contentful_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ def test_update_contentful__detect_and_delete_absent_entries(
for idx in range(total_to_create_per_locale):
ContentfulEntry.objects.create(
content_type=CONTENT_TYPE_PAGE_RESOURCE_CENTER,
contentful_id=f"entry_{idx+1}",
contentful_id=f"entry_{idx + 1}",
locale=locale,
)

Expand All @@ -710,7 +710,7 @@ def test_update_contentful__detect_and_delete_absent_entries__homepage_involved(
for idx in range(3):
ContentfulEntry.objects.create(
content_type=CONTENT_TYPE_PAGE_RESOURCE_CENTER,
contentful_id=f"entry_{idx+1}",
contentful_id=f"entry_{idx + 1}",
locale=locale,
)

Expand Down
4 changes: 2 additions & 2 deletions bedrock/contentful/tests/test_contentful_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ def test_locales_with_available_content(
for i in range(count):
ContentfulEntry.objects.create(
content_type=content_type,
contentful_id=f"entry_{i+1}",
contentful_id=f"entry_{i + 1}",
classification=classification,
locale=locale,
localisation_complete=True,
)
# Add some with incomplete localisation as control
ContentfulEntry.objects.create(
content_type=content_type,
contentful_id=f"entry_{i+100}",
contentful_id=f"entry_{i + 100}",
classification=classification,
locale=locale,
localisation_complete=False,
Expand Down
2 changes: 1 addition & 1 deletion bedrock/mozorg/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def render(self, name, value, attrs=None, renderer=None):

policy_txt = ftl("newsletter-form-im-okay-with-mozilla", url=reverse("privacy.notices.websites"))

return mark_safe(f"""<label for="{attrs['id']}" class="privacy-check-label">{input_txt}<span class="title">{policy_txt}</span></label>""")
return mark_safe(f"""<label for="{attrs["id"]}" class="privacy-check-label">{input_txt}<span class="title">{policy_txt}</span></label>""")


class HoneyPotWidget(widgets.TextInput):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, stdout=None, stderr=None, no_color=False):

def add_arguments(self, parser):
parser.add_argument("-q", "--quiet", action="store_true", dest="quiet", default=False, help="If no error occurs, swallow all output.")
parser.add_argument("--database", default="default", help=("Specifies the database to use, if using a db. " 'Defaults to "default".'))
parser.add_argument("--database", default="default", help=('Specifies the database to use, if using a db. Defaults to "default".'))
parser.add_argument("-f", "--force", action="store_true", dest="force", default=False, help="Load the data even if nothing new from git.")

def handle(self, *args, **options):
Expand Down
4 changes: 2 additions & 2 deletions bedrock/mozorg/templatetags/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def l10n_css(ctx):
locale = getattr(ctx["request"], "locale", "en-US")

if _l10n_media_exists("css", locale, "intl.css"):
markup = '<link rel="stylesheet" media="screen,projection,tv" href=' '"%s">' % static(path.join("css", "l10n", locale, "intl.css"))
markup = '<link rel="stylesheet" media="screen,projection,tv" href="%s">' % static(path.join("css", "l10n", locale, "intl.css"))
else:
markup = ""

Expand Down Expand Up @@ -708,7 +708,7 @@ def _fxa_product_button(
if class_name:
css_class += f" {class_name}"

markup = f'<a href="{href}" data-action="{settings.FXA_ENDPOINT}" class="{css_class}" {attrs}>' f"{button_text}" f"</a>"
markup = f'<a href="{href}" data-action="{settings.FXA_ENDPOINT}" class="{css_class}" {attrs}>{button_text}</a>'

return Markup(markup)

Expand Down
2 changes: 1 addition & 1 deletion bedrock/mozorg/tests/test_helper_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class TestL10nCSS(TestCase):
static_url_dev = "/static/"
cdn_url = "//mozorg.cdn.mozilla.net"
static_url_prod = cdn_url + static_url_dev
markup = '<link rel="stylesheet" media="screen,projection,tv" href=' '"%scss/l10n/%s/intl.css">'
markup = '<link rel="stylesheet" media="screen,projection,tv" href="%scss/l10n/%s/intl.css">'

def _render(self, locale):
req = self.rf.get("/")
Expand Down
29 changes: 10 additions & 19 deletions bedrock/products/templatetags/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,15 @@ def _vpn_get_ga_data(selected_plan):
analytics = selected_plan.get("analytics")

ga_data = (
"{"
"'id' : '%s',"
"'brand' : '%s',"
"'plan' : '%s',"
"'period' : '%s',"
"'price' : '%s',"
"'discount' : '%s',"
"'currency' : '%s'"
"}"
% (
id,
analytics.get("brand"),
analytics.get("plan"),
analytics.get("period"),
analytics.get("price"),
analytics.get("discount"),
analytics.get("currency"),
)
f"{{"
f"'id' : '{id}',"
f"'brand' : '{analytics.get('brand')}',"
f"'plan' : '{analytics.get('plan')}',"
f"'period' : '{analytics.get('period')}',"
f"'price' : '{analytics.get('price')}',"
f"'discount' : '{analytics.get('discount')}',"
f"'currency' : '{analytics.get('currency')}'"
f"}}"
)

return ga_data
Expand Down Expand Up @@ -107,7 +98,7 @@ def _vpn_product_link(product_url, entrypoint, link_text, class_name=None, optio
if class_name:
css_class += f" {class_name}"

markup = f'<a href="{href}" data-action="{settings.FXA_ENDPOINT}" class="{css_class}" {attrs}>' f"{link_text}" f"</a>"
markup = f'<a href="{href}" data-action="{settings.FXA_ENDPOINT}" class="{css_class}" {attrs}>{link_text}</a>'

return Markup(markup)

Expand Down
3 changes: 2 additions & 1 deletion bedrock/products/tests/test_helper_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,8 @@ def test_vpn_supported_locale(locale):
@pytest.mark.parametrize(
"locale",
[
"ach" "br",
"ach",
"br",
"sco",
"xh",
],
Expand Down
12 changes: 6 additions & 6 deletions bedrock/products/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,10 @@ def setUp(self):
classification=CONTENT_CLASSIFICATION_VPN,
locale=locale,
localisation_complete=True,
contentful_id=f"entry_{i+1}",
slug=f"slug-{i+1}",
contentful_id=f"entry_{i + 1}",
slug=f"slug-{i + 1}",
# We only get back the .data field, so let's put something useful in here to look for
data={"slug_for_test": f"slug-{i+1}-{locale}"},
data={"slug_for_test": f"slug-{i + 1}-{locale}"},
)

def _request(
Expand Down Expand Up @@ -501,10 +501,10 @@ def setUp(self):
classification=CONTENT_CLASSIFICATION_VPN,
locale=locale,
localisation_complete=True,
contentful_id=f"entry_{i+1}",
slug=f"slug-{i+1}",
contentful_id=f"entry_{i + 1}",
slug=f"slug-{i + 1}",
# We only get back the .data field, so let's put something useful in here to look for
data={"slug_for_test": f"slug-{i+1}-{locale}"},
data={"slug_for_test": f"slug-{i + 1}-{locale}"},
)

@patch("bedrock.products.views.l10n_utils.render", return_value=HttpResponse())
Expand Down
2 changes: 1 addition & 1 deletion bedrock/releasenotes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def release_notes(request, version, product="Firefox"):
"sort_num": 1,
"note": f'<a class="mdn-icon" rel="external" '
f'href="https://developer.mozilla.org/docs/Mozilla/Firefox/Releases/'
f'{ release.major_version }">Developer Information</a>',
f'{release.major_version}">Developer Information</a>',
},
)

Expand Down
9 changes: 5 additions & 4 deletions bedrock/security/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

from functools import total_ordering

from django.db import models
from django.template.defaultfilters import slugify
from django.utils.functional import total_ordering

from django_extensions.db.fields import ModificationDateTimeField
from django_extensions.db.fields.json import JSONField
Expand All @@ -26,6 +27,9 @@ class Meta:
def __str__(self):
return self.name

def __lt__(self, other):
return self.name_tuple < other.name_tuple

def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
# do not use self.name_tuple because don't want ".0" on versions.
product, vers = self.name_and_version
Expand Down Expand Up @@ -58,9 +62,6 @@ def html_id(self):
def version(self):
return self.name_tuple[1]

def __lt__(self, other):
return self.name_tuple < other.name_tuple


class SecurityAdvisory(models.Model):
id = models.CharField(max_length=8, primary_key=True, db_index=True)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.ruff]
line-length = 150
target-version = 'py39'
target-version = 'py312'
extend-exclude = ["docs", "assets", "static", "bedrock/externalfiles/files_cache"]

[tool.ruff.lint]
Expand Down
2 changes: 1 addition & 1 deletion requirements/dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pytest-parallel==0.1.1
pytest-rerunfailures==14.0
pytest-selenium==4.0.1
responses==0.25.6
ruff==0.8.6
ruff==0.9.1
selenium==4.9.1 # Pinned to 4.9.1 until https://github.com/pytest-dev/pytest-selenium/issues/315 is resolved
translate-toolkit==3.14.5
uv==0.5.18
Expand Down
38 changes: 19 additions & 19 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2055,25 +2055,25 @@ rsa==4.9 \
# via
# -r requirements/prod.txt
# google-auth
ruff==0.8.6 \
--hash=sha256:0509e8da430228236a18a677fcdb0c1f102dd26d5520f71f79b094963322ed25 \
--hash=sha256:0c000a471d519b3e6cfc9c6680025d923b4ca140ce3e4612d1a2ef58e11f11fe \
--hash=sha256:248b1fb3f739d01d528cc50b35ee9c4812aa58cc5935998e776bf8ed5b251e75 \
--hash=sha256:45a56f61b24682f6f6709636949ae8cc82ae229d8d773b4c76c09ec83964a95a \
--hash=sha256:496dd38a53aa173481a7d8866bcd6451bd934d06976a2505028a50583e001b76 \
--hash=sha256:52d587092ab8df308635762386f45f4638badb0866355b2b86760f6d3c076188 \
--hash=sha256:54799ca3d67ae5e0b7a7ac234baa657a9c1784b48ec954a094da7c206e0365b1 \
--hash=sha256:61323159cf21bc3897674e5adb27cd9e7700bab6b84de40d7be28c3d46dc67cf \
--hash=sha256:7ae4478b1471fc0c44ed52a6fb787e641a2ac58b1c1f91763bafbc2faddc5117 \
--hash=sha256:7d7fc2377a04b6e04ffe588caad613d0c460eb2ecba4c0ccbbfe2bc973cbc162 \
--hash=sha256:91a7ddb221779871cf226100e677b5ea38c2d54e9e2c8ed847450ebbdf99b32d \
--hash=sha256:9257aa841e9e8d9b727423086f0fa9a86b6b420fbf4bf9e1465d1250ce8e4d8d \
--hash=sha256:bc3c083c50390cf69e7e1b5a5a7303898966be973664ec0c4a4acea82c1d4315 \
--hash=sha256:dcad24b81b62650b0eb8814f576fc65cfee8674772a6e24c9b747911801eeaa5 \
--hash=sha256:defed167955d42c68b407e8f2e6f56ba52520e790aba4ca707a9c88619e580e3 \
--hash=sha256:e169ea1b9eae61c99b257dc83b9ee6c76f89042752cb2d83486a7d6e48e8f764 \
--hash=sha256:e88b8f6d901477c41559ba540beeb5a671e14cd29ebd5683903572f4b40a9807 \
--hash=sha256:f1d70bef3d16fdc897ee290d7d20da3cbe4e26349f62e8a0274e7a3f4ce7a905
ruff==0.9.1 \
--hash=sha256:186c2313de946f2c22bdf5954b8dd083e124bcfb685732cfb0beae0c47233d9b \
--hash=sha256:1cd76c7f9c679e6e8f2af8f778367dca82b95009bc7b1a85a47f1521ae524fa7 \
--hash=sha256:2f312c86fb40c5c02b44a29a750ee3b21002bd813b5233facdaf63a51d9a85e1 \
--hash=sha256:342a824b46ddbcdddd3abfbb332fa7fcaac5488bf18073e841236aadf4ad5c19 \
--hash=sha256:39d0174ccc45c439093971cc06ed3ac4dc545f5e8bdacf9f067adf879544d969 \
--hash=sha256:3cae39ba5d137054b0e5b472aee3b78a7c884e61591b100aeb544bcd1fc38d4f \
--hash=sha256:3f94942a3bb767675d9a051867c036655fe9f6c8a491539156a6f7e6b5f31831 \
--hash=sha256:46ebf5cc106cf7e7378ca3c28ce4293b61b449cd121b98699be727d40b79ba72 \
--hash=sha256:50c647ff96f4ba288db0ad87048257753733763b409b2faf2ea78b45c8bb7fcb \
--hash=sha256:5dc40a378a0e21b4cfe2b8a0f1812a6572fc7b230ef12cd9fac9161aa91d807f \
--hash=sha256:69572926c0f0c9912288915214ca9b2809525ea263603370b9e00bed2ba56dbd \
--hash=sha256:728d791b769cc28c05f12c280f99e8896932e9833fef1dd8756a6af2261fd1ab \
--hash=sha256:84330dda7abcc270e6055551aca93fdde1b0685fc4fd358f26410f9349cf1743 \
--hash=sha256:937267afce0c9170d6d29f01fcd1f4378172dec6760a9f4dface48cdabf9610a \
--hash=sha256:ae017c3a29bee341ba584f3823f805abbe5fe9cd97f87ed07ecbf533c4c88366 \
--hash=sha256:beb3298604540c884d8b282fe7625651378e1986c25df51dec5b2f60cafc31ce \
--hash=sha256:f0c8b149e9c7353cace7d698e1656ffcf1e36e50f8ea3b5d5f7f87ff9986a7ca \
--hash=sha256:fd2b25ecaf907d6458fa842675382c8597b3c746a2dde6717fe3415425df0c17
# via -r requirements/dev.in
s3transfer==0.10.4 \
--hash=sha256:244a76a24355363a68164241438de1b72f8781664920260c48465896b712a41e \
Expand Down
3 changes: 1 addition & 2 deletions tests/redirects/map_globalconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,7 @@
# bug 1090468
url_test(
"/security/{older-alerts,security-announcement,phishing-test{,-results}}.html",
"http://website-archive.mozilla.org/www.mozilla.org/security/security"
"/{older-alerts,security-announcement,phishing-test{,-results}}.html",
"http://website-archive.mozilla.org/www.mozilla.org/security/security/{older-alerts,security-announcement,phishing-test{,-results}}.html",
),
url_test(
"/security/iSECPartners_Phishing.pdf", "http://website-archive.mozilla.org/www.mozilla.org/security/security/iSECPartners_Phishing.pdf"
Expand Down

0 comments on commit eefdbfa

Please sign in to comment.