Skip to content

Commit f90b223

Browse files
AWhetterPCManticore
authored andcommitted
Can choose decorators that mutate a function's signature (#2926)
Close #259
1 parent f4fc3a1 commit f90b223

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ Release date: TBA
125125

126126
Close #2877
127127

128+
* Can choose to ignore `too-many-function-args`, `unexpected-keyword-arg`,
129+
and `no-value-for-parameter` for functions decorated with certain decorators.
130+
131+
Close #259
132+
128133
* Fixed a pragma comment on its own physical line being ignored when part
129134
of a logical line with the previous physical line.
130135

doc/whatsnew/2.4.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,14 @@ The following does not trigger a ``missing-return-doc`` anymore ::
116116
List of strings
117117
"""
118118
return ["hi", "bye"] #@
119+
120+
* Can choose to ignore `too-many-function-args`, `unexpected-keyword-arg`,
121+
and `no-value-for-parameter` for functions decorated with certain decorators.
122+
123+
For example a test may want to make use of hypothesis.
124+
Adding `hypothesis.extra.numpy.arrays` to `function_mutators`
125+
would mean that `no-value-for-parameter` would not be raised for::
126+
127+
@given(img=arrays(dtype=np.float32, shape=(3, 3, 3, 3)))
128+
def test_image(img):
129+
...

pylint/checkers/typecheck.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,16 @@ class should be ignored. A mixin class is detected if its name ends with \
762762
"found. The aspect of finding the hint is based on edit distance.",
763763
},
764764
),
765+
(
766+
"signature-mutators",
767+
{
768+
"default": [],
769+
"type": "csv",
770+
"metavar": "<decorator names>",
771+
"help": "List of decorators that change the signature of "
772+
"a decorated function.",
773+
},
774+
),
765775
)
766776

767777
@decorators.cachedproperty
@@ -1089,6 +1099,12 @@ def visit_call(self, node):
10891099
# Can't make sense of this.
10901100
return
10911101

1102+
# Has the function signature changed in ways we cannot reliably detect?
1103+
if hasattr(called, "decorators") and decorated_with(
1104+
called, self.config.signature_mutators
1105+
):
1106+
return
1107+
10921108
num_positional_args = len(call_site.positional_arguments)
10931109
keyword_args = list(call_site.keyword_arguments.keys())
10941110

pylint/test/functional/arguments.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,23 @@ def some_func(first, second, third):
215215
partial(some_func, 1)(1) # [no-value-for-parameter]
216216
partial(some_func, 1)(third=1) # [no-value-for-parameter]
217217
partial(some_func, 1, 2)(third=1, fourth=4) # [unexpected-keyword-arg]
218+
219+
220+
def mutation_decorator(fun):
221+
"""Decorator that changes a function's signature."""
222+
def wrapper(*args, do_something=True, **kwargs):
223+
if do_something:
224+
return fun(*args, **kwargs)
225+
226+
return None
227+
228+
return wrapper
229+
230+
231+
@mutation_decorator
232+
def mutated_function(arg):
233+
return arg
234+
235+
236+
mutated_function(do_something=False)
237+
mutated_function()

pylint/test/functional/arguments.rc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[TYPECHECK]
2+
signature-mutators=functional.arguments.mutation_decorator

0 commit comments

Comments
 (0)