Skip to content

Commit c098297

Browse files
committed
Add whitespace checks for match and case
1 parent 3d0ac73 commit c098297

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

pycodestyle.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def lru_cache(maxsize=128): # noqa as it's a fake implementation.
163163
)))
164164
)
165165
DUNDER_REGEX = re.compile(r'^__([^\s]+)__ = ')
166+
MATCH_CASE_REGEX = re.compile(r'^\s*\b(?:match|case)(\s*)(?=.*\:)')
166167

167168
_checks = {'physical_line': {}, 'logical_line': {}, 'tree': {}}
168169

@@ -512,6 +513,32 @@ def missing_whitespace_after_import_keyword(logical_line):
512513
yield pos, "E275 missing whitespace after keyword"
513514

514515

516+
@register_check
517+
def missing_whitespace_after_match_case(logical_line):
518+
r"""Check whitespace after 'match' and 'case'.
519+
520+
Python 3.10
521+
Okay: match status:
522+
E271: match status:
523+
E271: case\tstatus:
524+
E271: case _:
525+
E275: matchstatus:
526+
E275: casestatus:
527+
E275: case_:
528+
"""
529+
if sys.version_info < (3, 10):
530+
return
531+
match = MATCH_CASE_REGEX.match(logical_line)
532+
if match:
533+
whitespace = match.groups()[0]
534+
if whitespace == ' ':
535+
return
536+
if whitespace == '':
537+
yield match.start(1), "E275 missing whitespace after keyword"
538+
else:
539+
yield match.start(1), "E271 multiple spaces after keyword"
540+
541+
515542
@register_check
516543
def missing_whitespace(logical_line):
517544
r"""Each comma, semicolon or colon should be followed by whitespace.

testsuite/python310.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,21 @@
77
pass
88
case _:
99
print("Default")
10+
#: E271:2:6 E271:3:9 E271:5:9 E271:7:9
11+
var = 1
12+
match var:
13+
case 1:
14+
pass
15+
case 2:
16+
pass
17+
case (
18+
3
19+
):
20+
pass
21+
#: E275:2:6 E275:3:9 E275:5:9
22+
var = 1
23+
match(var):
24+
case(1):
25+
pass
26+
case_:
27+
pass

testsuite/support.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pycodestyle import Checker, BaseReport, StandardReport, readlines
77

88
SELFTEST_REGEX = re.compile(r'\b(Okay|[EW]\d{3}):\s(.*)')
9+
SELFTEST_PY_REGEX = re.compile(r'\bPython (\d).(\d+)')
910
ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
1011

1112

@@ -112,8 +113,14 @@ def selftest(options):
112113
counters = report.counters
113114
checks = options.physical_checks + options.logical_checks
114115
for name, check, argument_names in checks:
116+
python_version = None
115117
for line in check.__doc__.splitlines():
116118
line = line.lstrip()
119+
match = SELFTEST_PY_REGEX.match(line)
120+
if match:
121+
python_version = tuple(map(int, match.groups()))
122+
if python_version and sys.version_info < python_version:
123+
continue
117124
match = SELFTEST_REGEX.match(line)
118125
if match is None:
119126
continue

0 commit comments

Comments
 (0)