Skip to content

Commit 3ee10e8

Browse files
committed
Remove support for Python 2
- Removed use of six - Removed __future__ imports - Removed coding cookie comment (Python 3 defaults to utf-8) - Removed some force_text() calls where the value is known to be str - Replaced deprecated ugettext with gettext - Removed object as parent class - Simplified use of super() - Updated setup.cfg file to reflect that without Python 2 support, the wheel is no longer "universal" - Updated trove classifiers - Consistently import ngettext as __ throughout the project - Run pyupgrade on the project
1 parent d44e5f9 commit 3ee10e8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+124
-277
lines changed

.travis.yml

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ cache: pip
55
matrix:
66
fast_finish: true
77
include:
8-
- python: 2.7
9-
env: TOXENV=py27-dj111
108
- python: 3.4
119
env: TOXENV=py34-dj111
1210
- python: 3.5

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ style:
77

88
style_check:
99
isort -rc -c debug_toolbar example tests
10-
black --check debug_toolbar example tests setup.py
10+
black --target-version=py34 --check debug_toolbar example tests setup.py
1111

1212
flake8:
1313
flake8 debug_toolbar example tests

debug_toolbar/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import absolute_import, unicode_literals
2-
31
__all__ = ["VERSION"]
42

53

debug_toolbar/apps.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
from __future__ import absolute_import, unicode_literals
2-
31
import inspect
42

53
from django.apps import AppConfig
64
from django.conf import settings
75
from django.core.checks import Warning, register
86
from django.middleware.gzip import GZipMiddleware
97
from django.utils.module_loading import import_string
10-
from django.utils.translation import ugettext_lazy as _
8+
from django.utils.translation import gettext_lazy as _
119

1210

1311
class DebugToolbarConfig(AppConfig):

debug_toolbar/management/commands/debugsqlshell.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import absolute_import, print_function, unicode_literals
2-
31
from time import time
42

53
import sqlparse
@@ -19,7 +17,7 @@ def execute(self, sql, params=()):
1917
end_time = time()
2018
duration = (end_time - start_time) * 1000
2119
formatted_sql = sqlparse.format(raw_sql, reindent=True)
22-
print("%s [%.2fms]" % (formatted_sql, duration))
20+
print("{} [{:.2f}ms]".format(formatted_sql, duration))
2321

2422

2523
db_backends_utils.CursorDebugWrapper = PrintQueryWrapper

debug_toolbar/middleware.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22
Debug Toolbar middleware
33
"""
44

5-
from __future__ import absolute_import, unicode_literals
6-
75
import re
6+
from functools import lru_cache
87

98
from django.conf import settings
10-
from django.utils import six
11-
from django.utils.encoding import force_text
12-
from django.utils.lru_cache import lru_cache
139
from django.utils.module_loading import import_string
1410

1511
from debug_toolbar import settings as dt_settings
@@ -33,13 +29,13 @@ def get_show_toolbar():
3329
# If SHOW_TOOLBAR_CALLBACK is a string, which is the recommended
3430
# setup, resolve it to the corresponding callable.
3531
func_or_path = dt_settings.get_config()["SHOW_TOOLBAR_CALLBACK"]
36-
if isinstance(func_or_path, six.string_types):
32+
if isinstance(func_or_path, str):
3733
return import_string(func_or_path)
3834
else:
3935
return func_or_path
4036

4137

42-
class DebugToolbarMiddleware(object):
38+
class DebugToolbarMiddleware:
4339
"""
4440
Middleware to set up Debug Toolbar on incoming request and render toolbar
4541
on outgoing response.
@@ -86,7 +82,7 @@ def __call__(self, request):
8682
response.set_cookie("djdt", "hide", 864000)
8783

8884
# Insert the toolbar in the response.
89-
content = force_text(response.content, encoding=response.charset)
85+
content = response.content.decode(response.charset)
9086
insert_before = dt_settings.get_config()["INSERT_BEFORE"]
9187
pattern = re.escape(insert_before)
9288
bits = re.split(pattern, content, flags=re.IGNORECASE)

debug_toolbar/panels/__init__.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
from __future__ import absolute_import, unicode_literals
2-
31
from django.template.loader import render_to_string
42

53
from debug_toolbar import settings as dt_settings
64
from debug_toolbar.utils import get_name_from_obj
75

86

9-
class Panel(object):
7+
class Panel:
108
"""
119
Base class for panels.
1210
"""
@@ -153,7 +151,7 @@ def record_server_timing(self, key, title, value):
153151
154152
Each call to ``record_stats`` updates the statistics dictionary.
155153
"""
156-
data = {key: dict(title=title, value=value)}
154+
data = {key: {"title": title, "value": value}}
157155
self.toolbar.server_timing_stats.setdefault(self.panel_id, {}).update(data)
158156

159157
def get_server_timing_stats(self):

debug_toolbar/panels/cache.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import absolute_import, unicode_literals
2-
31
import inspect
42
import sys
53
import time
@@ -11,7 +9,7 @@
119
from django.core.cache.backends.base import BaseCache
1210
from django.dispatch import Signal
1311
from django.middleware import cache as middleware_cache
14-
from django.utils.translation import ugettext_lazy as _, ungettext
12+
from django.utils.translation import gettext_lazy as _, ngettext as __
1513

1614
from debug_toolbar import settings as dt_settings
1715
from debug_toolbar.panels import Panel
@@ -132,7 +130,7 @@ def decr_version(self, *args, **kwargs):
132130

133131
class CacheHandlerPatch(CacheHandler):
134132
def __getitem__(self, alias):
135-
actual_cache = super(CacheHandlerPatch, self).__getitem__(alias)
133+
actual_cache = super().__getitem__(alias)
136134
return CacheStatTracker(actual_cache)
137135

138136

@@ -147,7 +145,7 @@ class CachePanel(Panel):
147145
template = "debug_toolbar/panels/cache.html"
148146

149147
def __init__(self, *args, **kwargs):
150-
super(CachePanel, self).__init__(*args, **kwargs)
148+
super().__init__(*args, **kwargs)
151149
self.total_time = 0
152150
self.hits = 0
153151
self.misses = 0
@@ -218,7 +216,7 @@ def _store_call_info(
218216
@property
219217
def nav_subtitle(self):
220218
cache_calls = len(self.calls)
221-
return ungettext(
219+
return __(
222220
"%(cache_calls)d call in %(time).2fms",
223221
"%(cache_calls)d calls in %(time).2fms",
224222
cache_calls,
@@ -227,11 +225,11 @@ def nav_subtitle(self):
227225
@property
228226
def title(self):
229227
count = len(getattr(settings, "CACHES", ["default"]))
230-
return ungettext(
228+
return __(
231229
"Cache calls from %(count)d backend",
232230
"Cache calls from %(count)d backends",
233231
count,
234-
) % dict(count=count)
232+
) % {"count": count}
235233

236234
def enable_instrumentation(self):
237235
if isinstance(middleware_cache.caches, CacheHandlerPatch):

debug_toolbar/panels/headers.py

+20-24
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from __future__ import absolute_import, unicode_literals
2-
31
from collections import OrderedDict
42

5-
from django.utils.translation import ugettext_lazy as _
3+
from django.utils.translation import gettext_lazy as _
64

75
from debug_toolbar.panels import Panel
86

@@ -13,26 +11,24 @@ class HeadersPanel(Panel):
1311
"""
1412

1513
# List of environment variables we want to display
16-
ENVIRON_FILTER = set(
17-
(
18-
"CONTENT_LENGTH",
19-
"CONTENT_TYPE",
20-
"DJANGO_SETTINGS_MODULE",
21-
"GATEWAY_INTERFACE",
22-
"QUERY_STRING",
23-
"PATH_INFO",
24-
"PYTHONPATH",
25-
"REMOTE_ADDR",
26-
"REMOTE_HOST",
27-
"REQUEST_METHOD",
28-
"SCRIPT_NAME",
29-
"SERVER_NAME",
30-
"SERVER_PORT",
31-
"SERVER_PROTOCOL",
32-
"SERVER_SOFTWARE",
33-
"TZ",
34-
)
35-
)
14+
ENVIRON_FILTER = {
15+
"CONTENT_LENGTH",
16+
"CONTENT_TYPE",
17+
"DJANGO_SETTINGS_MODULE",
18+
"GATEWAY_INTERFACE",
19+
"QUERY_STRING",
20+
"PATH_INFO",
21+
"PYTHONPATH",
22+
"REMOTE_ADDR",
23+
"REMOTE_HOST",
24+
"REQUEST_METHOD",
25+
"SCRIPT_NAME",
26+
"SERVER_NAME",
27+
"SERVER_PORT",
28+
"SERVER_PROTOCOL",
29+
"SERVER_SOFTWARE",
30+
"TZ",
31+
}
3632

3733
title = _("Headers")
3834

@@ -51,7 +47,7 @@ def process_request(self, request):
5147
self.record_stats(
5248
{"request_headers": self.request_headers, "environ": self.environ}
5349
)
54-
return super(HeadersPanel, self).process_request(request)
50+
return super().process_request(request)
5551

5652
def generate_stats(self, request, response):
5753
self.response_headers = OrderedDict(sorted(response.items()))

debug_toolbar/panels/logging.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
from __future__ import absolute_import, unicode_literals
2-
31
import datetime
42
import logging
53

6-
from django.utils.translation import ugettext_lazy as _, ungettext
4+
from django.utils.translation import gettext_lazy as _, ngettext as __
75

86
from debug_toolbar.panels import Panel
97
from debug_toolbar.utils import ThreadCollector
@@ -22,7 +20,7 @@ def collect(self, item, thread=None):
2220
# TODO: Make this check whether SQL panel is enabled
2321
if item.get("channel", "") == "django.db.backends":
2422
return
25-
super(LogCollector, self).collect(item, thread)
23+
super().collect(item, thread)
2624

2725

2826
class ThreadTrackingHandler(logging.Handler):
@@ -59,7 +57,7 @@ class LoggingPanel(Panel):
5957
template = "debug_toolbar/panels/logging.html"
6058

6159
def __init__(self, *args, **kwargs):
62-
super(LoggingPanel, self).__init__(*args, **kwargs)
60+
super().__init__(*args, **kwargs)
6361
self._records = {}
6462

6563
nav_title = _("Logging")
@@ -68,15 +66,15 @@ def __init__(self, *args, **kwargs):
6866
def nav_subtitle(self):
6967
records = self._records[threading.currentThread()]
7068
record_count = len(records)
71-
return ungettext("%(count)s message", "%(count)s messages", record_count) % {
69+
return __("%(count)s message", "%(count)s messages", record_count) % {
7270
"count": record_count
7371
}
7472

7573
title = _("Log messages")
7674

7775
def process_request(self, request):
7876
collector.clear_collection()
79-
return super(LoggingPanel, self).process_request(request)
77+
return super().process_request(request)
8078

8179
def generate_stats(self, request, response):
8280
records = collector.get_collection()

debug_toolbar/panels/profiling.py

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
from __future__ import absolute_import, division, unicode_literals
2-
31
import cProfile
42
import os
53
from colorsys import hsv_to_rgb
64
from pstats import Stats
75

8-
from django.utils import six
96
from django.utils.html import format_html
10-
from django.utils.translation import ugettext_lazy as _
7+
from django.utils.translation import gettext_lazy as _
118

129
from debug_toolbar import settings as dt_settings
1310
from debug_toolbar.panels import Panel
@@ -23,7 +20,7 @@ def contains_profiler(func_tuple):
2320
the INVALID_PROFILE_FUNC in any string value of the tuple."""
2421
has_profiler = False
2522
for value in func_tuple:
26-
if isinstance(value, six.string_types):
23+
if isinstance(value, str):
2724
has_profiler |= INVALID_PROFILER_FUNC in value
2825
return has_profiler
2926

@@ -40,7 +37,7 @@ def get_root_func(self):
4037
return self.__root
4138

4239

43-
class FunctionCall(object):
40+
class FunctionCall:
4441
def __init__(
4542
self, statobj, func, depth=0, stats=None, id=0, parent_ids=[], hsv=(0, 0.5, 1)
4643
):
@@ -60,7 +57,7 @@ def parent_classes(self):
6057

6158
def background(self):
6259
r, g, b = hsv_to_rgb(*self.hsv)
63-
return "rgb(%f%%,%f%%,%f%%)" % (r * 100, g * 100, b * 100)
60+
return "rgb({:f}%,{:f}%,{:f}%)".format(r * 100, g * 100, b * 100)
6461

6562
def func_std_string(self): # match what old profile produced
6663
func_name = self.func
@@ -156,9 +153,7 @@ class ProfilingPanel(Panel):
156153

157154
def process_request(self, request):
158155
self.profiler = cProfile.Profile()
159-
return self.profiler.runcall(
160-
super(ProfilingPanel, self).process_request, request
161-
)
156+
return self.profiler.runcall(super().process_request, request)
162157

163158
def add_node(self, func_list, func, max_depth, cum_time=0.1):
164159
func_list.append(func)

debug_toolbar/panels/redirects.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
from __future__ import absolute_import, unicode_literals
2-
31
from django.template.response import SimpleTemplateResponse
4-
from django.utils.translation import ugettext_lazy as _
2+
from django.utils.translation import gettext_lazy as _
53

64
from debug_toolbar.panels import Panel
75

@@ -16,11 +14,13 @@ class RedirectsPanel(Panel):
1614
nav_title = _("Intercept redirects")
1715

1816
def process_request(self, request):
19-
response = super(RedirectsPanel, self).process_request(request)
17+
response = super().process_request(request)
2018
if 300 <= int(response.status_code) < 400:
2119
redirect_to = response.get("Location", None)
2220
if redirect_to:
23-
status_line = "%s %s" % (response.status_code, response.reason_phrase)
21+
status_line = "{} {}".format(
22+
response.status_code, response.reason_phrase
23+
)
2424
cookies = response.cookies
2525
context = {"redirect_to": redirect_to, "status_line": status_line}
2626
# Using SimpleTemplateResponse avoids running global context processors.

debug_toolbar/panels/request.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
from __future__ import absolute_import, unicode_literals
2-
31
from django.http import Http404
42
from django.urls import resolve
5-
from django.utils.encoding import force_text
6-
from django.utils.translation import ugettext_lazy as _
3+
from django.utils.translation import gettext_lazy as _
74

85
from debug_toolbar.panels import Panel
96
from debug_toolbar.utils import get_name_from_obj
@@ -58,7 +55,7 @@ def generate_stats(self, request, response):
5855
{
5956
"session": [
6057
(k, request.session.get(k))
61-
for k in sorted(request.session.keys(), key=force_text)
58+
for k in sorted(request.session.keys())
6259
]
6360
}
6461
)

debug_toolbar/panels/settings.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
from __future__ import absolute_import, unicode_literals
2-
31
from collections import OrderedDict
42

53
from django.conf import settings
6-
from django.utils.translation import ugettext_lazy as _
4+
from django.utils.translation import gettext_lazy as _
75
from django.views.debug import get_safe_settings
86

97
from debug_toolbar.panels import Panel

0 commit comments

Comments
 (0)