PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/pyflakes/test/test_other.py

https://gitlab.com/SrinivasDasthagiri/pyflakes
Python | 575 lines | 509 code | 7 blank | 59 comment | 19 complexity | 2197ea333deaed1044ff4fbc80f5b794 MD5 | raw file
  1. # (c) 2005-2010 Divmod, Inc.
  2. # See LICENSE file for details
  3. """
  4. Tests for various Pyflakes behavior.
  5. """
  6. from sys import version_info
  7. from pyflakes import messages as m
  8. from pyflakes.test import harness
  9. class Test(harness.Test):
  10. def test_duplicateArgs(self):
  11. self.flakes('def fu(bar, bar): pass', m.DuplicateArgument)
  12. def test_localReferencedBeforeAssignment(self):
  13. self.flakes('''
  14. a = 1
  15. def f():
  16. a; a=1
  17. f()
  18. ''', m.UndefinedName)
  19. test_localReferencedBeforeAssignment.todo = 'this requires finding all assignments in the function body first'
  20. def test_redefinedFunction(self):
  21. """
  22. Test that shadowing a function definition with another one raises a
  23. warning.
  24. """
  25. self.flakes('''
  26. def a(): pass
  27. def a(): pass
  28. ''', m.RedefinedFunction)
  29. def test_redefinedClassFunction(self):
  30. """
  31. Test that shadowing a function definition in a class suite with another
  32. one raises a warning.
  33. """
  34. self.flakes('''
  35. class A:
  36. def a(): pass
  37. def a(): pass
  38. ''', m.RedefinedFunction)
  39. def test_functionDecorator(self):
  40. """
  41. Test that shadowing a function definition with a decorated version of
  42. that function does not raise a warning.
  43. """
  44. self.flakes('''
  45. from somewhere import somedecorator
  46. def a(): pass
  47. a = somedecorator(a)
  48. ''')
  49. def test_classFunctionDecorator(self):
  50. """
  51. Test that shadowing a function definition in a class suite with a
  52. decorated version of that function does not raise a warning.
  53. """
  54. self.flakes('''
  55. class A:
  56. def a(): pass
  57. a = classmethod(a)
  58. ''')
  59. def test_unaryPlus(self):
  60. '''Don't die on unary +'''
  61. self.flakes('+1')
  62. def test_undefinedBaseClass(self):
  63. """
  64. If a name in the base list of a class definition is undefined, a
  65. warning is emitted.
  66. """
  67. self.flakes('''
  68. class foo(foo):
  69. pass
  70. ''', m.UndefinedName)
  71. def test_classNameUndefinedInClassBody(self):
  72. """
  73. If a class name is used in the body of that class's definition and
  74. the name is not already defined, a warning is emitted.
  75. """
  76. self.flakes('''
  77. class foo:
  78. foo
  79. ''', m.UndefinedName)
  80. def test_classNameDefinedPreviously(self):
  81. """
  82. If a class name is used in the body of that class's definition and
  83. the name was previously defined in some other way, no warning is
  84. emitted.
  85. """
  86. self.flakes('''
  87. foo = None
  88. class foo:
  89. foo
  90. ''')
  91. def test_comparison(self):
  92. """
  93. If a defined name is used on either side of any of the six comparison
  94. operators, no warning is emitted.
  95. """
  96. self.flakes('''
  97. x = 10
  98. y = 20
  99. x < y
  100. x <= y
  101. x == y
  102. x != y
  103. x >= y
  104. x > y
  105. ''')
  106. def test_identity(self):
  107. """
  108. If a deefined name is used on either side of an identity test, no
  109. warning is emitted.
  110. """
  111. self.flakes('''
  112. x = 10
  113. y = 20
  114. x is y
  115. x is not y
  116. ''')
  117. def test_containment(self):
  118. """
  119. If a defined name is used on either side of a containment test, no
  120. warning is emitted.
  121. """
  122. self.flakes('''
  123. x = 10
  124. y = 20
  125. x in y
  126. x not in y
  127. ''')
  128. def test_loopControl(self):
  129. """
  130. break and continue statements are supported.
  131. """
  132. self.flakes('''
  133. for x in [1, 2]:
  134. break
  135. ''')
  136. self.flakes('''
  137. for x in [1, 2]:
  138. continue
  139. ''')
  140. def test_ellipsis(self):
  141. """
  142. Ellipsis in a slice is supported.
  143. """
  144. self.flakes('''
  145. [1, 2][...]
  146. ''')
  147. def test_extendedSlice(self):
  148. """
  149. Extended slices are supported.
  150. """
  151. self.flakes('''
  152. x = 3
  153. [1, 2][x,:]
  154. ''')
  155. class TestUnusedAssignment(harness.Test):
  156. """
  157. Tests for warning about unused assignments.
  158. """
  159. def test_unusedVariable(self):
  160. """
  161. Warn when a variable in a function is assigned a value that's never
  162. used.
  163. """
  164. self.flakes('''
  165. def a():
  166. b = 1
  167. ''', m.UnusedVariable)
  168. def test_assignToGlobal(self):
  169. """
  170. Assigning to a global and then not using that global is perfectly
  171. acceptable. Do not mistake it for an unused local variable.
  172. """
  173. self.flakes('''
  174. b = 0
  175. def a():
  176. global b
  177. b = 1
  178. ''')
  179. def test_assignToMember(self):
  180. """
  181. Assigning to a member of another object and then not using that member
  182. variable is perfectly acceptable. Do not mistake it for an unused
  183. local variable.
  184. """
  185. # XXX: Adding this test didn't generate a failure. Maybe not
  186. # necessary?
  187. self.flakes('''
  188. class b:
  189. pass
  190. def a():
  191. b.foo = 1
  192. ''')
  193. def test_assignInForLoop(self):
  194. """
  195. Don't warn when a variable in a for loop is assigned to but not used.
  196. """
  197. self.flakes('''
  198. def f():
  199. for i in range(10):
  200. pass
  201. ''')
  202. def test_assignInListComprehension(self):
  203. """
  204. Don't warn when a variable in a list comprehension is assigned to but
  205. not used.
  206. """
  207. self.flakes('''
  208. def f():
  209. [None for i in range(10)]
  210. ''')
  211. def test_generatorExpression(self):
  212. """
  213. Don't warn when a variable in a generator expression is assigned to but not used.
  214. """
  215. self.flakes('''
  216. def f():
  217. (None for i in range(10))
  218. ''')
  219. def test_assignmentInsideLoop(self):
  220. """
  221. Don't warn when a variable assignment occurs lexically after its use.
  222. """
  223. self.flakes('''
  224. def f():
  225. x = None
  226. for i in range(10):
  227. if i > 2:
  228. return x
  229. x = i * 2
  230. ''')
  231. def test_tupleUnpacking(self):
  232. """
  233. Don't warn when a variable included in tuple unpacking is unused. It's
  234. very common for variables in a tuple unpacking assignment to be unused
  235. in good Python code, so warning will only create false positives.
  236. """
  237. self.flakes('''
  238. def f():
  239. (x, y) = 1, 2
  240. ''')
  241. def test_listUnpacking(self):
  242. """
  243. Don't warn when a variable included in list unpacking is unused.
  244. """
  245. self.flakes('''
  246. def f():
  247. [x, y] = [1, 2]
  248. ''')
  249. def test_closedOver(self):
  250. """
  251. Don't warn when the assignment is used in an inner function.
  252. """
  253. self.flakes('''
  254. def barMaker():
  255. foo = 5
  256. def bar():
  257. return foo
  258. return bar
  259. ''')
  260. def test_doubleClosedOver(self):
  261. """
  262. Don't warn when the assignment is used in an inner function, even if
  263. that inner function itself is in an inner function.
  264. """
  265. self.flakes('''
  266. def barMaker():
  267. foo = 5
  268. def bar():
  269. def baz():
  270. return foo
  271. return bar
  272. ''')
  273. class Python25Test(harness.Test):
  274. """
  275. Tests for checking of syntax only available in Python 2.5 and newer.
  276. """
  277. if version_info < (2, 5):
  278. skip = "Python 2.5 required for if-else and with tests"
  279. def test_ifexp(self):
  280. """
  281. Test C{foo if bar else baz} statements.
  282. """
  283. self.flakes("a = 'moo' if True else 'oink'")
  284. self.flakes("a = foo if True else 'oink'", m.UndefinedName)
  285. self.flakes("a = 'moo' if True else bar", m.UndefinedName)
  286. def test_withStatementNoNames(self):
  287. """
  288. No warnings are emitted for using inside or after a nameless C{with}
  289. statement a name defined beforehand.
  290. """
  291. self.flakes('''
  292. from __future__ import with_statement
  293. bar = None
  294. with open("foo"):
  295. bar
  296. bar
  297. ''')
  298. def test_withStatementSingleName(self):
  299. """
  300. No warnings are emitted for using a name defined by a C{with} statement
  301. within the suite or afterwards.
  302. """
  303. self.flakes('''
  304. from __future__ import with_statement
  305. with open('foo') as bar:
  306. bar
  307. bar
  308. ''')
  309. def test_withStatementAttributeName(self):
  310. """
  311. No warnings are emitted for using an attribute as the target of a
  312. C{with} statement.
  313. """
  314. self.flakes('''
  315. from __future__ import with_statement
  316. import foo
  317. with open('foo') as foo.bar:
  318. pass
  319. ''')
  320. def test_withStatementSubscript(self):
  321. """
  322. No warnings are emitted for using a subscript as the target of a
  323. C{with} statement.
  324. """
  325. self.flakes('''
  326. from __future__ import with_statement
  327. import foo
  328. with open('foo') as foo[0]:
  329. pass
  330. ''')
  331. def test_withStatementSubscriptUndefined(self):
  332. """
  333. An undefined name warning is emitted if the subscript used as the
  334. target of a C{with} statement is not defined.
  335. """
  336. self.flakes('''
  337. from __future__ import with_statement
  338. import foo
  339. with open('foo') as foo[bar]:
  340. pass
  341. ''', m.UndefinedName)
  342. def test_withStatementTupleNames(self):
  343. """
  344. No warnings are emitted for using any of the tuple of names defined by
  345. a C{with} statement within the suite or afterwards.
  346. """
  347. self.flakes('''
  348. from __future__ import with_statement
  349. with open('foo') as (bar, baz):
  350. bar, baz
  351. bar, baz
  352. ''')
  353. def test_withStatementListNames(self):
  354. """
  355. No warnings are emitted for using any of the list of names defined by a
  356. C{with} statement within the suite or afterwards.
  357. """
  358. self.flakes('''
  359. from __future__ import with_statement
  360. with open('foo') as [bar, baz]:
  361. bar, baz
  362. bar, baz
  363. ''')
  364. def test_withStatementComplicatedTarget(self):
  365. """
  366. If the target of a C{with} statement uses any or all of the valid forms
  367. for that part of the grammar (See
  368. U{http://docs.python.org/reference/compound_stmts.html#the-with-statement}),
  369. the names involved are checked both for definedness and any bindings
  370. created are respected in the suite of the statement and afterwards.
  371. """
  372. self.flakes('''
  373. from __future__ import with_statement
  374. c = d = e = g = h = i = None
  375. with open('foo') as [(a, b), c[d], e.f, g[h:i]]:
  376. a, b, c, d, e, g, h, i
  377. a, b, c, d, e, g, h, i
  378. ''')
  379. def test_withStatementSingleNameUndefined(self):
  380. """
  381. An undefined name warning is emitted if the name first defined by a
  382. C{with} statement is used before the C{with} statement.
  383. """
  384. self.flakes('''
  385. from __future__ import with_statement
  386. bar
  387. with open('foo') as bar:
  388. pass
  389. ''', m.UndefinedName)
  390. def test_withStatementTupleNamesUndefined(self):
  391. """
  392. An undefined name warning is emitted if a name first defined by a the
  393. tuple-unpacking form of the C{with} statement is used before the
  394. C{with} statement.
  395. """
  396. self.flakes('''
  397. from __future__ import with_statement
  398. baz
  399. with open('foo') as (bar, baz):
  400. pass
  401. ''', m.UndefinedName)
  402. def test_withStatementSingleNameRedefined(self):
  403. """
  404. A redefined name warning is emitted if a name bound by an import is
  405. rebound by the name defined by a C{with} statement.
  406. """
  407. self.flakes('''
  408. from __future__ import with_statement
  409. import bar
  410. with open('foo') as bar:
  411. pass
  412. ''', m.RedefinedWhileUnused)
  413. def test_withStatementTupleNamesRedefined(self):
  414. """
  415. A redefined name warning is emitted if a name bound by an import is
  416. rebound by one of the names defined by the tuple-unpacking form of a
  417. C{with} statement.
  418. """
  419. self.flakes('''
  420. from __future__ import with_statement
  421. import bar
  422. with open('foo') as (bar, baz):
  423. pass
  424. ''', m.RedefinedWhileUnused)
  425. def test_withStatementUndefinedInside(self):
  426. """
  427. An undefined name warning is emitted if a name is used inside the
  428. body of a C{with} statement without first being bound.
  429. """
  430. self.flakes('''
  431. from __future__ import with_statement
  432. with open('foo') as bar:
  433. baz
  434. ''', m.UndefinedName)
  435. def test_withStatementNameDefinedInBody(self):
  436. """
  437. A name defined in the body of a C{with} statement can be used after
  438. the body ends without warning.
  439. """
  440. self.flakes('''
  441. from __future__ import with_statement
  442. with open('foo') as bar:
  443. baz = 10
  444. baz
  445. ''')
  446. def test_withStatementUndefinedInExpression(self):
  447. """
  448. An undefined name warning is emitted if a name in the I{test}
  449. expression of a C{with} statement is undefined.
  450. """
  451. self.flakes('''
  452. from __future__ import with_statement
  453. with bar as baz:
  454. pass
  455. ''', m.UndefinedName)
  456. self.flakes('''
  457. from __future__ import with_statement
  458. with bar as bar:
  459. pass
  460. ''', m.UndefinedName)
  461. class Python27Test(harness.Test):
  462. """
  463. Tests for checking of syntax only available in Python 2.7 and newer.
  464. """
  465. if version_info < (2, 7):
  466. skip = "Python 2.7 required for dict/set comprehension tests"
  467. def test_dictComprehension(self):
  468. """
  469. Dict comprehensions are properly handled.
  470. """
  471. self.flakes('''
  472. a = {1: x for x in range(10)}
  473. ''')
  474. def test_setComprehensionAndLiteral(self):
  475. """
  476. Set comprehensions are properly handled.
  477. """
  478. self.flakes('''
  479. a = {1, 2, 3}
  480. b = {x for x in range(10)}
  481. ''')