Skip to content

Commit 35e1c61

Browse files
committed
Can choose decorators that mutate a function's signature
Close #259
1 parent ace1877 commit 35e1c61

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
@@ -117,6 +117,11 @@ Release date: TBA
117117

118118
Close #2877
119119

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+
Close #259
124+
120125

121126
What's New in Pylint 2.3.0?
122127
===========================

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
@@ -765,6 +765,16 @@ class should be ignored. A mixin class is detected if its name ends with \
765765
"found. The aspect of finding the hint is based on edit distance.",
766766
},
767767
),
768+
(
769+
"signature-mutators",
770+
{
771+
"default": [],
772+
"type": "csv",
773+
"metavar": "<decorator names>",
774+
"help": "List of decorators that change the signature of "
775+
"a decorated function.",
776+
},
777+
),
768778
)
769779

770780
@decorators.cachedproperty
@@ -1091,6 +1101,12 @@ def visit_call(self, node):
10911101
# Can't make sense of this.
10921102
return
10931103

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

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)