Skip to content

Commit

Permalink
Can choose decorators that mutate a function's signature (#2926)
Browse files Browse the repository at this point in the history
Close #259
  • Loading branch information
AWhetter authored and PCManticore committed May 23, 2019
1 parent f4fc3a1 commit f90b223
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 @@ -125,6 +125,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

* Fixed a pragma comment on its own physical line being ignored when part
of a logical line with the previous physical line.

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 @@ -762,6 +762,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 @@ -1089,6 +1099,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 f90b223

Please sign in to comment.