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.

simplifiable_if_statement.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """Test that some if statement tests can be simplified."""
  2. # pylint: disable=missing-docstring, invalid-name, no-else-return
  3. def test_simplifiable_1(arg):
  4. # Simple test that can be replaced by bool(arg)
  5. if arg: # [simplifiable-if-statement]
  6. return True
  7. else:
  8. return False
  9. def test_simplifiable_2(arg, arg2):
  10. # Can be reduced to bool(arg and not arg2)
  11. if arg and not arg2: # [simplifiable-if-statement]
  12. return True
  13. else:
  14. return False
  15. def test_simplifiable_3(arg, arg2):
  16. # Can be reduced to bool(arg and not arg2)
  17. if arg and not arg2: # [simplifiable-if-statement]
  18. var = True
  19. else:
  20. var = False
  21. return var
  22. def test_simplifiable_4(arg):
  23. if arg:
  24. var = True
  25. else:
  26. if arg == "arg1": # [simplifiable-if-statement]
  27. return True
  28. else:
  29. return False
  30. return var
  31. def test_not_necessarily_simplifiable_1(arg, arg2):
  32. # Can be reduced to bool(not arg and not arg2) or to
  33. # `not all(N)`, which is a bit harder to understand
  34. # than `any(N)` when var should be False.
  35. if arg or arg2:
  36. var = False
  37. else:
  38. var = True
  39. return var
  40. def test_not_necessarily_simplifiabile_2(arg):
  41. # This could theoretically be reduced to `not arg or arg > 3`
  42. # but the net result is that now the condition is harder to understand,
  43. # because it requires understanding of an extra clause:
  44. # * first, there is the negation of truthness with `not arg`
  45. # * the second clause is `arg > 3`, which occurs when arg has a
  46. # a truth value, but it implies that `arg > 3` is equivalent
  47. # with `arg and arg > 3`, which means that the user must
  48. # think about this assumption when evaluating `arg > 3`.
  49. # The original form is easier to grasp.
  50. if arg and arg <= 3:
  51. return False
  52. else:
  53. return True
  54. def test_not_simplifiable_3(arg):
  55. if arg:
  56. test_not_necessarily_simplifiabile_2(arg)
  57. test_not_necessarily_simplifiable_1(arg, arg)
  58. return False
  59. else:
  60. if arg < 3:
  61. test_simplifiable_3(arg, 42)
  62. return True
  63. def test_not_simplifiable_4(arg):
  64. # Not interested in multiple elifs
  65. if arg == "any":
  66. return True
  67. elif test_not_simplifiable_3(arg) == arg:
  68. return True
  69. else:
  70. return False
  71. def test_not_simplifiable_5(arg):
  72. # Different actions in each branch
  73. if arg == "any":
  74. return True
  75. else:
  76. var = 42
  77. return var
  78. def test_not_simplifiable_6(arg):
  79. # Different actions in each branch
  80. if arg == "any":
  81. var = 42
  82. else:
  83. return True
  84. return var
  85. def test_not_simplifiable_7(arg):
  86. # Returning something different
  87. if arg == "any":
  88. return 4
  89. else:
  90. return 5
  91. def test_not_simplifiable_8(arg):
  92. # Only one of the branch returns something boolean
  93. if arg == "any":
  94. return True
  95. else:
  96. return 0