Python ast.cmpop() Examples
The following are 6
code examples of ast.cmpop().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ast
, or try the search function
.
Example #1
Source File: pyupgrade.py From pyupgrade with MIT License | 6 votes |
def _compare_to_3( test: ast.Compare, op: Union[Type[ast.cmpop], Tuple[Type[ast.cmpop], ...]], ) -> bool: if not ( isinstance(test.ops[0], op) and isinstance(test.comparators[0], ast.Tuple) and len(test.comparators[0].elts) >= 1 and all(isinstance(n, ast.Num) for n in test.comparators[0].elts) ): return False # checked above but mypy needs help elts = cast('List[ast.Num]', test.comparators[0].elts) return elts[0].n == 3 and all(n.n == 0 for n in elts[1:])
Example #2
Source File: compares.py From wemake-python-styleguide with MIT License | 5 votes |
def _is_correct_len(sign: ast.cmpop, comparator: ast.AST) -> bool: """This is a helper function to tell what calls to ``len()`` are valid.""" if isinstance(operators.unwrap_unary_node(comparator), ast.Num): numeric_value = ast.literal_eval(comparator) if numeric_value == 0: return False if numeric_value == 1: return not isinstance(sign, (ast.GtE, ast.Lt)) return True
Example #3
Source File: compares.py From wemake-python-styleguide with MIT License | 5 votes |
def _check_constant(self, op: ast.cmpop, comparator: ast.expr) -> None: if not isinstance(op, (ast.Eq, ast.NotEq, ast.Is, ast.IsNot)): return real = get_assigned_expr(comparator) if not isinstance(real, (ast.List, ast.Dict, ast.Tuple)): return length = len(real.keys) if isinstance( real, ast.Dict, ) else len(real.elts) if not length: self.add_violation(FalsyConstantCompareViolation(comparator))
Example #4
Source File: compares.py From wemake-python-styleguide with MIT License | 5 votes |
def _check_is_constant_comprare( self, op: ast.cmpop, comparator: ast.expr, ) -> None: if not isinstance(op, (ast.Is, ast.IsNot)): return unwrapped = operators.unwrap_unary_node( get_assigned_expr(comparator), ) if isinstance(unwrapped, self._forbidden_for_is): self.add_violation(WrongIsCompareViolation(comparator))
Example #5
Source File: compares.py From wemake-python-styleguide with MIT License | 5 votes |
def get_similar_operators( operator: ast.cmpop, ) -> Union[Type[ast.cmpop], _MultipleCompareOperators]: """Returns similar operators types for the given operator.""" operator_type = operator.__class__ return SIMILAR_OPERATORS.get(operator_type, operator_type)
Example #6
Source File: compares.py From wemake-python-styleguide with MIT License | 5 votes |
def _mutate( self, comparison_node: ast.Compare, operator: ast.cmpop, name: str, is_left: bool, ) -> None: key_name = None if isinstance(operator, (ast.Lt, ast.LtE)): key_name = 'lower_bound' if is_left else 'upper_bound' elif isinstance(operator, (ast.Gt, ast.GtE)): key_name = 'upper_bound' if is_left else 'lower_bound' if key_name: getattr(self._uses[name], key_name).add(comparison_node)