-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Suppress useless-super-delegation
if return type changed (#5822)
#6141
Conversation
Pull Request Test Coverage Report for Build 2171288819
π - Coveralls |
class ReturnTypeAny: | ||
choices = ['a', 1, (2, 3)] | ||
|
||
def draw(self) -> None: |
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 annotation is incorrect, is that intentional?
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.
Well spotted, that wasn't intentional.
While I was fixing that, I realized there's another case we need to think about:
import typing
from typing import Any
class Base:
def foo(self) -> Any:
pass
class Derived:
def foo(self) -> typing.Any:
return super().foo()
This will be harder to fix, and it looks like the existing rules around type hints on parameters don't cope with this case either, so I suggest this can be handled as a later fix to both parameter type hints and return type hint.
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.
LGTM.
@cdce8p I'd like to get your input here. I'm not sure if this change is reasonable. We're not a type checker, but should we support this? You're probably the best person to judge this. |
I'd say this is a good change. It's a change that will lead to less false positives, and it's something that we can infer, so why not take advantage of it? |
@cdce8p could you check this, if you have some time ? It's blocking the release of 2.13.6 :) |
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.
I'm not fully convinced it is all that useful, but I guess we can add it. Especially since we do something similar for the parameter annotations already.
Just two suggestions:
- Do
not
return if one function doesn't have a return type (similar to parameter annotations). - Move it below the
if called_annotations and overridden_annotations
block.
for more information, see https://pre-commit.ci
I'm not sure what you mean by "Do not return if one function doesn't have a return type (similar to parameter annotations)." Currently my reasoning is that if the derived method adds a type missing from the base method we should not warn |
My idea was to be careful not to block too many errors. I.e. my suggestion would be to only ignore |
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.
Please don't force push changes new here. That will make it difficult / impossible to see and compare old revisions.
if ( | ||
function.returns is not None | ||
and meth_node.returns is None | ||
or meth_node.returns.as_string() != function.returns.as_string() | ||
): |
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.
if ( | |
function.returns is not None | |
and meth_node.returns is None | |
or meth_node.returns.as_string() != function.returns.as_string() | |
): | |
if function.returns is not None and ( | |
meth_node.returns is None | |
or meth_node.returns.as_string() != function.returns.as_string() | |
): |
To fix the tests, you would need to use the old condition.
For my #6141 (comment), I suggested something a little bit different.
if ( | |
function.returns is not None | |
and meth_node.returns is None | |
or meth_node.returns.as_string() != function.returns.as_string() | |
): | |
if ( | |
function.returns is not None | |
and meth_node.returns is not None | |
and meth_node.returns.as_string() != function.returns.as_string() | |
): |
My thinking was that the most likely reason I could think to override a method just to change the return type annotation is that I was extending some library code that doesn't have type declarations and I wanted my library to have type declarations, in which case issuing a diagnostic isn't helpful. I'm not sure I care enough about this to prolong the discussion, though. |
Usually, I think it would be better if the original library added type hints itself. However, I do agree that it's not worth the discussion. If you think it would be helpful, I guess we could do that, too. Will leave that up to you. |
No, I'm done with this change. Merge it or close the PR as you see fit. |
Thank you for the fix @timmartin ! |
β¦6141) * Reinstate warning if base method has no return type annotation
Type of Changes
Description
In the
useless-super-delegation
checker, check whether the return type annotation has been specialized in the derived class. If so, then the override is not useless and we should not issue the warning.Closes #5822