Skip to content

Commit

Permalink
Merge pull request #990 from cdce8p/whitespace-match-case
Browse files Browse the repository at this point in the history
Add whitespace checks for ``match`` and ``case``
  • Loading branch information
asottile committed May 3, 2021
2 parents 3d0ac73 + 3527106 commit 0f079a0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pycodestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def lru_cache(maxsize=128): # noqa as it's a fake implementation.
)))
)
DUNDER_REGEX = re.compile(r'^__([^\s]+)__ = ')
MATCH_CASE_REGEX = re.compile(r'^\s*\b(?:match|case)(\s*)(?=.*\:)')

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

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

if sys.version_info >= (3, 10):
match = MATCH_CASE_REGEX.match(logical_line)
if match:
if match[1] == ' ':
return
if match[1] == '':
yield match.start(1), "E275 missing whitespace after keyword"
else:
yield match.start(1), "E271 multiple spaces after keyword"


@register_check
def missing_whitespace_after_import_keyword(logical_line):
Expand Down
18 changes: 18 additions & 0 deletions testsuite/python310.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,21 @@
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
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# and then run "tox" from this directory.

[tox]
envlist = py27, py34, py35, py36, py37, py38, pypy, pypy3, jython
envlist = py27, py34, py35, py36, py37, py38, py39, py310, pypy, pypy3, jython
skip_missing_interpreters = True

[testenv]
Expand Down

0 comments on commit 0f079a0

Please sign in to comment.