Skip to content

Commit

Permalink
Correct handling of except in SpacesVisitor
Browse files Browse the repository at this point in the history
This required a small adjustment to the parser.
  • Loading branch information
knutwannheden committed Jan 31, 2025
1 parent 39081b5 commit 63739d8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
3 changes: 2 additions & 1 deletion rewrite/rewrite/python/_parser_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ def visit_Store(self, node):

def visit_ExceptHandler(self, node):
prefix = self.__source_before('except')
type_prefix = self.__whitespace()
except_type = self.__convert_type(node.type) if node.type else j.Empty(random_id(), Space.EMPTY,
Markers.EMPTY)
if node.name:
Expand Down Expand Up @@ -820,7 +821,7 @@ def visit_ExceptHandler(self, node):
Markers.EMPTY,
self.__pad_right(j.VariableDeclarations(
random_id(),
Space.EMPTY,
type_prefix,
Markers.EMPTY,
[], [],
except_type,
Expand Down
20 changes: 12 additions & 8 deletions rewrite/rewrite/python/format/spaces_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from rewrite import Tree, list_map
from rewrite.java import J, Assignment, JLeftPadded, AssignmentOperation, MemberReference, MethodInvocation, \
MethodDeclaration, Empty, ArrayAccess, Space, If, Block, ClassDeclaration, VariableDeclarations, JRightPadded, \
Import, ParameterizedType, Parentheses, WhileLoop
Import, ParameterizedType, Parentheses, Try, ControlParentheses, J2
from rewrite.python import PythonVisitor, SpacesStyle, Binary, ChainedAssignment, Slice, CollectionLiteral, \
ForLoop, DictLiteral, KeyValue, TypeHint, MultiImport, ExpressionTypeTree, ComprehensionExpression, NamedArgument
from rewrite.visitor import P, Cursor
Expand Down Expand Up @@ -102,6 +102,17 @@ def _process_argument(index, arg, args_size):
)
)

def visit_catch(self, catch: Try.Catch, p: P) -> J:
c = cast(Try.Catch, super().visit_catch(catch, p))
# c = c.with_parameter(c.parameter.with_tree(space_before(c.parameter.tree, True)))
return c

def visit_control_parentheses(self, control_parentheses: ControlParentheses[J2], p: P) -> J:
cp = cast(ControlParentheses[J2], super().visit_control_parentheses(control_parentheses, p))
cp = space_before(cp, False)
cp = cp.with_tree(space_before(cp.tree, True))
return cp

def visit_named_argument(self, named_argument: NamedArgument, p: P) -> J:
a = cast(NamedArgument, super().visit_named_argument(named_argument, p))
if a.padding.value is not None:
Expand Down Expand Up @@ -301,9 +312,6 @@ def visit_python_binary(self, binary: Binary, p: P) -> J:
def visit_if(self, if_stm: If, p: P) -> J:
if_: j.If = cast(If, super().visit_if(if_stm, p))

# Handle space before if condition e.g. if True: <-> if True:
if_ = if_.with_if_condition(space_before(if_._if_condition, True))

# Handle space before if colon e.g. if True: pass <-> if True: pass
if_ = if_.with_if_condition(
if_.if_condition.padding.with_tree(
Expand Down Expand Up @@ -346,10 +354,6 @@ def visit_python_for_loop(self, for_loop: ForLoop, p: P) -> J:
fl = fl.padding.with_iterable(space_before_right_padded_element(fl.padding.iterable, True))
return fl

def visit_while_loop(self, while_loop: WhileLoop, p: P) -> J:
w = cast(WhileLoop, super().visit_while_loop(while_loop, p))
return w.with_condition(space_before(w.condition, True))

def visit_parameterized_type(self, parameterized_type: ParameterizedType, p: P) -> J:
pt = cast(ParameterizedType, super().visit_parameterized_type(parameterized_type, p))

Expand Down
48 changes: 48 additions & 0 deletions rewrite/tests/python/all/format/spaces/try_space_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from rewrite.python import IntelliJ, SpacesVisitor
from rewrite.test import rewrite_run, python, RecipeSpec, from_visitor


def test_except():
style = IntelliJ.spaces()
rewrite_run(
# language=python
python(
"""\
try:
pass
except OSError as e:
pass
""",
"""\
try:
pass
except OSError as e:
pass
"""
),
spec=RecipeSpec()
.with_recipe(from_visitor(SpacesVisitor(style)))
)


def test_multi_except():
style = IntelliJ.spaces()
rewrite_run(
# language=python
python(
"""\
try:
pass
except(OSError, IOError) as e:
pass
""",
"""\
try:
pass
except (OSError, IOError) as e:
pass
"""
),
spec=RecipeSpec()
.with_recipe(from_visitor(SpacesVisitor(style)))
)

0 comments on commit 63739d8

Please sign in to comment.