Skip to content

Commit 0f079a0

Browse files
authored
Merge pull request #990 from cdce8p/whitespace-match-case
Add whitespace checks for ``match`` and ``case``
2 parents 3d0ac73 + 3527106 commit 0f079a0

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

pycodestyle.py

Lines changed: 11 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

@@ -493,6 +494,16 @@ def whitespace_around_keywords(logical_line):
493494
elif len(after) > 1:
494495
yield match.start(2), "E271 multiple spaces after keyword"
495496

497+
if sys.version_info >= (3, 10):
498+
match = MATCH_CASE_REGEX.match(logical_line)
499+
if match:
500+
if match[1] == ' ':
501+
return
502+
if match[1] == '':
503+
yield match.start(1), "E275 missing whitespace after keyword"
504+
else:
505+
yield match.start(1), "E271 multiple spaces after keyword"
506+
496507

497508
@register_check
498509
def missing_whitespace_after_import_keyword(logical_line):

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

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# and then run "tox" from this directory.
55

66
[tox]
7-
envlist = py27, py34, py35, py36, py37, py38, pypy, pypy3, jython
7+
envlist = py27, py34, py35, py36, py37, py38, py39, py310, pypy, pypy3, jython
88
skip_missing_interpreters = True
99

1010
[testenv]

0 commit comments

Comments
 (0)