eric6/Plugins/CheckerPlugins/CodeStyleChecker/MiscellaneousChecker.py

changeset 7651
ca87b7490449
parent 7639
422fd05e9c91
equal deleted inserted replaced
7650:36c2dc7e3437 7651:ca87b7490449
1469 """ 1469 """
1470 self.__checkForM507(node) 1470 self.__checkForM507(node)
1471 1471
1472 self.generic_visit(node) 1472 self.generic_visit(node)
1473 1473
1474 def visit_AsyncFor(self, node):
1475 """
1476 Public method to handle 'for' statements.
1477
1478 @param node reference to the node to be processed
1479 @type ast.AsyncFor
1480 """
1481 self.__checkForM507(node)
1482
1483 self.generic_visit(node)
1484
1474 def visit_Assert(self, node): 1485 def visit_Assert(self, node):
1475 """ 1486 """
1476 Public method to handle 'assert' statements. 1487 Public method to handle 'assert' statements.
1477 1488
1478 @param node reference to the node to be processed 1489 @param node reference to the node to be processed
1600 """ 1611 """
1601 super(ReturnVisitor, self).__init__() 1612 super(ReturnVisitor, self).__init__()
1602 1613
1603 self.__stack = [] 1614 self.__stack = []
1604 self.violations = [] 1615 self.violations = []
1616 self.__loopCount = 0
1605 1617
1606 @property 1618 @property
1607 def assigns(self): 1619 def assigns(self):
1608 """ 1620 """
1609 Public method to get the Assign nodes. 1621 Public method to get the Assign nodes.
1633 @return dictionary containing the node name as key and line number 1645 @return dictionary containing the node name as key and line number
1634 as value 1646 as value
1635 @rtype dict 1647 @rtype dict
1636 """ 1648 """
1637 return self.__stack[-1][ReturnVisitor.Returns] 1649 return self.__stack[-1][ReturnVisitor.Returns]
1650
1651 def visit_For(self, node):
1652 """
1653 Public method to handle a for loop.
1654
1655 @param node reference to the for node to handle
1656 @type ast.For
1657 """
1658 self.__visitLoop(node)
1659
1660 def visit_AsyncFor(self, node):
1661 """
1662 Public method to handle an async for loop.
1663
1664 @param node reference to the async for node to handle
1665 @type ast.AsyncFor
1666 """
1667 self.__visitLoop(node)
1668
1669 def visit_While(self, node):
1670 """
1671 Public method to handle a while loop.
1672
1673 @param node reference to the while node to handle
1674 @type ast.While
1675 """
1676 self.__visitLoop(node)
1677
1678 def __visitLoop(self, node):
1679 """
1680 Private method to handle loop nodes.
1681
1682 @param node reference to the loop node to handle
1683 @type ast.For, ast.AsyncFor or ast.While
1684 """
1685 self.__loopCount += 1
1686 self.generic_visit(node)
1687 self.__loopCount -= 1
1638 1688
1639 def __visitWithStack(self, node): 1689 def __visitWithStack(self, node):
1640 """ 1690 """
1641 Private method to traverse a given function node using a stack. 1691 Private method to traverse a given function node using a stack.
1642 1692
1688 @param node reference to the node to handle 1738 @param node reference to the node to handle
1689 @type ast.Assign 1739 @type ast.Assign
1690 """ 1740 """
1691 if not self.__stack: 1741 if not self.__stack:
1692 return 1742 return
1693 1743
1694 for target in node.targets:
1695 self.__visitAssignTarget(target)
1696 self.generic_visit(node.value) 1744 self.generic_visit(node.value)
1745
1746 target = node.targets[0]
1747 if (
1748 isinstance(target, ast.Tuple) and
1749 not isinstance(node.value, ast.Tuple)
1750 ):
1751 # skip unpacking assign
1752 return
1753
1754 self.__visitAssignTarget(target)
1697 1755
1698 def visit_Name(self, node): 1756 def visit_Name(self, node):
1699 """ 1757 """
1700 Public method to handle a name node. 1758 Public method to handle a name node.
1701 1759
1715 if isinstance(node, ast.Tuple): 1773 if isinstance(node, ast.Tuple):
1716 for elt in node.elts: 1774 for elt in node.elts:
1717 self.__visitAssignTarget(elt) 1775 self.__visitAssignTarget(elt)
1718 return 1776 return
1719 1777
1720 if isinstance(node, ast.Name): 1778 if not self.__loopCount and isinstance(node, ast.Name):
1721 self.assigns[node.id].append(node.lineno) 1779 self.assigns[node.id].append(node.lineno)
1722 return 1780 return
1723 1781
1724 self.generic_visit(node) 1782 self.generic_visit(node)
1725 1783
1753 Private method to check, if a node value is None. 1811 Private method to check, if a node value is None.
1754 1812
1755 @param node reference to the node to check 1813 @param node reference to the node to check
1756 @type ast.AST 1814 @type ast.AST
1757 @return flag indicating the node contains a None value 1815 @return flag indicating the node contains a None value
1816 @rtype bool
1758 """ 1817 """
1759 return ( 1818 return (
1760 AstUtilities.isNameConstant(node) and 1819 AstUtilities.isNameConstant(node) and
1761 AstUtilities.getValue(node) is None 1820 AstUtilities.getValue(node) is None
1821 )
1822
1823 def __isFalse(self, node):
1824 """
1825 Private method to check, if a node value is False.
1826
1827 @param node reference to the node to check
1828 @type ast.AST
1829 @return flag indicating the node contains a False value
1830 @rtype bool
1831 """
1832 return (
1833 AstUtilities.isNameConstant(node) and
1834 AstUtilities.getValue(node) is False
1762 ) 1835 )
1763 1836
1764 def __resultExists(self): 1837 def __resultExists(self):
1765 """ 1838 """
1766 Private method to check the existance of a return result. 1839 Private method to check the existance of a return result.
1805 1878
1806 self.__checkImplicitReturn(node.body[-1]) 1879 self.__checkImplicitReturn(node.body[-1])
1807 self.__checkImplicitReturn(node.orelse[-1]) 1880 self.__checkImplicitReturn(node.orelse[-1])
1808 return 1881 return
1809 1882
1810 if isinstance(node, ast.For) and node.orelse: 1883 if isinstance(node, (ast.For, ast.AsyncFor)) and node.orelse:
1811 self.__checkImplicitReturn(node.orelse[-1]) 1884 self.__checkImplicitReturn(node.orelse[-1])
1812 return 1885 return
1813 1886
1814 if isinstance(node, ast.With): 1887 if isinstance(node, (ast.With, ast.AsyncWith)):
1815 self.__checkImplicitReturn(node.body[-1]) 1888 self.__checkImplicitReturn(node.body[-1])
1889 return
1890
1891 if isinstance(node, ast.Assert) and self.__isFalse(node.test):
1816 return 1892 return
1817 1893
1818 try: 1894 try:
1819 okNodes = (ast.Return, ast.Raise, ast.While, ast.Try) 1895 okNodes = (ast.Return, ast.Raise, ast.While, ast.Try)
1820 except AttributeError: 1896 except AttributeError:

eric ide

mercurial