Skip to content

Commit 371da46

Browse files
authored
Bring codebase up to modern Python using pyupgrade (#213)
* Drop empty test_settings.py file Was emptied out back in e5dc7f6 * Remove redundant imports - via pyupgrade * Update dictionary-comprehension syntax - via pyupgrade * Update set() syntax - via pyupgrade * Update string formatting to f-strings - via pyupgrade * Update string formatting (but not to f-strings) - via pyupgrade * Drop redundant encoding comment - via pyupgrade * Update class def/inheritance - via pyupgrade * Update changelog
1 parent 9698258 commit 371da46

File tree

11 files changed

+16
-24
lines changed

11 files changed

+16
-24
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Next
1515
Unreleased
1616
==========
1717

18+
- Update Python syntax for modern versions with pyupgrade
1819
- Drop support for EOL Python <3.8 and Django <2.2 versions
1920
- Switch to ruff instead of pep8 and flake8
2021
- Move from CircleCI to Github Actions for CI

csp/decorators.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def _wrapped(*a, **kw):
1212

1313

1414
def csp_update(**kwargs):
15-
update = dict((k.lower().replace("_", "-"), v) for k, v in kwargs.items())
15+
update = {k.lower().replace("_", "-"): v for k, v in kwargs.items()}
1616

1717
def decorator(f):
1818
@wraps(f)
@@ -27,7 +27,7 @@ def _wrapped(*a, **kw):
2727

2828

2929
def csp_replace(**kwargs):
30-
replace = dict((k.lower().replace("_", "-"), v) for k, v in kwargs.items())
30+
replace = {k.lower().replace("_", "-"): v for k, v in kwargs.items()}
3131

3232
def decorator(f):
3333
@wraps(f)
@@ -42,7 +42,7 @@ def _wrapped(*a, **kw):
4242

4343

4444
def csp(**kwargs):
45-
config = dict((k.lower().replace("_", "-"), [v] if isinstance(v, str) else v) for k, v in kwargs.items())
45+
config = {k.lower().replace("_", "-"): [v] if isinstance(v, str) else v for k, v in kwargs.items()}
4646

4747
def decorator(f):
4848
@wraps(f)

csp/extensions/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import absolute_import
2-
31
from jinja2 import nodes
42
from jinja2.ext import Extension
53

@@ -8,7 +6,7 @@
86

97
class NoncedScript(Extension):
108
# a set of names that trigger the extension.
11-
tags = set(["script"])
9+
tags = {"script"}
1210

1311
def parse(self, parser):
1412
# the first token is the token that started the tag. In our case

csp/middleware.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import absolute_import
2-
31
import base64
42
import http.client as http_client
53
import os

csp/templatetags/csp.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import absolute_import
2-
31
from django import template
42
from django.template.base import token_kwargs
53

csp/tests/test_utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import absolute_import
2-
31
from django.conf import settings
42
from django.test.utils import override_settings
53
from django.utils.functional import lazy

csp/tests/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ def get_response(req):
2121
rf = RequestFactory()
2222

2323

24-
class ScriptTestBase(object):
24+
class ScriptTestBase:
2525
def assert_template_eq(self, tpl1, tpl2):
2626
aaa = tpl1.replace("\n", "").replace(" ", "")
2727
bbb = tpl2.replace("\n", "").replace(" ", "")
28-
assert aaa == bbb, "{} != {}".format(aaa, bbb)
28+
assert aaa == bbb, f"{aaa} != {bbb}"
2929

3030
def process_templates(self, tpl, expected):
3131
request = rf.get("/")

csp/utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ def build_policy(config=None, update=None, replace=None, nonce=None):
9999
include_nonce_in = getattr(settings, "CSP_INCLUDE_NONCE_IN", ["default-src"])
100100
for section in include_nonce_in:
101101
policy = policy_parts.get(section, "")
102-
policy_parts[section] = ("%s %s" % (policy, "'nonce-%s'" % nonce)).strip()
102+
policy_parts[section] = ("{} {}".format(policy, "'nonce-%s'" % nonce)).strip()
103103

104-
return "; ".join(["{} {}".format(k, val).strip() for k, val in policy_parts.items()])
104+
return "; ".join([f"{k} {val}".strip() for k, val in policy_parts.items()])
105105

106106

107107
def _default_attr_mapper(attr_name, val):
108108
if val:
109-
return ' {}="{}"'.format(attr_name, val)
109+
return f' {attr_name}="{val}"'
110110
else:
111111
return ""
112112

@@ -115,7 +115,7 @@ def _bool_attr_mapper(attr_name, val):
115115
# Only return the bare word if the value is truthy
116116
# ie - defer=False should actually return an empty string
117117
if val:
118-
return " {}".format(attr_name)
118+
return f" {attr_name}"
119119
else:
120120
return ""
121121

@@ -125,9 +125,9 @@ def _async_attr_mapper(attr_name, val):
125125
attributes. It can be set explicitly to `false` with no surrounding quotes
126126
according to the spec."""
127127
if val in [False, "False"]:
128-
return " {}=false".format(attr_name)
128+
return f" {attr_name}=false"
129129
elif val:
130-
return " {}".format(attr_name)
130+
return f" {attr_name}"
131131
else:
132132
return ""
133133

@@ -144,7 +144,7 @@ def _async_attr_mapper(attr_name, val):
144144
SCRIPT_ATTRS["nomodule"] = _bool_attr_mapper
145145

146146
# Generates an interpolatable string of valid attrs eg - '{nonce}{id}...'
147-
ATTR_FORMAT_STR = "".join(["{{{}}}".format(a) for a in SCRIPT_ATTRS])
147+
ATTR_FORMAT_STR = "".join([f"{{{a}}}" for a in SCRIPT_ATTRS])
148148

149149

150150
_script_tag_contents_re = re.compile(
@@ -176,4 +176,4 @@ def build_script_tag(content=None, **kwargs):
176176
# Don't render block contents if the script has a 'src' attribute
177177
c = _unwrap_script(content) if content and not kwargs.get("src") else ""
178178
attrs = ATTR_FORMAT_STR.format(**data).rstrip()
179-
return "<script{}>{}</script>".format(attrs, c).strip()
179+
return f"<script{attrs}>{c}</script>".strip()

docs/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
#
32
# Django-CSP documentation build configuration file, created by
43
# sphinx-quickstart on Wed Oct 31 13:02:27 2012.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
os.system("python setup.py sdist upload")
1212
os.system("python setup.py bdist_wheel upload")
1313
print("You probably want to also tag the version now:")
14-
print(' git tag -a %s -m "version %s"' % (version, version))
14+
print(f' git tag -a {version} -m "version {version}"')
1515
print(" git push --tags")
1616
sys.exit()
1717

test_settings.py

Whitespace-only changes.

0 commit comments

Comments
 (0)