10 examples of 'bad operand type for unary +: 'str'' in Python

Every line of 'bad operand type for unary +: 'str'' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
713@utils.check_messages("nonexistent-operator")
714def visit_unaryop(self, node):
715 """check use of the non-existent ++ and -- operator operator"""
716 if (
717 (node.op in "+-")
718 and isinstance(node.operand, astroid.UnaryOp)
719 and (node.operand.op == node.op)
720 ):
721 self.add_message("nonexistent-operator", node=node, args=node.op * 2)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

156def _UnaryOp(self, tree):
157 L = self.visit(tree.operand)
158 op = Name(op_table[type(tree.op)])
159 return Apply(op, [L])
1438def visit_UnaryOp(self, n: UnaryOp) -> Type:
1439 # We support specifically Literal[-4] and nothing else.
1440 # For example, Literal[+4] or Literal[~6] is not supported.
1441 typ = self.visit(n.operand)
1442 if isinstance(typ, RawExpressionType) and isinstance(n.op, USub):
1443 if isinstance(typ.literal_value, int):
1444 typ.literal_value *= -1
1445 return typ
1446 return self.invalid_type(n)
308def visit_UnaryOp(self, node):
309 'Replace bitwise unaryop with function call'
310 self.generic_visit(node)
311 if isinstance(node.op, ast.Invert):
312 return ast.Call(ast.Name('mnot', ast.Load()),
313 [node.operand], [], None, None)
314 return node
101@classmethod
102def binop_type(cls, op, right_type):
103 if issubclass(right_type, StringType):
104 if op in REL_BIN_OPS:
105 return BoolType
106
107 return None
178def convert_unary_op(self, instance, op):
179 raise ConversionException("can't handle unary op %s on %s" % (op, self))
541def visitUnaryOperator(self, op:BinaryOperator):
542 if isinstance(op.op, (UPlus, UMinus)):
543 value = op.value.accept(self)
544 op.expr_type = value
545 return value
546 else:
547 assert False, f"Type inference for operator {type(op.op)} is not yet supported"
24@pytest.mark.parametrize("width", WIDTHS)
25@pytest.mark.parametrize("op", [
26 operator.neg,
27 operator.inv,
28 ])
29@pytest.mark.parametrize("BV", [SMTBitVector, z3BitVector])
30def test_unary_op(width, op, BV):
31 assert isinstance(op(BV[width]()), BV[width])
1977def visit_unary_expr(self, expr: UnaryExpr) -> None:
1978 expr.expr.accept(self)
198def transform_unary_op(self, op, operand):
199 name, precedence, _ = self.get_op_info(op)
200
201 x = '{x}'
202
203 if name == 'not':
204 return "np.logical_not(%s)" % x
205
206 right_op = getattr(operand, 'op', None)
207 if right_op:
208 _, other_precedence, other_assoc = self.get_op_info(right_op)
209 if other_precedence < precedence or other_precedence == precedence \
210 and other_assoc is not None:
211 x = '({x})'
212
213 if name in self._KEYWORDS:
214 return "%s %s" % (name, x)
215 else:
216 return "%s%s" % (name, x)

Related snippets