Removing empty parentheses after class name (#180)
This commit is contained in:
parent
2d9eaafa97
commit
6dbb657681
17
black.py
17
black.py
@ -743,6 +743,9 @@ def append(self, leaf: Leaf, preformatted: bool = False) -> None:
|
|||||||
if not has_value:
|
if not has_value:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if token.COLON == leaf.type and self.is_class_parenth_empty:
|
||||||
|
del self.leaves[-2:]
|
||||||
|
|
||||||
if self.leaves and not preformatted:
|
if self.leaves and not preformatted:
|
||||||
# Note: at this point leaf.prefix should be empty except for
|
# Note: at this point leaf.prefix should be empty except for
|
||||||
# imports, for which we only preserve newlines.
|
# imports, for which we only preserve newlines.
|
||||||
@ -840,6 +843,19 @@ def is_yield(self) -> bool:
|
|||||||
and self.leaves[0].value == "yield"
|
and self.leaves[0].value == "yield"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_class_parenth_empty(self) -> bool:
|
||||||
|
"""Is this class parentheses blank?"""
|
||||||
|
return (
|
||||||
|
bool(self)
|
||||||
|
and len(self.leaves) == 4
|
||||||
|
and self.is_class
|
||||||
|
and self.leaves[2].type == token.LPAR
|
||||||
|
and self.leaves[2].value == "("
|
||||||
|
and self.leaves[3].type == token.RPAR
|
||||||
|
and self.leaves[3].value == ")"
|
||||||
|
)
|
||||||
|
|
||||||
def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
|
def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
|
||||||
"""If so, needs to be split before emitting."""
|
"""If so, needs to be split before emitting."""
|
||||||
for leaf in self.leaves:
|
for leaf in self.leaves:
|
||||||
@ -1125,6 +1141,7 @@ def line(self, indent: int = 0, type: Type[Line] = Line) -> Iterator[Line]:
|
|||||||
|
|
||||||
If any lines were generated, set up a new current_line.
|
If any lines were generated, set up a new current_line.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not self.current_line:
|
if not self.current_line:
|
||||||
if self.current_line.__class__ == type:
|
if self.current_line.__class__ == type:
|
||||||
self.current_line.depth += indent
|
self.current_line.depth += indent
|
||||||
|
71
tests/class_blank_parentheses.py
Normal file
71
tests/class_blank_parentheses.py
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
class SimpleClassWithBlankParentheses():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ClassWithSpaceParentheses():
|
||||||
|
first_test_data = 90
|
||||||
|
second_test_data = 100
|
||||||
|
|
||||||
|
def test_func(self):
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
class ClassWithEmptyFunc(object):
|
||||||
|
|
||||||
|
def func_with_blank_parentheses():
|
||||||
|
return 5
|
||||||
|
|
||||||
|
|
||||||
|
def public_func_with_blank_parentheses():
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def class_under_the_func_with_blank_parentheses():
|
||||||
|
|
||||||
|
class InsideFunc():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class NormalClass():
|
||||||
|
|
||||||
|
def func_for_testing(self, first, second):
|
||||||
|
sum = first + second
|
||||||
|
return sum
|
||||||
|
|
||||||
|
|
||||||
|
# output
|
||||||
|
|
||||||
|
|
||||||
|
class SimpleClassWithBlankParentheses:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ClassWithSpaceParentheses:
|
||||||
|
first_test_data = 90
|
||||||
|
second_test_data = 100
|
||||||
|
|
||||||
|
def test_func(self):
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
class ClassWithEmptyFunc(object):
|
||||||
|
|
||||||
|
def func_with_blank_parentheses():
|
||||||
|
return 5
|
||||||
|
|
||||||
|
|
||||||
|
def public_func_with_blank_parentheses():
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def class_under_the_func_with_blank_parentheses():
|
||||||
|
|
||||||
|
class InsideFunc:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class NormalClass:
|
||||||
|
|
||||||
|
def func_for_testing(self, first, second):
|
||||||
|
sum = first + second
|
||||||
|
return sum
|
@ -325,6 +325,14 @@ def test_fmtonoff(self) -> None:
|
|||||||
black.assert_equivalent(source, actual)
|
black.assert_equivalent(source, actual)
|
||||||
black.assert_stable(source, actual, line_length=ll)
|
black.assert_stable(source, actual, line_length=ll)
|
||||||
|
|
||||||
|
@patch("black.dump_to_file", dump_to_stderr)
|
||||||
|
def test_remove_empty_parentheses_after_class(self) -> None:
|
||||||
|
source, expected = read_data("class_blank_parentheses")
|
||||||
|
actual = fs(source)
|
||||||
|
self.assertFormatEqual(expected, actual)
|
||||||
|
black.assert_equivalent(source, actual)
|
||||||
|
black.assert_stable(source, actual, line_length=ll)
|
||||||
|
|
||||||
def test_report(self) -> None:
|
def test_report(self) -> None:
|
||||||
report = black.Report()
|
report = black.Report()
|
||||||
out_lines = []
|
out_lines = []
|
||||||
|
Loading…
Reference in New Issue
Block a user