summaryrefslogtreecommitdiff
path: root/tests/functional/a/arguments.py
blob: 81a9bb73d9b8529c564fae6d6f4427dd53e3f9f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# pylint: disable=too-few-public-methods, missing-docstring,import-error,wrong-import-position
# pylint: disable=wrong-import-order, unnecessary-lambda, consider-using-f-string
# pylint: disable=unnecessary-lambda-assignment, no-self-argument, unused-argument, kwarg-superseded-by-positional-arg

def decorator(fun):
    """Decorator"""
    return fun


class DemoClass:
    """Test class for method invocations."""

    @staticmethod
    def static_method(arg):
        """static method."""
        return arg + arg

    @classmethod
    def class_method(cls, arg):
        """class method"""
        return arg + arg

    def method(self, arg):
        """method."""
        return (self, arg)

    @decorator
    def decorated_method(self, arg):
        """decorated method."""
        return (self, arg)


def function_1_arg(first_argument):
    """one argument function"""
    return first_argument

def function_3_args(first_argument, second_argument, third_argument):
    """three arguments function"""
    return first_argument, second_argument, third_argument

def function_default_arg(one=1, two=2):
    """function with default value"""
    return two, one


function_1_arg(420)
function_1_arg()  # [no-value-for-parameter]
function_1_arg(1337, 347)  # [too-many-function-args]

function_3_args(420, 789)  # [no-value-for-parameter]
# +1:[no-value-for-parameter,no-value-for-parameter,no-value-for-parameter]
function_3_args()
function_3_args(1337, 347, 456)
function_3_args('bab', 'bebe', None, 5.6)  # [too-many-function-args]

function_default_arg(1, two=5)
function_default_arg(two=5)

function_1_arg(bob=4)  # [unexpected-keyword-arg,no-value-for-parameter]
function_default_arg(1, 4, coin="hello")  # [unexpected-keyword-arg]

function_default_arg(1, one=5)  # [redundant-keyword-arg]

# Remaining tests are for coverage of correct names in messages.
LAMBDA = lambda arg: 1

LAMBDA()  # [no-value-for-parameter]

def method_tests():
    """Method invocations."""
    demo = DemoClass()
    demo.static_method()  # [no-value-for-parameter]
    DemoClass.static_method()  # [no-value-for-parameter]

    demo.class_method()  # [no-value-for-parameter]
    DemoClass.class_method()  # [no-value-for-parameter]

    demo.method()  # [no-value-for-parameter]
    DemoClass.method(demo)  # [no-value-for-parameter]

    demo.decorated_method()  # [no-value-for-parameter]
    DemoClass.decorated_method(demo)  # [no-value-for-parameter]

# Test a regression (issue #234)
import sys

class Text:
    """ Regression """

    if sys.version_info > (3,):
        def __new__(cls):
            """ empty """
            return object.__new__(cls)
    else:
        def __new__(cls):
            """ empty """
            return object.__new__(cls)

Text()

class TestStaticMethod:

    @staticmethod
    def test(first, second=None, **kwargs):
        return first, second, kwargs

    def func(self):
        self.test(42)
        self.test(42, second=34)
        self.test(42, 42)
        self.test() # [no-value-for-parameter]
        self.test(42, 42, 42) # [too-many-function-args]


class TypeCheckConstructor:
    def __init__(self, first, second):
        self.first = first
        self.second = second
    def test(self):
        type(self)(1, 2, 3) # [too-many-function-args]
        # +1: [no-value-for-parameter,no-value-for-parameter]
        type(self)()
        type(self)(1, lala=2) # [no-value-for-parameter,unexpected-keyword-arg]
        type(self)(1, 2)
        type(self)(first=1, second=2)


class Test:
    """ lambda needs Test instance as first argument """
    lam = lambda self, icon: (self, icon)

    def test(self):
        self.lam(42)
        self.lam() # [no-value-for-parameter]
        self.lam(1, 2, 3) # [too-many-function-args]

Test().lam() # [no-value-for-parameter]

# Don't emit a redundant-keyword-arg for this example,
# it's perfectly valid

class Issue642:
    attr = 0
    def __str__(self):
        return "{self.attr}".format(self=self)

# These should not emit anything regarding the number of arguments,
# since they have something invalid.
from ala_bala_portocola import unknown

# pylint: disable=not-a-mapping,not-an-iterable

function_1_arg(*unknown)
function_1_arg(1, *2)
function_1_arg(1, 2, 3, **unknown)
function_1_arg(4, 5, **1)
function_1_arg(5, 6, **{unknown: 1})
function_1_arg(**{object: 1})
function_1_arg(**{1: 2})

def no_context_but_redefined(*args):
    args = [1]
    #+1: [no-value-for-parameter, no-value-for-parameter]
    expect_three(*list(args))

def no_context_one_elem(*args):
    expect_three(args) # [no-value-for-parameter, no-value-for-parameter]

# Don't emit no-value-for-parameter for this, since we
# don't have the context at our disposal.
def expect_three(one, two, three):
    return one + two + three


def no_context(*args):
    expect_three(*args)

def no_context_two(*args):
    expect_three(*list(args))

def no_context_three(*args):
    expect_three(*set(args))

def compare_prices(arg):
    return set((arg, ))

def find_problems2(prob_dates):
    for fff in range(10):
        prob_dates |= compare_prices(fff)


from collections import namedtuple


def namedtuple_replace_issue_1036():
    cls = namedtuple('cls', 'a b c')
    new_instance = cls(1, 2, 3)._replace(
        a=24,
        b=24,
        c=42
    )
    # +1:[unexpected-keyword-arg,unexpected-keyword-arg]
    new_bad_instance = cls(1, 2, 3)._replace(d=24, e=32)
    return new_instance, new_bad_instance


from functools import partial

def some_func(first, second, third):
    return first + second + third


partial(some_func, 1, 2)(3)
partial(some_func, third=1, second=2)(3)
partial(some_func, 1, third=2)(second=3)
partial(some_func, 1)(1)  # [no-value-for-parameter]
partial(some_func, 1)(third=1)  # [no-value-for-parameter]
partial(some_func, 1, 2)(third=1, fourth=4)  # [unexpected-keyword-arg]


def mutation_decorator(fun):
    """Decorator that changes a function's signature."""
    def wrapper(*args, do_something=True, **kwargs):
        if do_something:
            return fun(*args, **kwargs)

        return None

    return wrapper


def other_mutation_decorator(fun):
    """Another decorator that changes a function's signature"""
    def wrapper(*args, do_something=True, **kwargs):
        if do_something:
            return fun(*args, **kwargs)

        return None

    return wrapper


@mutation_decorator
def mutated_function(arg):
    return arg


@other_mutation_decorator
def mutated(arg):
    return arg


mutated_function(do_something=False)
mutated_function()

mutated(do_something=True)


def func(one, two, three):
    return one + two + three


CALL = lambda *args: func(*args)


# Ensure `too-many-function-args` is not emitted when a function call is assigned
# to a class attribute inside the class where the function is defined.
# Reference: https://github.com/pylint-dev/pylint/issues/6592
class FruitPicker:
    def _pick_fruit(fruit):
        def _print_selection(self):
            print(f"Selected: {fruit}!")
        return _print_selection

    pick_apple = _pick_fruit("apple")
    pick_pear = _pick_fruit("pear")

picker = FruitPicker()
picker.pick_apple()
picker.pick_pear()


def name1(apple, /, **kwargs):
    """
    Positional-only parameter with `**kwargs`.
    Calling this function with the `apple` keyword should not emit
    `redundant-keyword-arg` since it is added to `**kwargs`.

    >>> name1("Red apple", apple="Green apple")
    "Red apple"
    {"apple": "Green apple"}
    """
    print(apple)
    print(kwargs)


name1("Red apple", apple="Green apple")


def name2(apple, /, banana, **kwargs):
    """
    Positional-only parameter with positional-or-keyword parameter and `**kwargs`.
    """


# `banana` is redundant
# +1:[redundant-keyword-arg]
name2("Red apple", "Yellow banana", apple="Green apple", banana="Green banana")


# Test `no-value-for-parameter` in the context of positional-only parameters

def name3(param1, /, **kwargs): ...
def name4(param1, /, param2, **kwargs): ...
def name5(param1=True, /, **kwargs): ...
def name6(param1, **kwargs): ...

name3(param1=43)  # [no-value-for-parameter]
name3(43)
name4(1, param2=False)
name5()
name6(param1=43)