Skip to content

Commit

Permalink
Activate flake8-simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Jun 5, 2023
1 parent fcfdc7c commit ebd6ea3
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 29 deletions.
5 changes: 1 addition & 4 deletions debug_toolbar/panels/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ def _store_call_info(
else:
self.hits += 1
elif name == "get_many":
if "keys" in kwargs:
keys = kwargs["keys"]
else:
keys = args[0]
keys = kwargs["keys"] if "keys" in kwargs else args[0]
self.hits += len(return_value)
self.misses += len(keys) - len(return_value)
time_taken *= 1000
Expand Down
6 changes: 3 additions & 3 deletions debug_toolbar/panels/history/panel.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import json

from django.http.request import RawPostDataException
Expand Down Expand Up @@ -62,10 +63,9 @@ def generate_stats(self, request, response):
and request.body
and request.headers.get("content-type") == "application/json"
):
try:
with contextlib.suppress(ValueError):
data = json.loads(request.body)
except ValueError:
pass

except RawPostDataException:
# It is not guaranteed that we may read the request data (again).
data = None
Expand Down
5 changes: 1 addition & 4 deletions debug_toolbar/panels/profiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ def subfuncs(self):
for func, stats in self.statobj.all_callees[self.func].items():
i += 1
h1 = h + (i / count) / (self.depth + 1)
if stats[3] == 0:
s1 = 0
else:
s1 = s * (stats[3] / self.stats[3])
s1 = 0 if stats[3] == 0 else s * (stats[3] / self.stats[3])
yield FunctionCall(
self.statobj,
func,
Expand Down
4 changes: 1 addition & 3 deletions debug_toolbar/panels/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,5 @@ def generate_stats(self, request, response):
(k, request.session.get(k)) for k in sorted(request.session.keys())
]
except TypeError:
session_list = [
(k, request.session.get(k)) for k in request.session.keys()
]
session_list = [(k, request.session.get(k)) for k in request.session]
self.record_stats({"session": {"list": session_list}})
11 changes: 4 additions & 7 deletions debug_toolbar/panels/sql/tracking.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import contextvars
import datetime
import json
Expand Down Expand Up @@ -59,10 +60,7 @@ def cursor(*args, **kwargs):
cursor = connection._djdt_cursor(*args, **kwargs)
if logger is None:
return cursor
if allow_sql.get():
wrapper = NormalCursorWrapper
else:
wrapper = ExceptionCursorWrapper
wrapper = NormalCursorWrapper if allow_sql.get() else ExceptionCursorWrapper
return wrapper(cursor.cursor, connection, logger)

def chunked_cursor(*args, **kwargs):
Expand Down Expand Up @@ -174,10 +172,9 @@ def _record(self, method, sql, params):
stop_time = perf_counter()
duration = (stop_time - start_time) * 1000
_params = ""
try:
with contextlib.suppress(TypeError):
# object JSON serializable?
_params = json.dumps(self._decode(params))
except TypeError:
pass # object not JSON serializable
template_info = get_template_info()

# Sql might be an object (such as psycopg Composed).
Expand Down
5 changes: 1 addition & 4 deletions debug_toolbar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,7 @@ def get_template_source_from_exception_info(


def get_name_from_obj(obj: Any) -> str:
if hasattr(obj, "__name__"):
name = obj.__name__
else:
name = obj.__class__.__name__
name = obj.__name__ if hasattr(obj, "__name__") else obj.__class__.__name__

if hasattr(obj, "__module__"):
module = obj.__module__
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ extend-select = [
# flake8-pie
"PIE",
# flake8-simplify
# "SIM",
"SIM",
# flake8-gettext
"INT",
# pygrep-hooks
Expand Down
5 changes: 2 additions & 3 deletions tests/panels/test_profiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ def test_view_executed_once(self):
self.assertContains(response, "Profiling")
self.assertEqual(User.objects.count(), 1)

with self.assertRaises(IntegrityError):
with transaction.atomic():
response = self.client.get("/new_user/")
with self.assertRaises(IntegrityError), transaction.atomic():
response = self.client.get("/new_user/")
self.assertEqual(User.objects.count(), 1)

0 comments on commit ebd6ea3

Please sign in to comment.