test_is_literal.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. from pyflakes.messages import IsLiteral
  2. from pyflakes.test.harness import TestCase
  3. class Test(TestCase):
  4. def test_is_str(self):
  5. self.flakes("""
  6. x = 'foo'
  7. if x is 'foo':
  8. pass
  9. """, IsLiteral)
  10. def test_is_bytes(self):
  11. self.flakes("""
  12. x = b'foo'
  13. if x is b'foo':
  14. pass
  15. """, IsLiteral)
  16. def test_is_unicode(self):
  17. self.flakes("""
  18. x = u'foo'
  19. if x is u'foo':
  20. pass
  21. """, IsLiteral)
  22. def test_is_int(self):
  23. self.flakes("""
  24. x = 10
  25. if x is 10:
  26. pass
  27. """, IsLiteral)
  28. def test_is_true(self):
  29. self.flakes("""
  30. x = True
  31. if x is True:
  32. pass
  33. """)
  34. def test_is_false(self):
  35. self.flakes("""
  36. x = False
  37. if x is False:
  38. pass
  39. """)
  40. def test_is_not_str(self):
  41. self.flakes("""
  42. x = 'foo'
  43. if x is not 'foo':
  44. pass
  45. """, IsLiteral)
  46. def test_is_not_bytes(self):
  47. self.flakes("""
  48. x = b'foo'
  49. if x is not b'foo':
  50. pass
  51. """, IsLiteral)
  52. def test_is_not_unicode(self):
  53. self.flakes("""
  54. x = u'foo'
  55. if x is not u'foo':
  56. pass
  57. """, IsLiteral)
  58. def test_is_not_int(self):
  59. self.flakes("""
  60. x = 10
  61. if x is not 10:
  62. pass
  63. """, IsLiteral)
  64. def test_is_not_true(self):
  65. self.flakes("""
  66. x = True
  67. if x is not True:
  68. pass
  69. """)
  70. def test_is_not_false(self):
  71. self.flakes("""
  72. x = False
  73. if x is not False:
  74. pass
  75. """)
  76. def test_left_is_str(self):
  77. self.flakes("""
  78. x = 'foo'
  79. if 'foo' is x:
  80. pass
  81. """, IsLiteral)
  82. def test_left_is_bytes(self):
  83. self.flakes("""
  84. x = b'foo'
  85. if b'foo' is x:
  86. pass
  87. """, IsLiteral)
  88. def test_left_is_unicode(self):
  89. self.flakes("""
  90. x = u'foo'
  91. if u'foo' is x:
  92. pass
  93. """, IsLiteral)
  94. def test_left_is_int(self):
  95. self.flakes("""
  96. x = 10
  97. if 10 is x:
  98. pass
  99. """, IsLiteral)
  100. def test_left_is_true(self):
  101. self.flakes("""
  102. x = True
  103. if True is x:
  104. pass
  105. """)
  106. def test_left_is_false(self):
  107. self.flakes("""
  108. x = False
  109. if False is x:
  110. pass
  111. """)
  112. def test_left_is_not_str(self):
  113. self.flakes("""
  114. x = 'foo'
  115. if 'foo' is not x:
  116. pass
  117. """, IsLiteral)
  118. def test_left_is_not_bytes(self):
  119. self.flakes("""
  120. x = b'foo'
  121. if b'foo' is not x:
  122. pass
  123. """, IsLiteral)
  124. def test_left_is_not_unicode(self):
  125. self.flakes("""
  126. x = u'foo'
  127. if u'foo' is not x:
  128. pass
  129. """, IsLiteral)
  130. def test_left_is_not_int(self):
  131. self.flakes("""
  132. x = 10
  133. if 10 is not x:
  134. pass
  135. """, IsLiteral)
  136. def test_left_is_not_true(self):
  137. self.flakes("""
  138. x = True
  139. if True is not x:
  140. pass
  141. """)
  142. def test_left_is_not_false(self):
  143. self.flakes("""
  144. x = False
  145. if False is not x:
  146. pass
  147. """)
  148. def test_chained_operators_is_true(self):
  149. self.flakes("""
  150. x = 5
  151. if x is True < 4:
  152. pass
  153. """)
  154. def test_chained_operators_is_str(self):
  155. self.flakes("""
  156. x = 5
  157. if x is 'foo' < 4:
  158. pass
  159. """, IsLiteral)
  160. def test_chained_operators_is_true_end(self):
  161. self.flakes("""
  162. x = 5
  163. if 4 < x is True:
  164. pass
  165. """)
  166. def test_chained_operators_is_str_end(self):
  167. self.flakes("""
  168. x = 5
  169. if 4 < x is 'foo':
  170. pass
  171. """, IsLiteral)
  172. def test_is_tuple_constant(self):
  173. self.flakes('''\
  174. x = 5
  175. if x is ():
  176. pass
  177. ''', IsLiteral)
  178. def test_is_tuple_constant_containing_constants(self):
  179. self.flakes('''\
  180. x = 5
  181. if x is (1, '2', True, (1.5, ())):
  182. pass
  183. ''', IsLiteral)
  184. def test_is_tuple_containing_variables_ok(self):
  185. # a bit nonsensical, but does not trigger a SyntaxWarning
  186. self.flakes('''\
  187. x = 5
  188. if x is (x,):
  189. pass
  190. ''')