@@ -143,7 +143,7 @@ def are_exclusive(stmt1, stmt2, exceptions: list[str] | None = None) -> bool:
143143 previous = stmt2
144144 for node in stmt2 .node_ancestors ():
145145 if node in stmt1_parents :
146- # if the common parent is a If or TryExcept statement, look if
146+ # if the common parent is a If or Try statement, look if
147147 # nodes are in exclusive branches
148148 if isinstance (node , If ) and exceptions is None :
149149 c2attr , c2node = node .locate_child (previous )
@@ -155,7 +155,7 @@ def are_exclusive(stmt1, stmt2, exceptions: list[str] | None = None) -> bool:
155155 if c1attr != c2attr :
156156 # different `If` branches (`If.body` and `If.orelse`)
157157 return True
158- elif isinstance (node , TryExcept ):
158+ elif isinstance (node , Try ):
159159 c2attr , c2node = node .locate_child (previous )
160160 c1attr , c1node = node .locate_child (children [node ])
161161 if c1node is not c2node :
@@ -3720,97 +3720,33 @@ def infer_lhs(self, context: InferenceContext | None = None, **kwargs: Any):
37203720 return self ._infer_subscript (context , ** kwargs )
37213721
37223722
3723- class TryExcept (_base_nodes .MultiLineWithElseBlockNode , _base_nodes .Statement ):
3724- """Class representing an :class:`ast.TryExcept ` node.
3723+ class Try (_base_nodes .MultiLineWithElseBlockNode , _base_nodes .Statement ):
3724+ """Class representing a :class:`ast.Try ` node.
37253725
37263726 >>> import astroid
37273727 >>> node = astroid.extract_node('''
37283728 try:
37293729 do_something()
37303730 except Exception as error:
37313731 print("Error!")
3732+ finally:
3733+ print("Cleanup!")
37323734 ''')
37333735 >>> node
3734- <TryExcept l.2 at 0x7f23b2e9d908 >
3736+ <Try l.2 at 0x7f23b2e41d68 >
37353737 """
37363738
3737- _astroid_fields = ("body" , "handlers" , "orelse" )
3738- _multi_line_block_fields = ("body" , "handlers" , "orelse" )
3739-
3740- body : list [NodeNG ]
3741- """The contents of the block to catch exceptions from."""
3742-
3743- handlers : list [ExceptHandler ]
3744- """The exception handlers."""
3745-
3746- orelse : list [NodeNG ]
3747- """The contents of the ``else`` block."""
3748-
3749- def postinit (
3750- self ,
3751- body : list [NodeNG ],
3752- handlers : list [ExceptHandler ],
3753- orelse : list [NodeNG ],
3754- ) -> None :
3755- self .body = body
3756- self .handlers = handlers
3757- self .orelse = orelse
3758-
3759- def _infer_name (self , frame , name ):
3760- return name
3761-
3762- def block_range (self , lineno : int ) -> tuple [int , int ]:
3763- """Get a range from the given line number to where this node ends.
3764-
3765- :param lineno: The line number to start the range at.
3766-
3767- :returns: The range of line numbers that this node belongs to,
3768- starting at the given line number.
3769- """
3770- last = None
3771- for exhandler in self .handlers :
3772- if exhandler .type and lineno == exhandler .type .fromlineno :
3773- return lineno , lineno
3774- if exhandler .body [0 ].fromlineno <= lineno <= exhandler .body [- 1 ].tolineno :
3775- return lineno , exhandler .body [- 1 ].tolineno
3776- if last is None :
3777- last = exhandler .body [0 ].fromlineno - 1
3778- return self ._elsed_block_range (lineno , self .orelse , last )
3779-
3780- def get_children (self ):
3781- yield from self .body
3782-
3783- yield from self .handlers or ()
3784- yield from self .orelse or ()
3785-
3786-
3787- class TryFinally (_base_nodes .MultiLineWithElseBlockNode , _base_nodes .Statement ):
3788- """Class representing an :class:`ast.TryFinally` node.
3789-
3790- >>> import astroid
3791- >>> node = astroid.extract_node('''
3792- try:
3793- do_something()
3794- except Exception as error:
3795- print("Error!")
3796- finally:
3797- print("Cleanup!")
3798- ''')
3799- >>> node
3800- <TryFinally l.2 at 0x7f23b2e41d68>
3801- """
3802-
3803- _astroid_fields = ("body" , "finalbody" )
3804- _multi_line_block_fields = ("body" , "finalbody" )
3739+ _astroid_fields = ("body" , "handlers" , "orelse" , "finalbody" )
3740+ _multi_line_block_fields = ("body" , "handlers" , "orelse" , "finalbody" )
38053741
38063742 def __init__ (
38073743 self ,
3808- lineno : int | None = None ,
3809- col_offset : int | None = None ,
3810- parent : NodeNG | None = None ,
38113744 * ,
3812- end_lineno : int | None = None ,
3813- end_col_offset : int | None = None ,
3745+ lineno : int ,
3746+ col_offset : int ,
3747+ end_lineno : int ,
3748+ end_col_offset : int ,
3749+ parent : NodeNG ,
38143750 ) -> None :
38153751 """
38163752 :param lineno: The line that this node appears on in the source code.
@@ -3825,8 +3761,14 @@ def __init__(
38253761 :param end_col_offset: The end column this node appears on in the
38263762 source code. Note: This is after the last symbol.
38273763 """
3828- self .body : list [NodeNG | TryExcept ] = []
3829- """The try-except that the finally is attached to."""
3764+ self .body : list [NodeNG ] = []
3765+ """The contents of the block to catch exceptions from."""
3766+
3767+ self .handlers : list [ExceptHandler ] = []
3768+ """The exception handlers."""
3769+
3770+ self .orelse : list [NodeNG ] = []
3771+ """The contents of the ``else`` block."""
38303772
38313773 self .finalbody : list [NodeNG ] = []
38323774 """The contents of the ``finally`` block."""
@@ -3841,40 +3783,58 @@ def __init__(
38413783
38423784 def postinit (
38433785 self ,
3844- body : list [NodeNG | TryExcept ] | None = None ,
3845- finalbody : list [NodeNG ] | None = None ,
3786+ * ,
3787+ body : list [NodeNG ],
3788+ handlers : list [ExceptHandler ],
3789+ orelse : list [NodeNG ],
3790+ finalbody : list [NodeNG ],
38463791 ) -> None :
38473792 """Do some setup after initialisation.
38483793
3849- :param body: The try-except that the finally is attached to.
3794+ :param body: The contents of the block to catch exceptions from.
3795+
3796+ :param handlers: The exception handlers.
3797+
3798+ :param orelse: The contents of the ``else`` block.
38503799
38513800 :param finalbody: The contents of the ``finally`` block.
38523801 """
3853- if body is not None :
3854- self .body = body
3855- if finalbody is not None :
3856- self .finalbody = finalbody
3857-
3858- def block_range (self , lineno : int ) -> tuple [int , int ]:
3859- """Get a range from the given line number to where this node ends.
3802+ self .body = body
3803+ self .handlers = handlers
3804+ self .orelse = orelse
3805+ self .finalbody = finalbody
38603806
3861- :param lineno: The line number to start the range at.
3807+ def _infer_name (self , frame , name ):
3808+ return name
38623809
3863- :returns: The range of line numbers that this node belongs to,
3864- starting at the given line number.
3865- """
3866- child = self .body [0 ]
3867- # py2.5 try: except: finally:
3868- if (
3869- isinstance (child , TryExcept )
3870- and child .fromlineno == self .fromlineno
3871- and child .tolineno >= lineno > self .fromlineno
3872- ):
3873- return child .block_range (lineno )
3874- return self ._elsed_block_range (lineno , self .finalbody )
3810+ def block_range (self , lineno : int ) -> tuple [int , int ]:
3811+ """Get a range from a given line number to where this node ends."""
3812+ if lineno == self .fromlineno :
3813+ return lineno , lineno
3814+ if self .body and self .body [0 ].fromlineno <= lineno <= self .body [- 1 ].tolineno :
3815+ # Inside try body - return from lineno till end of try body
3816+ return lineno , self .body [- 1 ].tolineno
3817+ for exhandler in self .handlers :
3818+ if exhandler .type and lineno == exhandler .type .fromlineno :
3819+ return lineno , lineno
3820+ if exhandler .body [0 ].fromlineno <= lineno <= exhandler .body [- 1 ].tolineno :
3821+ return lineno , exhandler .body [- 1 ].tolineno
3822+ if self .orelse :
3823+ if self .orelse [0 ].fromlineno - 1 == lineno :
3824+ return lineno , lineno
3825+ if self .orelse [0 ].fromlineno <= lineno <= self .orelse [- 1 ].tolineno :
3826+ return lineno , self .orelse [- 1 ].tolineno
3827+ if self .finalbody :
3828+ if self .finalbody [0 ].fromlineno - 1 == lineno :
3829+ return lineno , lineno
3830+ if self .finalbody [0 ].fromlineno <= lineno <= self .finalbody [- 1 ].tolineno :
3831+ return lineno , self .finalbody [- 1 ].tolineno
3832+ return lineno , self .tolineno
38753833
38763834 def get_children (self ):
38773835 yield from self .body
3836+ yield from self .handlers
3837+ yield from self .orelse
38783838 yield from self .finalbody
38793839
38803840
0 commit comments