Skip to content

Commit 808c8d7

Browse files
authored
Add pyupgrade and django-upgrade pre-commit hooks (#1553)
1 parent e3d3828 commit 808c8d7

File tree

11 files changed

+29
-19
lines changed

11 files changed

+29
-19
lines changed

.pre-commit-config.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ repos:
1414
rev: 0.10.1
1515
hooks:
1616
- id: doc8
17+
- repo: https://github.com/asottile/pyupgrade
18+
rev: v2.29.1
19+
hooks:
20+
- id: pyupgrade
21+
args: [--py36-plus]
22+
- repo: https://github.com/adamchainz/django-upgrade
23+
rev: 1.4.0
24+
hooks:
25+
- id: django-upgrade
26+
args: [--target-version, "3.2"]
1727
- repo: https://github.com/pycqa/isort
1828
rev: 5.10.1
1929
hooks:

debug_toolbar/management/commands/debugsqlshell.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def execute(self, sql, params=()):
2727
end_time = time()
2828
duration = (end_time - start_time) * 1000
2929
formatted_sql = sqlparse.format(raw_sql, reindent=True)
30-
print("{} [{:.2f}ms]".format(formatted_sql, duration))
30+
print(f"{formatted_sql} [{duration:.2f}ms]")
3131

3232

3333
base_module.CursorDebugWrapper = PrintQueryWrapper

debug_toolbar/panels/cache.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def __init__(self, cache):
6363
self.cache = cache
6464

6565
def __repr__(self):
66-
return str("<CacheStatTracker for %s>") % repr(self.cache)
66+
return "<CacheStatTracker for %s>" % repr(self.cache)
6767

6868
def _get_func_info(self):
6969
frame = sys._getframe(3)

debug_toolbar/panels/profiling.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def parent_classes(self):
3030

3131
def background(self):
3232
r, g, b = hsv_to_rgb(*self.hsv)
33-
return "rgb({:f}%,{:f}%,{:f}%)".format(r * 100, g * 100, b * 100)
33+
return f"rgb({r * 100:f}%,{g * 100:f}%,{b * 100:f}%)"
3434

3535
def func_std_string(self): # match what old profile produced
3636
func_name = self.func

debug_toolbar/panels/signals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def generate_stats(self, request, response):
9797
receiver_class_name = getattr(
9898
receiver.__self__, "__class__", type
9999
).__name__
100-
text = "{}.{}".format(receiver_class_name, receiver_name)
100+
text = f"{receiver_class_name}.{receiver_name}"
101101
else:
102102
text = receiver_name
103103
receivers.append(text)

debug_toolbar/panels/sql/utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def reformat_sql(sql, with_toggle=False):
2626
if not with_toggle:
2727
return formatted
2828
simple = simplify(parse_sql(sql, aligned_indent=False))
29-
uncollapsed = '<span class="djDebugUncollapsed">{}</span>'.format(simple)
30-
collapsed = '<span class="djDebugCollapsed djdt-hidden">{}</span>'.format(formatted)
29+
uncollapsed = f'<span class="djDebugUncollapsed">{simple}</span>'
30+
collapsed = f'<span class="djDebugCollapsed djdt-hidden">{formatted}</span>'
3131
return collapsed + uncollapsed
3232

3333

debug_toolbar/panels/sql/views.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ def sql_explain(request, verified_data):
4949
# SQLite's EXPLAIN dumps the low-level opcodes generated for a query;
5050
# EXPLAIN QUERY PLAN dumps a more human-readable summary
5151
# See https://www.sqlite.org/lang_explain.html for details
52-
cursor.execute("EXPLAIN QUERY PLAN {}".format(sql), params)
52+
cursor.execute(f"EXPLAIN QUERY PLAN {sql}", params)
5353
elif vendor == "postgresql":
54-
cursor.execute("EXPLAIN ANALYZE {}".format(sql), params)
54+
cursor.execute(f"EXPLAIN ANALYZE {sql}", params)
5555
else:
56-
cursor.execute("EXPLAIN {}".format(sql), params)
56+
cursor.execute(f"EXPLAIN {sql}", params)
5757
headers = [d[0] for d in cursor.description]
5858
result = cursor.fetchall()
5959

debug_toolbar/panels/templates/panel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _request_context_bind_template(self, template):
4242
self.context_processors = OrderedDict()
4343
updates = {}
4444
for processor in processors:
45-
name = "{}.{}".format(processor.__module__, processor.__name__)
45+
name = f"{processor.__module__}.{processor.__name__}"
4646
context = processor(self.request)
4747
self.context_processors[name] = context
4848
updates.update(context)

debug_toolbar/panels/templates/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def template_source(request):
4444
except TemplateDoesNotExist:
4545
pass
4646
else:
47-
source = "Template Does Not Exist: {}".format(template_origin_name)
47+
source = f"Template Does Not Exist: {template_origin_name}"
4848

4949
try:
5050
from pygments import highlight

debug_toolbar/utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def get_module_path(module_name):
2727
try:
2828
module = import_module(module_name)
2929
except ImportError as e:
30-
raise ImproperlyConfigured("Error importing HIDE_IN_STACKTRACES: {}".format(e))
30+
raise ImproperlyConfigured(f"Error importing HIDE_IN_STACKTRACES: {e}")
3131
else:
3232
source_path = inspect.getsourcefile(module)
3333
if source_path.endswith("__init__.py"):
@@ -157,7 +157,7 @@ def get_name_from_obj(obj):
157157

158158
if hasattr(obj, "__module__"):
159159
module = obj.__module__
160-
name = "{}.{}".format(module, name)
160+
name = f"{module}.{name}"
161161

162162
return name
163163

tests/panels/test_sql.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ def test_param_conversion(self):
133133
# comparisons in MySQL.
134134
if not (django.VERSION >= (4, 1) and connection.vendor == "mysql"):
135135
self.assertEqual(
136-
tuple([q[1]["params"] for q in self.panel._queries]),
136+
tuple(q[1]["params"] for q in self.panel._queries),
137137
('["Foo"]', "[10, 1]", '["2017-12-22 16:07:01"]'),
138138
)
139139
else:
140140
self.assertEqual(
141-
tuple([q[1]["params"] for q in self.panel._queries]),
141+
tuple(q[1]["params"] for q in self.panel._queries),
142142
('["Foo", true, false]', "[10, 1]", '["2017-12-22 16:07:01"]'),
143143
)
144144

@@ -227,7 +227,7 @@ def test_raw_query_param_conversion(self):
227227
self.assertEqual(len(self.panel._queries), 2)
228228

229229
self.assertEqual(
230-
tuple([q[1]["params"] for q in self.panel._queries]),
230+
tuple(q[1]["params"] for q in self.panel._queries),
231231
(
232232
'["Foo", true, false, "2017-12-22 16:07:01"]',
233233
" ".join(
@@ -246,7 +246,7 @@ def test_insert_content(self):
246246
Test that the panel only inserts content after generate_stats and
247247
not the process_request.
248248
"""
249-
list(User.objects.filter(username="café".encode("utf-8")))
249+
list(User.objects.filter(username="café".encode()))
250250
response = self.panel.process_request(self.request)
251251
# ensure the panel does not have content yet.
252252
self.assertNotIn("café", self.panel.content)
@@ -262,7 +262,7 @@ def test_insert_locals(self):
262262
Test that the panel inserts locals() content.
263263
"""
264264
local_var = "<script>alert('test');</script>" # noqa: F841
265-
list(User.objects.filter(username="café".encode("utf-8")))
265+
list(User.objects.filter(username="café".encode()))
266266
response = self.panel.process_request(self.request)
267267
self.panel.generate_stats(self.request, response)
268268
self.assertIn("local_var", self.panel.content)
@@ -276,7 +276,7 @@ def test_not_insert_locals(self):
276276
"""
277277
Test that the panel does not insert locals() content.
278278
"""
279-
list(User.objects.filter(username="café".encode("utf-8")))
279+
list(User.objects.filter(username="café".encode()))
280280
response = self.panel.process_request(self.request)
281281
self.panel.generate_stats(self.request, response)
282282
self.assertNotIn("djdt-locals", self.panel.content)

0 commit comments

Comments
 (0)