Skip to content

Commit d9efb8f

Browse files
committed
Add E252 on missing whitespace for annotated parameter defaults
1 parent 4f5d398 commit d9efb8f

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

pycodestyle.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,8 @@ def whitespace_around_named_parameter_equals(logical_line, tokens):
869869
r"""Don't use spaces around the '=' sign in function arguments.
870870
871871
Don't use spaces around the '=' sign when used to indicate a
872-
keyword argument or a default parameter value.
872+
keyword argument or a default parameter value, except when using a type
873+
annotation.
873874
874875
Okay: def complex(real, imag=0.0):
875876
Okay: return magic(r=real, i=imag)
@@ -882,20 +883,29 @@ def whitespace_around_named_parameter_equals(logical_line, tokens):
882883
883884
E251: def complex(real, imag = 0.0):
884885
E251: return magic(r = real, i = imag)
886+
E252: def complex(real, image: float=0.0):
885887
"""
886888
parens = 0
887889
no_space = False
890+
require_space = False
888891
prev_end = None
889892
annotated_func_arg = False
890893
in_def = bool(STARTSWITH_DEF_REGEX.match(logical_line))
894+
891895
message = "E251 unexpected spaces around keyword / parameter equals"
896+
missing_message = "E252 missing whitespace around parameter equals"
897+
892898
for token_type, text, start, end, line in tokens:
893899
if token_type == tokenize.NL:
894900
continue
895901
if no_space:
896902
no_space = False
897903
if start != prev_end:
898904
yield (prev_end, message)
905+
if require_space:
906+
require_space = False
907+
if start == prev_end:
908+
yield (prev_end, missing_message)
899909
if token_type == tokenize.OP:
900910
if text in '([':
901911
parens += 1
@@ -905,10 +915,15 @@ def whitespace_around_named_parameter_equals(logical_line, tokens):
905915
annotated_func_arg = True
906916
elif parens and text == ',' and parens == 1:
907917
annotated_func_arg = False
908-
elif parens and text == '=' and not annotated_func_arg:
909-
no_space = True
910-
if start != prev_end:
911-
yield (prev_end, message)
918+
elif parens and text == '=':
919+
if not annotated_func_arg:
920+
no_space = True
921+
if start != prev_end:
922+
yield (prev_end, message)
923+
else:
924+
require_space = True
925+
if start == prev_end:
926+
yield (prev_end, missing_message)
912927
if not parens:
913928
annotated_func_arg = False
914929

testsuite/E25.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ async def add(a: int = 0, b: int = 0) -> int:
4242
#: E272:1:6
4343
async def add(a: int = 0, b: int = 0) -> int:
4444
return a + b
45+
#: E252:1:15 E252:1:16 E252:1:27 E252:1:36
46+
def add(a: int=0, b: int =0, c: int= 0) -> int:
47+
return a + b + c

0 commit comments

Comments
 (0)