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.

inconsistent_returns.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #pylint: disable=missing-docstring, no-else-return, invalid-name, unused-variable, superfluous-parens
  2. """Testing inconsistent returns"""
  3. import math
  4. import sys
  5. # These ones are consistent
  6. def explicit_returns(var):
  7. if var >= 0:
  8. return math.sqrt(var)
  9. else:
  10. return None
  11. def explicit_returns2(var):
  12. if var < 0:
  13. return None
  14. return math.sqrt(var)
  15. def empty_implicit_returns(var):
  16. if var < 0:
  17. return
  18. def returns_in_exceptions():
  19. try:
  20. raise ValueError('test')
  21. except ValueError:
  22. return 1
  23. except (OSError, TypeError):
  24. return 2
  25. def returns_and_exceptions(var):
  26. if var < 10:
  27. return var**2
  28. else:
  29. raise ValueError("Incorrect value")
  30. def returns_and_exceptions_issue1770(var):
  31. try:
  32. if var == 1:
  33. return 'a'
  34. elif var == 2:
  35. return 'b'
  36. else:
  37. raise ValueError
  38. except AssertionError:
  39. return None
  40. def explicit_returns3(arg):
  41. if arg:
  42. return False
  43. else:
  44. if arg < 3:
  45. print('arg < 3')
  46. return True
  47. def explicit_returns4(arg):
  48. if arg:
  49. if arg > 2:
  50. print('arg > 2')
  51. return False
  52. else:
  53. if arg < 3:
  54. print('arg < 3')
  55. return True
  56. def explicit_returns5(arg):
  57. if arg:
  58. if arg > 2:
  59. print('arg > 2')
  60. return False
  61. else:
  62. return True
  63. def nested_function():
  64. def dummy_return():
  65. return
  66. return dummy_return
  67. def explicit_returns6(x, y, z):
  68. if x: # pylint: disable=no-else-return
  69. a = 1
  70. if y: # pylint: disable=no-else-return
  71. b = 2
  72. return y
  73. else:
  74. c = 3
  75. return x
  76. else:
  77. d = 4
  78. return z
  79. def explicit_returns7(arg):
  80. if arg < 0:
  81. arg = 2 * arg
  82. return 'below 0'
  83. elif arg == 0:
  84. print("Null arg")
  85. return '0'
  86. else:
  87. arg = 3 * arg
  88. return 'above 0'
  89. def bug_1772():
  90. """Don't check inconsistent return statements inside while loop"""
  91. counter = 1
  92. while True:
  93. counter += 1
  94. if counter == 100:
  95. return 7
  96. def bug_1771(var):
  97. if var == 1:
  98. sys.exit(1)
  99. else:
  100. return var * 2
  101. def bug_1771_with_user_config(var):
  102. # sys.getdefaultencoding is considered as a never
  103. # returning function in the inconsistent_returns.rc file.
  104. if var == 1:
  105. sys.getdefaultencoding()
  106. else:
  107. return var * 2
  108. def bug_1794_inner_func_in_if(var):
  109. # pylint: disable = no-else-return
  110. if var:
  111. def _inner():
  112. return None
  113. return None
  114. else:
  115. return None
  116. try:
  117. import ConfigParser as configparser
  118. except ImportError:
  119. import configparser
  120. # Due to the try/except import above, astroid cannot safely
  121. # infer the exception type. It doesn't matter here, because
  122. # as the raise statement is not inside a try/except one, there
  123. # is no need to infer the exception type. It is just an exception
  124. # that is raised.
  125. def bug_1794(a):
  126. for x in range(a):
  127. if x == 100:
  128. return a
  129. raise configparser.NoSectionError('toto')
  130. # Next ones are not consistent
  131. def explicit_implicit_returns(var): # [inconsistent-return-statements]
  132. if var >= 0:
  133. return math.sqrt(var)
  134. def empty_explicit_returns(var): # [inconsistent-return-statements]
  135. if var < 0:
  136. return
  137. return math.sqrt(var)
  138. def explicit_implicit_returns2(arg): # [inconsistent-return-statements]
  139. if arg:
  140. if arg > 2:
  141. print('arg > 2')
  142. return False
  143. else:
  144. return True
  145. def explicit_implicit_returns3(arg): # [inconsistent-return-statements]
  146. if arg:
  147. if arg > 2:
  148. print('arg > 2')
  149. return False
  150. else:
  151. return True
  152. def returns_missing_in_catched_exceptions(arg): # [inconsistent-return-statements]
  153. try:
  154. arg = arg**2
  155. raise ValueError('test')
  156. except ValueError:
  157. print('ValueError')
  158. arg = 0
  159. except (OSError, TypeError):
  160. return 2
  161. def complex_func(arg): # [inconsistent-return-statements]
  162. for i in range(arg):
  163. if i > arg / 2:
  164. break
  165. else:
  166. return arg
  167. def inconsistent_returns_in_nested_function():
  168. def not_consistent_returns_inner(arg): # [inconsistent-return-statements]
  169. for i in range(arg):
  170. if i > arg / 2:
  171. break
  172. else:
  173. return arg
  174. return not_consistent_returns_inner
  175. class BlargException(Exception):
  176. pass
  177. def blarg(someval):
  178. try:
  179. if someval:
  180. raise BlargException()
  181. return 5
  182. except BlargException:
  183. raise
  184. def bug_1772_counter_example(): # [inconsistent-return-statements]
  185. counter = 1
  186. if counter == 1:
  187. while True:
  188. counter += 1
  189. if counter == 100:
  190. return 7
  191. def bug_1771_counter_example(var): # [inconsistent-return-statements]
  192. if var == 1:
  193. inconsistent_returns_in_nested_function()
  194. else:
  195. return var * 2
  196. def bug_1794_inner_func_in_if_counter_example_1(var): # [inconsistent-return-statements]
  197. # pylint: disable = no-else-return
  198. if var:
  199. def _inner():
  200. return None
  201. return None
  202. else:
  203. return
  204. def bug_1794_inner_func_in_if_counter_example_2(var): # [inconsistent-return-statements]
  205. # pylint: disable = no-else-return
  206. if var:
  207. def _inner():
  208. return
  209. return None
  210. else:
  211. return
  212. def bug_1794_inner_func_in_if_counter_example_3(var): # [inconsistent-return-statements]
  213. # pylint: disable = no-else-return
  214. if var:
  215. def _inner():
  216. return None
  217. return None
  218. else:
  219. def _inner2(var_bis): # [inconsistent-return-statements]
  220. if var_bis:
  221. return True
  222. return