Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

invalid_unary_operand_type.py 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """Detect problems with invalid operands used on invalid objects."""
  2. # pylint: disable=missing-docstring,too-few-public-methods,invalid-name
  3. # pylint: disable=unused-variable
  4. import collections
  5. class Implemented(object):
  6. def __invert__(self):
  7. return 42
  8. def __pos__(self):
  9. return 42
  10. def __neg__(self):
  11. return 42
  12. def these_are_good():
  13. negative = -1
  14. negative1 = -1.0
  15. positive = +1
  16. positive2 = +1.0
  17. inverted = ~1
  18. not_int = not 1
  19. not_float = not 2.0
  20. not_string = not ""
  21. not_list = not []
  22. not_dict = not {}
  23. not_tuple = not (1, 2)
  24. inverted_instance = ~Implemented()
  25. positive_instance = +Implemented()
  26. negative_instance = -Implemented()
  27. not_instance = not Implemented()
  28. def these_are_bad():
  29. invert_list = ~[] # [invalid-unary-operand-type]
  30. invert_tuple = ~() # [invalid-unary-operand-type]
  31. invert_dict = ~dict() # [invalid-unary-operand-type]
  32. invert_dict_1 = ~{} # [invalid-unary-operand-type]
  33. invert_set = ~set() # [invalid-unary-operand-type]
  34. neg_set = -set() # [invalid-unary-operand-type]
  35. neg_str = -"" # [invalid-unary-operand-type]
  36. invert_str = ~"" # [invalid-unary-operand-type]
  37. pos_str = +"" # [invalid-unary-operand-type]
  38. class A(object):
  39. pass
  40. invert_func = ~(lambda: None) # [invalid-unary-operand-type]
  41. invert_class = ~A # [invalid-unary-operand-type]
  42. invert_instance = ~A() # [invalid-unary-operand-type]
  43. invert_module = ~collections # [invalid-unary-operand-type]
  44. invert_float = ~2.0 # [invalid-unary-operand-type]