-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Bug1085 #1618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug1085 #1618
Changes from 12 commits
b100a7e
3d7ced8
9eaee35
7a97110
dddde51
4ca8dd6
2345f75
ce58c0c
22f4712
8e30a4e
faddbc5
0e5540c
404edda
24f51c9
08178ea
2cdc01f
1b3e73a
6959f7f
370d5e1
a59b484
429aa87
eded8cd
be08f68
c3c9ce7
eec133b
50208dc
142ad02
8756114
34f8264
7c762b0
a2dbd84
891468f
4cc1191
2fb6602
fb5637c
4109efe
ad1bcdd
7fc54ca
2b34d88
f3a4c4b
07661b3
2a2581c
cd71dc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,6 +133,24 @@ def _positional_parameters(method): | |
| positional = positional[1:] | ||
| return positional | ||
|
|
||
| def _has_different_parameters_default_value(original, overridden): | ||
| """ | ||
| Return True if one of the overridden arguments has a default | ||
| value different from the default value of the original argument | ||
| """ | ||
| orig_name = [param.name for param in original.args] | ||
|
||
| for param_name in orig_name: | ||
| try: | ||
| orig_default = original.default_value(param_name).value | ||
| except astroid.exceptions.NoDefault: | ||
| orig_default = None | ||
| try: | ||
| over_default = overridden.default_value(param_name).value | ||
| if orig_default != over_default: | ||
|
||
| return True | ||
| except astroid.exceptions.NoDefault: | ||
| pass | ||
| return False | ||
|
|
||
| def _has_different_parameters(original, overridden, dummy_parameter_regex): | ||
| zipped = six.moves.zip_longest(original, overridden) | ||
|
|
@@ -197,7 +215,6 @@ def _different_parameters(original, overridden, dummy_parameter_regex): | |
| different_kwonly | ||
| )) | ||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. revert removing this line |
||
| def _is_invalid_base_class(cls): | ||
| return cls.name in INVALID_BASE_CLASSES and is_builtin_object(cls) | ||
|
|
||
|
|
@@ -705,7 +722,8 @@ def _check_useless_super_delegation(self, function): | |
| nothing additional whatsoever than not implementing the method at all. | ||
| If the method uses super() to delegate an operation to the rest of the MRO, | ||
| and if the method called is the same as the current one, the arguments | ||
| passed to super() are the same as the parameters that were passed to | ||
| passed to super(), including their default value, | ||
| are the same as the parameters that were passed to | ||
| this method, then the method could be removed altogether, by letting | ||
| other implementation to take precedence. | ||
| ''' | ||
|
|
@@ -757,6 +775,11 @@ def _check_useless_super_delegation(self, function): | |
| return | ||
| if super_call.type.name != current_scope.name: | ||
| return | ||
|
|
||
| # Detect if the method has argument with default value that is | ||
| # different from the one used in the base method | ||
| if self._check_different_default_values_in_overridden_method(function): | ||
| return | ||
|
|
||
| # Detect if the parameters are the same as the call's arguments. | ||
| params = _signature_from_arguments(function.args) | ||
|
|
@@ -765,6 +788,34 @@ def _check_useless_super_delegation(self, function): | |
| self.add_message('useless-super-delegation', node=function, | ||
| args=(function.name, )) | ||
|
|
||
| @staticmethod | ||
| def _check_different_default_values_in_overridden_method(function): | ||
| """ | ||
| Check if the parameters default values in overridden methods are | ||
| different from those in the base class | ||
| """ | ||
| klass = function.parent.frame() | ||
| for overridden in klass.local_attr_ancestors(function.name): | ||
| # get astroid for the searched method | ||
| try: | ||
| meth_node = overridden[function.name] | ||
| except KeyError: | ||
| # we have found the method but it's not in the local | ||
| # dictionary. | ||
| # This may happen with astroid build from living objects | ||
| continue | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this branch is not taken in tests - it would be cool to have this covered by some automated test, but I guess we do not have to be that restrictive.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact, i just copied/pasted code of the |
||
| if not isinstance(meth_node, astroid.FunctionDef): | ||
| continue | ||
|
||
| instance = klass.instantiate_class() | ||
| method1 = function_to_method(function, instance) | ||
| refmethod = function_to_method(meth_node, instance) | ||
| # Don't care about functions with unknown argument (builtins). | ||
| if method1.args.args is None or refmethod.args.args is None: | ||
| continue | ||
| if _has_different_parameters_default_value(refmethod.args, method1.args): | ||
| return True | ||
| return False | ||
|
|
||
| def _check_slots(self, node): | ||
| if '__slots__' not in node.locals: | ||
| return | ||
|
|
@@ -1229,7 +1280,6 @@ def _check_signature(self, method1, refmethod, class_type, cls): | |
| if (isinstance(decorator, astroid.Attribute) and | ||
| decorator.attrname == 'setter'): | ||
| return | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. revert removing this line |
||
| if _different_parameters( | ||
| refmethod, method1, | ||
| dummy_parameter_regex=self._dummy_rgx): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| useless-super-delegation:98:UselessSuper.equivalent_params:Useless super delegation in method 'equivalent_params' | ||
| useless-super-delegation:101:UselessSuper.equivalent_params_1:Useless super delegation in method 'equivalent_params_1' | ||
| useless-super-delegation:104:UselessSuper.equivalent_params_2:Useless super delegation in method 'equivalent_params_2' | ||
| useless-super-delegation:107:UselessSuper.equivalent_params_3:Useless super delegation in method 'equivalent_params_3' | ||
| useless-super-delegation:110:UselessSuper.equivalent_params_4:Useless super delegation in method 'equivalent_params_4' | ||
| useless-super-delegation:113:UselessSuper.equivalent_params_5:Useless super delegation in method 'equivalent_params_5' | ||
| useless-super-delegation:116:UselessSuper.equivalent_params_6:Useless super delegation in method 'equivalent_params_6' | ||
| useless-super-delegation:119:UselessSuper.__init__:Useless super delegation in method '__init__' | ||
| useless-super-delegation:111:UselessSuper.equivalent_params:Useless super delegation in method 'equivalent_params' | ||
| useless-super-delegation:114:UselessSuper.equivalent_params_1:Useless super delegation in method 'equivalent_params_1' | ||
| useless-super-delegation:117:UselessSuper.equivalent_params_2:Useless super delegation in method 'equivalent_params_2' | ||
| useless-super-delegation:120:UselessSuper.equivalent_params_3:Useless super delegation in method 'equivalent_params_3' | ||
| useless-super-delegation:123:UselessSuper.equivalent_params_4:Useless super delegation in method 'equivalent_params_4' | ||
| useless-super-delegation:126:UselessSuper.equivalent_params_5:Useless super delegation in method 'equivalent_params_5' | ||
| useless-super-delegation:129:UselessSuper.equivalent_params_6:Useless super delegation in method 'equivalent_params_6' | ||
| useless-super-delegation:132:UselessSuper.with_default_argument:Useless super delegation in method 'with_default_argument' | ||
| useless-super-delegation:136:UselessSuper.without_default_argument:Useless super delegation in method 'without_default_argument' | ||
| useless-super-delegation:139:UselessSuper.__init__:Useless super delegation in method '__init__' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will also need a What's new Entry, in the
Other Changessection. Also please add yourself to Contributors file if you are not already there.