summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Mueller <30130371+cdce8p@users.noreply.github.com>2021-05-03 00:35:11 +0200
committerMarc Mueller <30130371+cdce8p@users.noreply.github.com>2021-05-03 01:56:28 +0200
commitc098297fa5154fd18f81ae97feb06e84b0f6ba15 (patch)
tree50da82b08ffeb3fd3bf4e7a76d259ed25b3771e0
parent3d0ac73d8045b5fa771dbbf594ca0b9a4e581e15 (diff)
downloadpep8-c098297fa5154fd18f81ae97feb06e84b0f6ba15.tar.gz
Add whitespace checks for match and case
-rwxr-xr-xpycodestyle.py27
-rw-r--r--testsuite/python310.py18
-rw-r--r--testsuite/support.py7
3 files changed, 52 insertions, 0 deletions
diff --git a/pycodestyle.py b/pycodestyle.py
index 0f2f078..abadf6e 100755
--- a/pycodestyle.py
+++ b/pycodestyle.py
@@ -163,6 +163,7 @@ STARTSWITH_INDENT_STATEMENT_REGEX = re.compile(
)))
)
DUNDER_REGEX = re.compile(r'^__([^\s]+)__ = ')
+MATCH_CASE_REGEX = re.compile(r'^\s*\b(?:match|case)(\s*)(?=.*\:)')
_checks = {'physical_line': {}, 'logical_line': {}, 'tree': {}}
@@ -513,6 +514,32 @@ def missing_whitespace_after_import_keyword(logical_line):
@register_check
+def missing_whitespace_after_match_case(logical_line):
+ r"""Check whitespace after 'match' and 'case'.
+
+ Python 3.10
+ Okay: match status:
+ E271: match status:
+ E271: case\tstatus:
+ E271: case _:
+ E275: matchstatus:
+ E275: casestatus:
+ E275: case_:
+ """
+ if sys.version_info < (3, 10):
+ return
+ match = MATCH_CASE_REGEX.match(logical_line)
+ if match:
+ whitespace = match.groups()[0]
+ if whitespace == ' ':
+ return
+ if whitespace == '':
+ yield match.start(1), "E275 missing whitespace after keyword"
+ else:
+ yield match.start(1), "E271 multiple spaces after keyword"
+
+
+@register_check
def missing_whitespace(logical_line):
r"""Each comma, semicolon or colon should be followed by whitespace.
diff --git a/testsuite/python310.py b/testsuite/python310.py
index 83b7bb4..e78d372 100644
--- a/testsuite/python310.py
+++ b/testsuite/python310.py
@@ -7,3 +7,21 @@ match (var, var2):
pass
case _:
print("Default")
+#: E271:2:6 E271:3:9 E271:5:9 E271:7:9
+var = 1
+match var:
+ case 1:
+ pass
+ case 2:
+ pass
+ case (
+ 3
+ ):
+ pass
+#: E275:2:6 E275:3:9 E275:5:9
+var = 1
+match(var):
+ case(1):
+ pass
+ case_:
+ pass
diff --git a/testsuite/support.py b/testsuite/support.py
index eb8b443..e6c897f 100644
--- a/testsuite/support.py
+++ b/testsuite/support.py
@@ -6,6 +6,7 @@ import sys
from pycodestyle import Checker, BaseReport, StandardReport, readlines
SELFTEST_REGEX = re.compile(r'\b(Okay|[EW]\d{3}):\s(.*)')
+SELFTEST_PY_REGEX = re.compile(r'\bPython (\d).(\d+)')
ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
@@ -112,8 +113,14 @@ def selftest(options):
counters = report.counters
checks = options.physical_checks + options.logical_checks
for name, check, argument_names in checks:
+ python_version = None
for line in check.__doc__.splitlines():
line = line.lstrip()
+ match = SELFTEST_PY_REGEX.match(line)
+ if match:
+ python_version = tuple(map(int, match.groups()))
+ if python_version and sys.version_info < python_version:
+ continue
match = SELFTEST_REGEX.match(line)
if match is None:
continue