From c098297fa5154fd18f81ae97feb06e84b0f6ba15 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 3 May 2021 00:35:11 +0200 Subject: Add whitespace checks for match and case --- pycodestyle.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'pycodestyle.py') 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': {}} @@ -512,6 +513,32 @@ def missing_whitespace_after_import_keyword(logical_line): yield pos, "E275 missing whitespace after keyword" +@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. -- cgit v1.2.1