@@ -869,7 +869,8 @@ def whitespace_around_named_parameter_equals(logical_line, tokens):
869
869
r"""Don't use spaces around the '=' sign in function arguments.
870
870
871
871
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.
873
874
874
875
Okay: def complex(real, imag=0.0):
875
876
Okay: return magic(r=real, i=imag)
@@ -882,20 +883,29 @@ def whitespace_around_named_parameter_equals(logical_line, tokens):
882
883
883
884
E251: def complex(real, imag = 0.0):
884
885
E251: return magic(r = real, i = imag)
886
+ E252: def complex(real, image: float=0.0):
885
887
"""
886
888
parens = 0
887
889
no_space = False
890
+ require_space = False
888
891
prev_end = None
889
892
annotated_func_arg = False
890
893
in_def = bool (STARTSWITH_DEF_REGEX .match (logical_line ))
894
+
891
895
message = "E251 unexpected spaces around keyword / parameter equals"
896
+ missing_message = "E252 missing whitespace around parameter equals"
897
+
892
898
for token_type , text , start , end , line in tokens :
893
899
if token_type == tokenize .NL :
894
900
continue
895
901
if no_space :
896
902
no_space = False
897
903
if start != prev_end :
898
904
yield (prev_end , message )
905
+ if require_space :
906
+ require_space = False
907
+ if start == prev_end :
908
+ yield (prev_end , missing_message )
899
909
if token_type == tokenize .OP :
900
910
if text in '([' :
901
911
parens += 1
@@ -905,10 +915,15 @@ def whitespace_around_named_parameter_equals(logical_line, tokens):
905
915
annotated_func_arg = True
906
916
elif parens and text == ',' and parens == 1 :
907
917
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 )
912
927
if not parens :
913
928
annotated_func_arg = False
914
929
0 commit comments