diff options
author | Ashley Whetter <AWhetter@users.noreply.github.com> | 2019-05-22 23:17:33 -0700 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2019-05-23 08:17:33 +0200 |
commit | f90b223a5ea70a0b9a52034280fe22638e6ef14d (patch) | |
tree | 80611757a3dc94a7f19b5d734fdc73e1517e562b | |
parent | f4fc3a1bbcb58a2f4d110e9bbc76d57f29172ada (diff) | |
download | pylint-git-f90b223a5ea70a0b9a52034280fe22638e6ef14d.tar.gz |
Can choose decorators that mutate a function's signature (#2926)
Close #259
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | doc/whatsnew/2.4.rst | 11 | ||||
-rw-r--r-- | pylint/checkers/typecheck.py | 16 | ||||
-rw-r--r-- | pylint/test/functional/arguments.py | 20 | ||||
-rw-r--r-- | pylint/test/functional/arguments.rc | 2 |
5 files changed, 54 insertions, 0 deletions
@@ -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. diff --git a/doc/whatsnew/2.4.rst b/doc/whatsnew/2.4.rst index 42fe36c2c..fa9cc5d3b 100644 --- a/doc/whatsnew/2.4.rst +++ b/doc/whatsnew/2.4.rst @@ -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): + ... diff --git a/pylint/checkers/typecheck.py b/pylint/checkers/typecheck.py index d814918d6..85454c4a0 100644 --- a/pylint/checkers/typecheck.py +++ b/pylint/checkers/typecheck.py @@ -762,6 +762,16 @@ accessed. Python regular expressions are accepted.", "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 @@ -1089,6 +1099,12 @@ accessed. Python regular expressions are accepted.", # 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()) diff --git a/pylint/test/functional/arguments.py b/pylint/test/functional/arguments.py index ea833ebb7..d023244d6 100644 --- a/pylint/test/functional/arguments.py +++ b/pylint/test/functional/arguments.py @@ -215,3 +215,23 @@ 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 + + +@mutation_decorator +def mutated_function(arg): + return arg + + +mutated_function(do_something=False) +mutated_function() diff --git a/pylint/test/functional/arguments.rc b/pylint/test/functional/arguments.rc new file mode 100644 index 000000000..bb6bf0e37 --- /dev/null +++ b/pylint/test/functional/arguments.rc @@ -0,0 +1,2 @@ +[TYPECHECK] +signature-mutators=functional.arguments.mutation_decorator |