"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "pylint/checkers/variables.py" between
pylint-2.14.1.tar.gz and pylint-2.14.2.tar.gz

About: pylint analyzes Python source code looking for bugs and signs of poor quality.

variables.py  (pylint-2.14.1):variables.py  (pylint-2.14.2)
skipping to change at line 696 skipping to change at line 696
# Or the node_statement is in the else block of the relevant Try Except # Or the node_statement is in the else block of the relevant Try Except
elif ( elif (
isinstance(node_statement.parent, nodes.TryExcept) isinstance(node_statement.parent, nodes.TryExcept)
and node_statement in node_statement.parent.orelse and node_statement in node_statement.parent.orelse
and closest_try_except.parent.parent_of(node_statement) and closest_try_except.parent.parent_of(node_statement)
): ):
uncertain_nodes.append(other_node) uncertain_nodes.append(other_node)
# Assume the except blocks execute, so long as each handler # Assume the except blocks execute, so long as each handler
# defines the name, raises, or returns. # defines the name, raises, or returns.
elif all( elif all(
NamesConsumer._defines_name_raises_or_returns(node.name, han NamesConsumer._defines_name_raises_or_returns_recursive(
dler) node.name, handler
)
for handler in closest_try_except.handlers for handler in closest_try_except.handlers
): ):
continue continue
if NamesConsumer._check_loop_finishes_via_except(node, closest_try_e xcept): if NamesConsumer._check_loop_finishes_via_except(node, closest_try_e xcept):
continue continue
# Passed all tests for uncertain execution # Passed all tests for uncertain execution
uncertain_nodes.append(other_node) uncertain_nodes.append(other_node)
return uncertain_nodes return uncertain_nodes
@staticmethod @staticmethod
def _defines_name_raises_or_returns( def _defines_name_raises_or_returns(name: str, node: nodes.NodeNG) -> bool:
name: str, handler: nodes.ExceptHandler if isinstance(node, (nodes.Raise, nodes.Return)):
return True
if (
isinstance(node, nodes.AnnAssign)
and node.value
and isinstance(node.target, nodes.AssignName)
and node.target.name == name
):
return True
if isinstance(node, nodes.Assign):
for target in node.targets:
for elt in utils.get_all_elements(target):
if isinstance(elt, nodes.AssignName) and elt.name == name:
return True
if isinstance(node, nodes.If):
# Check for assignments inside the test
if isinstance(node.test, nodes.NamedExpr) and node.test.target.name
== name:
return True
if isinstance(node.test, nodes.Call):
for arg_or_kwarg in node.test.args + [
kw.value for kw in node.test.keywords
]:
if (
isinstance(arg_or_kwarg, nodes.NamedExpr)
and arg_or_kwarg.target.name == name
):
return True
return False
@staticmethod
def _defines_name_raises_or_returns_recursive(
name: str, node: nodes.NodeNG
) -> bool: ) -> bool:
"""Return True if some child of `handler` defines the name `name`, """Return True if some child of `node` defines the name `name`,
raises, or returns. raises, or returns.
""" """
for stmt in node.get_children():
def _define_raise_or_return(stmt: nodes.NodeNG) -> bool: if NamesConsumer._defines_name_raises_or_returns(name, stmt):
if isinstance(stmt, (nodes.Raise, nodes.Return)):
return True
if isinstance(stmt, nodes.Assign):
for target in stmt.targets:
for elt in utils.get_all_elements(target):
if isinstance(elt, nodes.AssignName) and elt.name == nam
e:
return True
if isinstance(stmt, nodes.If):
# Check for assignments inside the test
if (
isinstance(stmt.test, nodes.NamedExpr)
and stmt.test.target.name == name
):
return True
if isinstance(stmt.test, nodes.Call):
for arg_or_kwarg in stmt.test.args + [
kw.value for kw in stmt.test.keywords
]:
if (
isinstance(arg_or_kwarg, nodes.NamedExpr)
and arg_or_kwarg.target.name == name
):
return True
return False
for stmt in handler.get_children():
if _define_raise_or_return(stmt):
return True return True
if isinstance(stmt, (nodes.If, nodes.With)): if isinstance(stmt, (nodes.If, nodes.With)):
if any( if any(
_define_raise_or_return(nested_stmt) NamesConsumer._defines_name_raises_or_returns(name, nested_s tmt)
for nested_stmt in stmt.get_children() for nested_stmt in stmt.get_children()
): ):
return True return True
return False return False
@staticmethod @staticmethod
def _check_loop_finishes_via_except( def _check_loop_finishes_via_except(
node: nodes.NodeNG, other_node_try_except: nodes.TryExcept node: nodes.NodeNG, other_node_try_except: nodes.TryExcept
) -> bool: ) -> bool:
"""Check for a case described in https://github.com/PyCQA/pylint/issues/ 5683. """Check for a case described in https://github.com/PyCQA/pylint/issues/ 5683.
skipping to change at line 1480 skipping to change at line 1486
"""Checks a consumer for conditions that should trigger messages.""" """Checks a consumer for conditions that should trigger messages."""
# If the name has already been consumed, only check it's not a loop # If the name has already been consumed, only check it's not a loop
# variable used outside the loop. # variable used outside the loop.
if node.name in current_consumer.consumed: if node.name in current_consumer.consumed:
# Avoid the case where there are homonyms inside function scope and # Avoid the case where there are homonyms inside function scope and
# comprehension current scope (avoid bug #1731) # comprehension current scope (avoid bug #1731)
if utils.is_func_decorator(current_consumer.node) or not isinstance( if utils.is_func_decorator(current_consumer.node) or not isinstance(
node, nodes.ComprehensionScope node, nodes.ComprehensionScope
): ):
self._check_late_binding_closure(node) self._check_late_binding_closure(node)
self._loopvar_name(node)
return (VariableVisitConsumerAction.RETURN, None) return (VariableVisitConsumerAction.RETURN, None)
found_nodes = current_consumer.get_next_to_consume(node) found_nodes = current_consumer.get_next_to_consume(node)
if found_nodes is None: if found_nodes is None:
return (VariableVisitConsumerAction.CONTINUE, None) return (VariableVisitConsumerAction.CONTINUE, None)
if not found_nodes: if not found_nodes:
if node.name in current_consumer.consumed_uncertain: if node.name in current_consumer.consumed_uncertain:
confidence = CONTROL_FLOW confidence = CONTROL_FLOW
else: else:
confidence = HIGH confidence = HIGH
 End of changes. 6 change blocks. 
37 lines changed or deleted 41 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)