pycodestyle icon indicating copy to clipboard operation
pycodestyle copied to clipboard

Add whitespace checks for `match` and `case` - 2nd attempt

Open cdce8p opened this issue 4 years ago • 0 comments

Second attempt after #990

I've improved the default MATCH_CASE_REGEX, especially the lookahead part.

- (?=.*\:)
+ (?=.*\S.*\:(?=\s*$|\s*#))

It now requires at least one non-whitespace char to be present between case and :. Furthermore it matches end of line or begin of comment.

To account for case statements written inline, I've added fallback logic. That uses the tokenizer to check the : isn't inside any brackets.

match 0:
    case _: print('hi')
    case {"x": 42}: pass
    case "Hello: World": pass

cdce8p avatar Oct 13 '21 13:10 cdce8p