1613 |
1615 |
1614 |
1616 |
1615 ####################################################################### |
1617 ####################################################################### |
1616 ## BugBearVisitor |
1618 ## BugBearVisitor |
1617 ## |
1619 ## |
1618 ## adapted from: flake8-bugbear v22.12.6 |
1620 ## adapted from: flake8-bugbear v23.7.10 |
1619 ## |
1621 ## |
1620 ## Original: Copyright (c) 2016 Łukasz Langa |
1622 ## Original: Copyright (c) 2016 Łukasz Langa |
1621 ####################################################################### |
1623 ####################################################################### |
1622 |
1624 |
1623 BugBearContext = namedtuple("BugBearContext", ["node", "stack"]) |
1625 BugBearContext = namedtuple("BugBearContext", ["node", "stack"]) |
2023 self.violations.append((node, "M510")) |
2025 self.violations.append((node, "M510")) |
2024 |
2026 |
2025 self.__checkForM526(node) |
2027 self.__checkForM526(node) |
2026 |
2028 |
2027 self.__checkForM528(node) |
2029 self.__checkForM528(node) |
|
2030 self.__checkForM534(node) |
2028 |
2031 |
2029 self.generic_visit(node) |
2032 self.generic_visit(node) |
2030 |
2033 |
2031 def visit_Module(self, node): |
2034 def visit_Module(self, node): |
2032 """ |
2035 """ |
2294 return # method is being run on an imported module |
2297 return # method is being run on an imported module |
2295 |
2298 |
2296 if len(node.args) != 1 or not AstUtilities.isString(node.args[0]): |
2299 if len(node.args) != 1 or not AstUtilities.isString(node.args[0]): |
2297 return # used arguments don't match the builtin strip |
2300 return # used arguments don't match the builtin strip |
2298 |
2301 |
2299 s = AstUtilities.getValue(node.args[0]) |
2302 value = AstUtilities.getValue(node.args[0]) |
2300 if len(s) == 1: |
2303 if len(value) == 1: |
2301 return # stripping just one character |
2304 return # stripping just one character |
2302 |
2305 |
2303 if len(s) == len(set(s)): |
2306 if len(value) == len(set(value)): |
2304 return # no characters appear more than once |
2307 return # no characters appear more than once |
2305 |
2308 |
2306 self.violations.append((node, "M505")) |
2309 self.violations.append((node, "M505")) |
2307 |
2310 |
2308 def __checkForM507(self, node): |
2311 def __checkForM507(self, node): |
2810 Private method to check a set for duplicate items. |
2813 Private method to check a set for duplicate items. |
2811 |
2814 |
2812 @param node reference to the node to be processed |
2815 @param node reference to the node to be processed |
2813 @type ast.Set |
2816 @type ast.Set |
2814 """ |
2817 """ |
2815 constants = [ |
2818 seen = set() |
2816 item.value |
2819 for elt in node.elts: |
2817 for item in filter(lambda x: isinstance(x, ast.Constant), node.elts) |
2820 if not isinstance(elt, ast.Constant): |
2818 ] |
2821 continue |
2819 if len(constants) != len(set(constants)): |
2822 if elt.value in seen: |
2820 self.violations.append((node, "M533")) |
2823 self.violations.append((node, "M533", repr(elt.value))) |
|
2824 else: |
|
2825 seen.add(elt.value) |
|
2826 |
|
2827 def __checkForM534(self, node): |
|
2828 """ |
|
2829 Private method to check that re.sub/subn/split arguments flags/count/maxsplit |
|
2830 are passed as keyword arguments. |
|
2831 |
|
2832 @param node reference to the node to be processed |
|
2833 @type ast.Call |
|
2834 """ |
|
2835 if not isinstance(node.func, ast.Attribute): |
|
2836 return |
|
2837 if not isinstance(node.func.value, ast.Name) or node.func.value.id != "re": |
|
2838 return |
|
2839 |
|
2840 def check(numArgs, paramName): |
|
2841 if len(node.args) > numArgs: |
|
2842 self.violations.append((node, "M534", node.func.attr, paramName)) |
|
2843 |
|
2844 if node.func.attr in ("sub", "subn"): |
|
2845 check(3, "count") |
|
2846 elif node.func.attr == "split": |
|
2847 check(2, "maxsplit") |
2821 |
2848 |
2822 |
2849 |
2823 class NameFinder(ast.NodeVisitor): |
2850 class NameFinder(ast.NodeVisitor): |
2824 """ |
2851 """ |
2825 Class to extract a name out of a tree of nodes. |
2852 Class to extract a name out of a tree of nodes. |