Reputation: 980
PEP8 suggests no spaces around equal operators in function parameters.
For example:
Correct:
def func(a=0):
print('PEP8 compliant spacing')
Incorrect:
def func(a = 0):
print('Invalid PEP8 spacing')
PyCharm's auto-formatter fails to pick up incorrect spacing when typing is included.
For example, PyCharm does not correctly format the following function:
def func(a: int = 0):
print('Invalid PEP8 spacing')
To:
def func(a: int=0):
print('PEP8 compliant spacing')
Has anyone found a way to have PyCharm's auto-formatter pick up spacing violations where typing is present?
Upvotes: 4
Views: 584
Reputation: 687
Although the original post misinterprets the PEP8 standard, there is actually a bug in the formatter with regards to this particular standard, when using a set for the type hint:
def foo(a: {str, None}=None):
print("This is the result before formatting, no PEP8 violation is reported")
def foo(a: {str, None} = None):
print("This is the result after formatting, the whitespace around the '=' is underlined as a PEP8 violation")
The formatting results in code that reports a PEP8 violation, either the formatter is wrong or the implementation of the standard is wrong. I think it is the latter.
If you use a tuple or list for the type hint there is no issue, but I feel a set is the most appropriate data structure, a tuple could imply that 'a' is a tuple of one string and one None e.g. ("hello", None).
def foo(a: (str, None) = None):
print("This is the result after formatting, no PEP8 violation is reported")
Upvotes: 0
Reputation: 20472
You are mistaken in your quoting of PEP8. The whitespace is supposed to be there in this case:
When combining an argument annotation with a default value, use spaces around the = sign (but only for those arguments that have both an annotation and a default).
Yes:
def munge(sep: AnyStr = None): ...
No:
def munge(input: AnyStr=None): ...
def munge(input: AnyStr, limit = 1000): ...
Upvotes: 8