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.

using_constant_test.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. """Verify if constant tests are used inside if statements."""
  2. # pylint: disable=invalid-name, missing-docstring,too-few-public-methods
  3. # pylint: disable=no-init,expression-not-assigned
  4. import collections
  5. def function():
  6. yield
  7. class Class(object):
  8. def method(self):
  9. pass
  10. instance = Class()
  11. if collections: # [using-constant-test]
  12. pass
  13. # GenExpr
  14. if (node for node in range(10)): # [using-constant-test]
  15. pass
  16. if lambda: None: # [using-constant-test]
  17. pass
  18. if function: # [using-constant-test]
  19. pass
  20. if Class: # [using-constant-test]
  21. pass
  22. if 2: # [using-constant-test]
  23. pass
  24. if True: # [using-constant-test]
  25. pass
  26. if '': # [using-constant-test]
  27. pass
  28. if b'': # [using-constant-test]
  29. pass
  30. if 2.0: # [using-constant-test]
  31. pass
  32. if {}: # [using-constant-test]
  33. pass
  34. if {1, 2, 3}: # [using-constant-test]
  35. pass
  36. if (1, 2, 3): # [using-constant-test]
  37. pass
  38. if (): # [using-constant-test]
  39. pass
  40. # Generator
  41. generator = function()
  42. if generator: # [using-constant-test]
  43. pass
  44. if 1 if 2 else 3: # [using-constant-test]
  45. pass
  46. def test_comprehensions():
  47. [data for data in range(100) if len] # [using-constant-test]
  48. [data for data in range(100) if 1] # [using-constant-test]
  49. (data for data in range(100) if len) # [using-constant-test]
  50. (data for data in range(100) if 1) # [using-constant-test]
  51. {data for data in range(100) if len} # [using-constant-test]
  52. {data: 1 for data in range(100) if len} # [using-constant-test]
  53. # For these, we require to do inference, even though the result can be a
  54. # constant value. For some of them, we could determine that the test
  55. # is constant, such as 2 + 3, but the components of the BinOp
  56. # can be anything else (2 + somefunccall).
  57. name = 42
  58. if name:
  59. pass
  60. # UnboundMethod / Function
  61. if Class.method:
  62. pass
  63. # BoundMethod
  64. if instance.method:
  65. pass
  66. if 3 + 4:
  67. pass
  68. if 3 and 4:
  69. pass
  70. if not 3:
  71. pass
  72. if instance.method():
  73. pass
  74. # pylint: disable=misplaced-comparison-constant
  75. if 2 < 3:
  76. pass
  77. if tuple((1, 2, 3)):
  78. pass
  79. if dict():
  80. pass
  81. if tuple():
  82. pass
  83. if [1, 2, 3][:1]:
  84. pass
  85. def test(*args):
  86. if args:
  87. return 42
  88. return None
  89. def test_good_comprehension_checks():
  90. [data for data in range(100)]
  91. [data for data in range(100) if data]
  92. [data for data in range(100) if len(data)]
  93. (data for data in range(100) if data)
  94. (data for data in range(100) if len(data))
  95. {data for data in range(100) if data}
  96. {data for data in range(100) if len(data)}
  97. {data: 1 for data in range(100) if data}
  98. {data: 1 for data in range(100)}