Skip to content

Commit

Permalink
Fix #11057: Update codebase to prefer python3 syntax (#11086)
Browse files Browse the repository at this point in the history
* Update `super()` calls

* Update for use f-strings

* Remove utf-8 comments

* Import from built-in `unittest.mock`

* Use native dict and set syntax

* Update exception errors

* Remove six compatibility code

* Remove explicit utf-8 encoding

* Use `yield from`
  • Loading branch information
robhudson committed Jan 11, 2022
1 parent 866e82c commit 2177e4d
Show file tree
Hide file tree
Showing 148 changed files with 291 additions and 383 deletions.
2 changes: 1 addition & 1 deletion bedrock/base/cache.py
Expand Up @@ -49,7 +49,7 @@ def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
def incr(self, key, delta=1, version=None):
value = self.get(key, version=version)
if value is None:
raise ValueError("Key '%s' not found" % key)
raise ValueError(f"Key '{key}' not found")
new_value = value + delta
key = self.make_key(key, version=version)
with self._lock:
Expand Down
2 changes: 1 addition & 1 deletion bedrock/base/log_settings.py
Expand Up @@ -38,7 +38,7 @@ def emit(self, record):
"prod": {
"()": commonware.log.Formatter,
"datefmt": "%H:%M:%s",
"format": "%s %s: [%%(REMOTE_ADDR)s] %s" % (hostname, settings.SYSLOG_TAG, base_fmt),
"format": f"{hostname} {settings.SYSLOG_TAG}: [%(REMOTE_ADDR)s] {base_fmt}",
},
"cef": {
"()": cef.SysLogFormatter,
Expand Down
4 changes: 2 additions & 2 deletions bedrock/base/management/commands/update_www_config.py
Expand Up @@ -16,7 +16,7 @@

def get_config_file_name(app_name=None):
app_name = app_name or settings.APP_NAME or "bedrock-dev"
return os.path.join(settings.WWW_CONFIG_PATH, "waffle_configs", "%s.env" % app_name)
return os.path.join(settings.WWW_CONFIG_PATH, "waffle_configs", f"{app_name}.env")


def get_config_values():
Expand Down Expand Up @@ -61,7 +61,7 @@ def handle(self, *args, **options):
count = refresh_db_values()

if count:
self.output("%s configs successfully loaded" % count)
self.output(f"{count} configs successfully loaded")
else:
self.output("No configs found. Please try again later.")

Expand Down
2 changes: 1 addition & 1 deletion bedrock/base/middleware.py
Expand Up @@ -105,7 +105,7 @@ def process_request(self, request):

response = HttpResponse(status=401, content="<h1>Unauthorized. This site is in private demo mode.</h1>")
realm = settings.APP_NAME or "bedrock-demo"
response["WWW-Authenticate"] = 'Basic realm="{}"'.format(realm)
response["WWW-Authenticate"] = f'Basic realm="{realm}"'
return response


Expand Down
2 changes: 0 additions & 2 deletions bedrock/base/migrations/0001_initial.py
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

# This Source Code Form is subject to the terms of the Mozilla Public
# 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/.
Expand Down
2 changes: 1 addition & 1 deletion bedrock/base/models.py
Expand Up @@ -13,7 +13,7 @@ class Meta:
app_label = "base"

def __str__(self):
return "%s=%s" % (self.name, self.value)
return f"{self.name}={self.value}"


def get_config_dict():
Expand Down
4 changes: 2 additions & 2 deletions bedrock/base/templatetags/helpers.py
Expand Up @@ -124,7 +124,7 @@ def js_bundle(name):
Bundles are defined in the "media/static-bundles.json" file.
"""
path = "js/{}.js".format(name)
path = f"js/{name}.js"
path = staticfiles_storage.url(path)
return jinja2.Markup(JS_TEMPLATE % path)

Expand All @@ -135,7 +135,7 @@ def css_bundle(name):
Bundles are defined in the "media/static-bundles.json" file.
"""
path = "css/{}.css".format(name)
path = f"css/{name}.css"
path = staticfiles_storage.url(path)
return jinja2.Markup(CSS_TEMPLATE % path)

Expand Down
3 changes: 2 additions & 1 deletion bedrock/base/tests/test_helpers.py
Expand Up @@ -2,10 +2,11 @@
# 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 unittest.mock import patch

from django.test import TestCase, override_settings

from django_jinja.backend import Jinja2
from mock import patch

from bedrock.base.templatetags import helpers

Expand Down
10 changes: 4 additions & 6 deletions bedrock/base/tests/test_simple_dict_cache.py
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

# This Source Code Form is subject to the terms of the Mozilla Public
# 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/.
Expand Down Expand Up @@ -33,7 +31,7 @@ def custom_key_func(key, key_prefix, version):

_caches_setting_base = {
"default": {},
"prefix": {"KEY_PREFIX": "cacheprefix{}".format(os.getpid())},
"prefix": {"KEY_PREFIX": f"cacheprefix{os.getpid()}"},
"v2": {"VERSION": 2},
"custom_key": {"KEY_FUNCTION": custom_key_func},
"custom_key2": {"KEY_FUNCTION": "bedrock.base.tests.test_simple_dict_cache.custom_key_func"},
Expand All @@ -49,7 +47,7 @@ def caches_setting_for_tests(base=None, **params):
# This results in the following search order:
# params -> _caches_setting_base -> base
base = base or {}
setting = dict((k, base.copy()) for k in _caches_setting_base.keys())
setting = {k: base.copy() for k in _caches_setting_base.keys()}
for key, cache_params in setting.items():
cache_params.update(_caches_setting_base[key])
cache_params.update(params)
Expand Down Expand Up @@ -324,11 +322,11 @@ def _perform_cull_test(self, cull_cache, initial_count, final_count):
# Create initial cache key entries. This will overflow the cache,
# causing a cull.
for i in range(1, initial_count):
cull_cache.set("cull%d" % i, "value", 1000)
cull_cache.set(f"cull{i}", "value", 1000)
count = 0
# Count how many keys are left in the cache.
for i in range(1, initial_count):
if "cull%d" % i in cull_cache: # noqa
if f"cull{i}" in cull_cache: # noqa
count = count + 1
self.assertEqual(count, final_count)

Expand Down
6 changes: 3 additions & 3 deletions bedrock/base/tests/test_urlresolvers.py
@@ -1,15 +1,15 @@
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# 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 unittest.mock import Mock, patch

from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from django.urls import re_path

import pytest
from mock import Mock, patch

from bedrock.base.urlresolvers import Prefixer, find_supported, reverse, split_path

Expand Down Expand Up @@ -47,7 +47,7 @@ class TestReverse(TestCase):
def test_unicode_url(self, get_url_prefix):
# If the prefixer returns a unicode URL it should be escaped and cast
# as a str object.
get_url_prefix.return_value = FakePrefixer(lambda p: "/Françoi%s" % p)
get_url_prefix.return_value = FakePrefixer(lambda p: f"/Françoi{p}")
result = reverse("test.view")

# Ensure that UTF-8 characters are escaped properly.
Expand Down
4 changes: 2 additions & 2 deletions bedrock/base/tests/test_waffle.py
Expand Up @@ -2,9 +2,9 @@
# 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 django.conf import settings
from unittest.mock import patch

from mock import patch
from django.conf import settings

from bedrock.base import waffle

Expand Down
3 changes: 2 additions & 1 deletion bedrock/base/tests/test_waffle_config.py
Expand Up @@ -2,8 +2,9 @@
# 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 unittest.mock import patch

from everett.manager import ConfigManager, ConfigurationMissingError
from mock import patch

from bedrock.base import waffle_config
from bedrock.mozorg.tests import TestCase
Expand Down
11 changes: 4 additions & 7 deletions bedrock/base/views.py
Expand Up @@ -52,10 +52,7 @@ def get_template_names(self):
GIT_SHA = getenv("GIT_SHA")
BUCKET_NAME = getenv("AWS_DB_S3_BUCKET", "bedrock-db-dev")
REGION_NAME = os.getenv("AWS_DB_REGION", "us-west-2")
S3_BASE_URL = "https://s3-{}.amazonaws.com/{}".format(
REGION_NAME,
BUCKET_NAME,
)
S3_BASE_URL = f"https://s3-{REGION_NAME}.amazonaws.com/{BUCKET_NAME}"


def get_l10n_repo_info():
Expand Down Expand Up @@ -84,17 +81,17 @@ def get_extra_server_info():
"git_sha": GIT_SHA,
}
try:
with open(DB_INFO_FILE, "r") as fp:
with open(DB_INFO_FILE) as fp:
db_info = json.load(fp)
except (IOError, ValueError):
except (OSError, ValueError):
pass
else:
last_updated_timestamp = datetime.fromtimestamp(db_info["updated"])
db_info["last_updated_timestamp"] = last_updated_timestamp
db_info["last_update"] = timeago.format(last_updated_timestamp)
db_info["file_url"] = get_db_file_url(db_info["file_name"])
for key, value in db_info.items():
server_info["db_%s" % key] = value
server_info[f"db_{key}"] = value

return server_info

Expand Down
5 changes: 2 additions & 3 deletions bedrock/careers/feeds.py
Expand Up @@ -16,9 +16,8 @@ class LatestPositionsFeed(Feed):
title = "Current Mozilla job openings"
description = "The current list of job openings, available internships and contract opportunities at Mozilla."
feed_copyright = (
"Portions of this content are ©1998–%s by individual "
"mozilla.org contributors. Content available under a "
"Creative Commons license." % date.today().year
f"Portions of this content are ©1998–{date.today().year} by individual "
"mozilla.org contributors. Content available under a Creative Commons license."
)

def link(self):
Expand Down
2 changes: 1 addition & 1 deletion bedrock/careers/forms.py
Expand Up @@ -13,7 +13,7 @@ class PositionFilterForm(forms.Form):
location = forms.ChoiceField(widget=forms.Select(attrs={"autocomplete": "off"}))

def __init__(self, *args, **kwargs):
super(PositionFilterForm, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)

# Populate position type choices dynamically.
locations = Position.locations()
Expand Down
4 changes: 2 additions & 2 deletions bedrock/careers/models.py
Expand Up @@ -29,7 +29,7 @@ class Meta:
)

def __str__(self):
return "{}@{}".format(self.job_id, self.source)
return f"{self.job_id}@{self.source}"

@property
def location_list(self):
Expand All @@ -45,7 +45,7 @@ def position_types(cls):
@classmethod
def locations(cls):
return sorted(
set(location.strip() for location in chain(*[locations.split(",") for locations in cls.objects.values_list("location", flat=True)]))
{location.strip() for location in chain(*[locations.split(",") for locations in cls.objects.values_list("location", flat=True)])}
)

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion bedrock/careers/tests/__init__.py
Expand Up @@ -26,5 +26,5 @@ class Meta:
@factory.lazy_attribute
def apply_url(self):
if self.source == "gh":
url = "https://boards.greenhouse.io/{}/jobs/{}".format(settings.GREENHOUSE_BOARD, self.job_id)
url = f"https://boards.greenhouse.io/{settings.GREENHOUSE_BOARD}/jobs/{self.job_id}"
return url.format(self.job_id)
Expand Up @@ -32,7 +32,7 @@ def handle(self, *args, **options):
self.output("Loading content cards into database")
count = ContentCard.objects.refresh()

self.output("%s content cards successfully loaded" % count)
self.output(f"{count} content cards successfully loaded")

repo.set_db_latest()

Expand Down
1 change: 0 additions & 1 deletion bedrock/contentcards/migrations/0001_initial.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# 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/.
Expand Down
16 changes: 8 additions & 8 deletions bedrock/contentcards/models.py
Expand Up @@ -24,7 +24,7 @@ def get_page_content_cards(page_name, locale):
def get_data_from_file_path(file_path):
card_name, locale = file_path.stem.split(".")
page_name = file_path.parts[-2]
page_id = "{}-{}-{}".format(page_name, locale, card_name)
page_id = f"{page_name}-{locale}-{card_name}"
return {
"locale": locale,
"card_name": card_name,
Expand All @@ -35,7 +35,7 @@ def get_data_from_file_path(file_path):

class ContentCardManager(models.Manager):
def get_card(self, page_name, name, locale="en-US"):
card_id = "{}-{}-{}".format(page_name, locale, name)
card_id = f"{page_name}-{locale}-{name}"
return self.get(id=card_id)

def get_page_cards(self, page_name, locale="en-US"):
Expand Down Expand Up @@ -82,7 +82,7 @@ class Meta:
ordering = ("id",)

def __str__(self):
return "{} ({})".format(self.card_name, self.locale)
return f"{self.card_name} ({self.locale})"

@property
def html(self):
Expand All @@ -94,24 +94,24 @@ def card_data(self):
data = {}
data.update(self.data)
if "image" in data:
data["image_url"] = "%scontentcards/img/%s" % (settings.CONTENT_CARDS_URL, data["image"])
data["image_url"] = f"{settings.CONTENT_CARDS_URL}contentcards/img/{data['image']}"
del data["image"]

if "highres_image" in data:
data["highres_image_url"] = "%scontentcards/img/%s" % (settings.CONTENT_CARDS_URL, data["highres_image"])
data["highres_image_url"] = f"{settings.CONTENT_CARDS_URL}contentcards/img/{data['highres_image']}"
del data["highres_image"]

if "ga_title" not in data:
data["ga_title"] = data["title"]

if "media_icon" in data:
data["media_icon"] = "mzp-has-%s" % data["media_icon"]
data["media_icon"] = f"mzp-has-{data['media_icon']}"

if "aspect_ratio" in data:
data["aspect_ratio"] = "mzp-has-aspect-%s" % data["aspect_ratio"]
data["aspect_ratio"] = f"mzp-has-aspect-{data['aspect_ratio']}"

if "size" in data:
data["class"] = "mzp-c-card-%s" % data["size"]
data["class"] = f"mzp-c-card-{data['size']}"
del data["size"]

if "link_url" in data and not URL_RE.match(data["link_url"]):
Expand Down
2 changes: 1 addition & 1 deletion bedrock/contentcards/tests/test_models.py
Expand Up @@ -78,7 +78,7 @@ def test_card_data(self):

def test_get_page_cards(self):
cards = models.ContentCard.objects.get_page_cards("home")
self.assertTrue(all(name in cards for name in ["card_%d" % i for i in range(1, 6)]))
self.assertTrue(all(name in cards for name in [f"card_{i}" for i in range(1, 6)]))
self.assertDictEqual(
cards["card_2"],
{
Expand Down
2 changes: 1 addition & 1 deletion bedrock/contentful/api.py
Expand Up @@ -294,7 +294,7 @@ def render(self, node):
cta_text = _make_plain_text(node)
data_cta = f' data-cta-type="link" data-cta-text="{cta_text}"'

return '<a href="{0}{1}"{2}{3}>{4}</a>'.format(urlunparse(url), ref, data_cta, rel, self._render_content(node))
return f'<a href="{urlunparse(url)}{ref}"{data_cta}{rel}>{self._render_content(node)}</a>'


def _render_list(tag, content):
Expand Down
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# 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/.
Expand Down
1 change: 0 additions & 1 deletion bedrock/contentful/tests/test_contentful_commands.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# 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/.
Expand Down
5 changes: 2 additions & 3 deletions bedrock/exp/tests/test_views.py
@@ -1,13 +1,12 @@
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# 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 unittest.mock import ANY, patch

from django.test import override_settings
from django.test.client import RequestFactory

from mock import ANY, patch

from bedrock.exp import views
from bedrock.mozorg.tests import TestCase

Expand Down

0 comments on commit 2177e4d

Please sign in to comment.