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.

arguments.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # pylint: disable=too-few-public-methods, no-absolute-import,missing-docstring,import-error,wrong-import-position
  2. # pylint: disable=wrong-import-order
  3. def decorator(fun):
  4. """Decorator"""
  5. return fun
  6. class DemoClass(object):
  7. """Test class for method invocations."""
  8. @staticmethod
  9. def static_method(arg):
  10. """static method."""
  11. return arg + arg
  12. @classmethod
  13. def class_method(cls, arg):
  14. """class method"""
  15. return arg + arg
  16. def method(self, arg):
  17. """method."""
  18. return (self, arg)
  19. @decorator
  20. def decorated_method(self, arg):
  21. """decorated method."""
  22. return (self, arg)
  23. def function_1_arg(first_argument):
  24. """one argument function"""
  25. return first_argument
  26. def function_3_args(first_argument, second_argument, third_argument):
  27. """three arguments function"""
  28. return first_argument, second_argument, third_argument
  29. def function_default_arg(one=1, two=2):
  30. """fonction with default value"""
  31. return two, one
  32. function_1_arg(420)
  33. function_1_arg() # [no-value-for-parameter]
  34. function_1_arg(1337, 347) # [too-many-function-args]
  35. function_3_args(420, 789) # [no-value-for-parameter]
  36. # +1:[no-value-for-parameter,no-value-for-parameter,no-value-for-parameter]
  37. function_3_args()
  38. function_3_args(1337, 347, 456)
  39. function_3_args('bab', 'bebe', None, 5.6) # [too-many-function-args]
  40. function_default_arg(1, two=5)
  41. function_default_arg(two=5)
  42. function_1_arg(bob=4) # [unexpected-keyword-arg,no-value-for-parameter]
  43. function_default_arg(1, 4, coin="hello") # [unexpected-keyword-arg]
  44. function_default_arg(1, one=5) # [redundant-keyword-arg]
  45. # Remaining tests are for coverage of correct names in messages.
  46. LAMBDA = lambda arg: 1
  47. LAMBDA() # [no-value-for-parameter]
  48. def method_tests():
  49. """Method invocations."""
  50. demo = DemoClass()
  51. demo.static_method() # [no-value-for-parameter]
  52. DemoClass.static_method() # [no-value-for-parameter]
  53. demo.class_method() # [no-value-for-parameter]
  54. DemoClass.class_method() # [no-value-for-parameter]
  55. demo.method() # [no-value-for-parameter]
  56. DemoClass.method(demo) # [no-value-for-parameter]
  57. demo.decorated_method() # [no-value-for-parameter]
  58. DemoClass.decorated_method(demo) # [no-value-for-parameter]
  59. # Test a regression (issue #234)
  60. import sys
  61. class Text(object):
  62. """ Regression """
  63. if sys.version_info > (3,):
  64. def __new__(cls):
  65. """ empty """
  66. return object.__new__(cls)
  67. else:
  68. def __new__(cls):
  69. """ empty """
  70. return object.__new__(cls)
  71. Text()
  72. class TestStaticMethod(object):
  73. @staticmethod
  74. def test(first, second=None, **kwargs):
  75. return first, second, kwargs
  76. def func(self):
  77. self.test(42)
  78. self.test(42, second=34)
  79. self.test(42, 42)
  80. self.test() # [no-value-for-parameter]
  81. self.test(42, 42, 42) # [too-many-function-args]
  82. class TypeCheckConstructor(object):
  83. def __init__(self, first, second):
  84. self.first = first
  85. self.second = second
  86. def test(self):
  87. type(self)(1, 2, 3) # [too-many-function-args]
  88. # +1: [no-value-for-parameter,no-value-for-parameter]
  89. type(self)()
  90. type(self)(1, lala=2) # [no-value-for-parameter,unexpected-keyword-arg]
  91. type(self)(1, 2)
  92. type(self)(first=1, second=2)
  93. class Test(object):
  94. """ lambda needs Test instance as first argument """
  95. lam = lambda self, icon: (self, icon)
  96. def test(self):
  97. self.lam(42)
  98. self.lam() # [no-value-for-parameter]
  99. self.lam(1, 2, 3) # [too-many-function-args]
  100. Test().lam() # [no-value-for-parameter]
  101. # Don't emit a redundant-keyword-arg for this example,
  102. # it's perfectly valid
  103. class Issue642(object):
  104. attr = 0
  105. def __str__(self):
  106. return "{self.attr}".format(self=self)
  107. # These should not emit anything regarding the number of arguments,
  108. # since they have something invalid.
  109. from ala_bala_portocola import unknown
  110. # pylint: disable=not-a-mapping,not-an-iterable
  111. function_1_arg(*unknown)
  112. function_1_arg(1, *2)
  113. function_1_arg(1, 2, 3, **unknown)
  114. function_1_arg(4, 5, **1)
  115. function_1_arg(5, 6, **{unknown: 1})
  116. function_1_arg(**{object: 1})
  117. function_1_arg(**{1: 2})
  118. def no_context_but_redefined(*args):
  119. args = [1]
  120. #+1: [no-value-for-parameter, no-value-for-parameter]
  121. expect_three(*list(args))
  122. def no_context_one_elem(*args):
  123. expect_three(args) # [no-value-for-parameter, no-value-for-parameter]
  124. # Don't emit no-value-for-parameter for this, since we
  125. # don't have the context at our disposal.
  126. def expect_three(one, two, three):
  127. return one + two + three
  128. def no_context(*args):
  129. expect_three(*args)
  130. def no_context_two(*args):
  131. expect_three(*list(args))
  132. def no_context_three(*args):
  133. expect_three(*set(args))
  134. def compare_prices(arg):
  135. return set((arg, ))
  136. def find_problems2(prob_dates):
  137. for fff in range(10):
  138. prob_dates |= compare_prices(fff)
  139. from collections import namedtuple
  140. def namedtuple_replace_issue_1036():
  141. cls = namedtuple('cls', 'a b c')
  142. new_instance = cls(1, 2, 3)._replace(
  143. a=24,
  144. b=24,
  145. c=42
  146. )
  147. # +1:[unexpected-keyword-arg,unexpected-keyword-arg]
  148. new_bad_instance = cls(1, 2, 3)._replace(d=24, e=32)
  149. return new_instance, new_bad_instance