diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2016-07-10 14:48:46 +0100 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2016-07-10 14:54:14 +0100 |
commit | d6cf1f0581c20f958dfad88ca6ee379f611be69e (patch) | |
tree | 51be85559528e86146496de15e3c06c34c7318e3 /pylint/checkers/classes.py | |
parent | 4d82484d610c3db3257c1cd133d92e9abf1d9b8b (diff) | |
download | pylint-git-d6cf1f0581c20f958dfad88ca6ee379f611be69e.tar.gz |
arguments-differ takes in consideration kwonlyargs and variadics
The check was rewritten to be more strict about overridding a method:
- now it complains if the parent method had variadics in the signature,
but the overridden method does not have. For instance, having *args in
the parent method means that it can accept anything, but restricting this
to (a, b) in the overridden method leads to inconsistencies in their
behavior and can potentially introduce new bugs.
- it complains if the number of keyword only parameters are not the
same in the overridden method
- it complains about the names of the parameters. For instance, having
(a, b) in the parent method and changing this to (c, d) in the overridden
method means that the latter one cannot be accessed in the same way
as the parent method, when using keyword arguments.
Close #983
Diffstat (limited to 'pylint/checkers/classes.py')
-rw-r--r-- | pylint/checkers/classes.py | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/pylint/checkers/classes.py b/pylint/checkers/classes.py index 847e3ea58..c0b827bb8 100644 --- a/pylint/checkers/classes.py +++ b/pylint/checkers/classes.py @@ -60,6 +60,10 @@ def _different_parameters(original, overridden): * they have different positional parameters, including different names + * one of the methods is having variadics, while the other is not + + * they have different keyword only parameters. + """ original_parameters = _positional_parameters(original) @@ -67,12 +71,18 @@ def _different_parameters(original, overridden): different_positional = _has_different_parameters(original_parameters, overridden_parameters) - - if different_positional: - # If the second method is using positional variadic arguments, then it is okay - # to not complain about it. - different_positional = overridden.args.vararg is None - return different_positional + different_kwonly = _has_different_parameters(original.args.kwonlyargs, + overridden.args.kwonlyargs) + + # Both or none should have extra variadics, otherwise the method + # loses or gains capabilities that are not reflected into the parent method, + # leading to potential inconsistencies in the code. + different_kwarg = sum(1 for param in (original.args.kwarg, overridden.args.kwarg) + if not param) == 1 + different_vararg = sum(1 for param in (original.args.vararg, overridden.args.vararg) + if not param) == 1 + + return different_positional or different_kwarg or different_vararg or different_kwonly def _is_invalid_base_class(cls): |