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.

unbalanced_tuple_unpacking.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """Check possible unbalanced tuple unpacking """
  2. from __future__ import absolute_import
  3. from functional.unpacking import unpack
  4. # pylint: disable=using-constant-test
  5. def do_stuff():
  6. """This is not right."""
  7. first, second = 1, 2, 3 # [unbalanced-tuple-unpacking]
  8. return first + second
  9. def do_stuff1():
  10. """This is not right."""
  11. first, second = [1, 2, 3] # [unbalanced-tuple-unpacking]
  12. return first + second
  13. def do_stuff2():
  14. """This is not right."""
  15. (first, second) = 1, 2, 3 # [unbalanced-tuple-unpacking]
  16. return first + second
  17. def do_stuff3():
  18. """This is not right."""
  19. first, second = range(100)
  20. return first + second
  21. def do_stuff4():
  22. """ This is right """
  23. first, second = 1, 2
  24. return first + second
  25. def do_stuff5():
  26. """ This is also right """
  27. first, second = (1, 2)
  28. return first + second
  29. def do_stuff6():
  30. """ This is right """
  31. (first, second) = (1, 2)
  32. return first + second
  33. def temp():
  34. """ This is not weird """
  35. if True:
  36. return [1, 2]
  37. return [2, 3, 4]
  38. def do_stuff7():
  39. """ This is not right, but we're not sure """
  40. first, second = temp()
  41. return first + second
  42. def temp2():
  43. """ This is weird, but correct """
  44. if True:
  45. return (1, 2)
  46. else:
  47. if True:
  48. return (2, 3)
  49. return (4, 5)
  50. def do_stuff8():
  51. """ This is correct """
  52. first, second = temp2()
  53. return first + second
  54. def do_stuff9():
  55. """ This is not correct """
  56. first, second = unpack() # [unbalanced-tuple-unpacking]
  57. return first + second
  58. class UnbalancedUnpacking(object):
  59. """ Test unbalanced tuple unpacking in instance attributes. """
  60. # pylint: disable=attribute-defined-outside-init, invalid-name, too-few-public-methods
  61. def test(self):
  62. """ unpacking in instance attributes """
  63. # we're not sure if temp() returns two or three values
  64. # so we shouldn't emit an error
  65. self.a, self.b = temp()
  66. self.a, self.b = temp2()
  67. self.a, self.b = unpack() # [unbalanced-tuple-unpacking]
  68. def issue329(*args):
  69. """ Don't emit unbalanced tuple unpacking if the
  70. rhs of the assignment is a variable-length argument,
  71. because we don't know the actual length of the tuple.
  72. """
  73. first, second, third = args
  74. return first, second, third
  75. def test_decimal():
  76. """Test a false positive with decimal.Decimal.as_tuple
  77. See astroid https://bitbucket.org/logilab/astroid/issues/92/
  78. """
  79. from decimal import Decimal
  80. dec = Decimal(2)
  81. first, second, third = dec.as_tuple()
  82. return first, second, third
  83. def test_issue_559():
  84. """Test that we don't have a false positive wrt to issue #559."""
  85. from ctypes import c_int
  86. root_x, root_y, win_x, win_y = [c_int()] * 4
  87. return root_x, root_y, win_x, win_y