Skip to content

Commit

Permalink
Can choose decorators that mutate a function's signature
Browse files Browse the repository at this point in the history
Close #259
  • Loading branch information
AWhetter committed May 19, 2019
1 parent fc569a6 commit 715b26f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ Release date: TBA

Close #2877

* Can choose to ignore `too-many-function-args`, `unexpected-keyword-arg`,
and `no-value-for-parameter` for functions decorated with certain decorators.

Close #259


What's New in Pylint 2.3.0?
===========================
Expand Down
11 changes: 11 additions & 0 deletions doc/whatsnew/2.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,14 @@ The following does not trigger a ``missing-return-doc`` anymore ::
List of strings
"""
return ["hi", "bye"] #@

* Can choose to ignore `too-many-function-args`, `unexpected-keyword-arg`,
and `no-value-for-parameter` for functions decorated with certain decorators.

For example a test may want to make use of hypothesis.
Adding `hypothesis.extra.numpy.arrays` to `function_mutators`
would mean that `no-value-for-parameter` would not be raised for:

@given(img=arrays(dtype=np.float32, shape=(3, 3, 3, 3)))
def test_image(img):
...
16 changes: 16 additions & 0 deletions pylint/checkers/typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,16 @@ class should be ignored. A mixin class is detected if its name ends with \
"found. The aspect of finding the hint is based on edit distance.",
},
),
(
"signature-mutators",
{
"default": [],
"type": "csv",
"metavar": "<decorator names>",
"help": "List of decorators that change the signature of "
"a decorated function.",
},
),
)

@decorators.cachedproperty
Expand Down Expand Up @@ -1091,6 +1101,12 @@ def visit_call(self, node):
# Can't make sense of this.
return

# Has the function signature changed in ways we cannot reliably detect?
if hasattr(called, "decorators") and decorated_with(
called, self.config.signature_mutators
):
return

num_positional_args = len(call_site.positional_arguments)
keyword_args = list(call_site.keyword_arguments.keys())

Expand Down
20 changes: 20 additions & 0 deletions pylint/test/functional/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,23 @@ def some_func(first, second, third):
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


@mutation_decorator
def mutated_function(arg):
return arg


mutated_function(do_something=False)
mutated_function()
2 changes: 2 additions & 0 deletions pylint/test/functional/arguments.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[TYPECHECK]
signature-mutators=functional.arguments.mutation_decorator

0 comments on commit 715b26f

Please sign in to comment.