Skip to content

Commit 5315558

Browse files
authored
Use re.Pattern in type checks (#1018)
`re.Pattern` has bene available in Python for ages, but it was only documented in 3.11: https://docs.python.org/3.11/library/re.html#re.Pattern . Use it here to skip the import-time cost of compiling a dummy regex.
1 parent ac5601d commit 5315558

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

src/corsheaders/checks.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
from corsheaders.conf import conf
1212

13-
re_type = type(re.compile(""))
14-
1513

1614
def check_settings(**kwargs: Any) -> list[CheckMessage]:
1715
errors: list[CheckMessage] = []
@@ -122,7 +120,7 @@ def check_settings(**kwargs: Any) -> list[CheckMessage]:
122120
allowed_regexes_alias = "CORS_ALLOWED_ORIGIN_REGEXES"
123121
else:
124122
allowed_regexes_alias = "CORS_ORIGIN_REGEX_WHITELIST"
125-
if not is_sequence(conf.CORS_ALLOWED_ORIGIN_REGEXES, (str, re_type)):
123+
if not is_sequence(conf.CORS_ALLOWED_ORIGIN_REGEXES, (str, re.Pattern)):
126124
errors.append(
127125
Error(
128126
f"{allowed_regexes_alias} should be a sequence of strings and/or compiled regexes.",
@@ -135,8 +133,8 @@ def check_settings(**kwargs: Any) -> list[CheckMessage]:
135133
Error("CORS_EXPOSE_HEADERS should be a sequence.", id="corsheaders.E008")
136134
)
137135

138-
if not isinstance(conf.CORS_URLS_REGEX, (str, re_type)):
139-
errors.append(
136+
if not isinstance(conf.CORS_URLS_REGEX, (str, re.Pattern)):
137+
errors.append( # type: ignore [unreachable]
140138
Error("CORS_URLS_REGEX should be a string or regex.", id="corsheaders.E009")
141139
)
142140

0 commit comments

Comments
 (0)