Add typing to NodeNG.statement#1217
Conversation
|
We're actually catching the This does seem like something we want to fix, or at least document better. Using the However, how do we go about fixing a bug simultaneously in |
Pierre-Sassoulas
left a comment
There was a problem hiding this comment.
There is a really strange fail in the pre-commit continuous integration. Maybe we need to not use the current astroid with pylint's in pre-commit ?
|
@Pierre-Sassoulas No the
We will thus need to change However, @cdce8p also mentioned that he wondered if it is correct for |
cdce8p
left a comment
There was a problem hiding this comment.
Technically, you are correct that statement can return nodes.Statement or nodes.Module. I was just wondering if it's of any use for us to return Module or if we should raise an exception instead. Module itself isn't a Statement so from that perspective I would imaging it's special cased in pylint already.
In that sense, a general StatementMissing, or similar, error might make sense. This could also be raised if a node doesn't have a parent (instead of ParentMissingError).
Wouldn't using a |
Not really sure we need more information. It works just fine with the |
I do think we should at least show that this is expected behaviour of the method. The fact that we are actually handling
I'm not sure if I agree with I think raising more specific errors in the long run will be better as it makes the code more maintainable/readable. Not only can we give |
I agree with that. Just not sure At the moment, I just find |
Do you then want to raise
|
I would recommend to raise |
for more information, see https://pre-commit.ci
|
2 questions:
|
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
cdce8p
left a comment
There was a problem hiding this comment.
I've seen that pytest will emit DeprecationWarnings for astroid itself. Could you add future=True to existing statement in astroid, too?
https://github.com/PyCQA/astroid/runs/4126645345?check_suite_focus=true#step:6:81
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
This started to fail some of the tests. I wanted to do this in a separate PR because I wondered if some of the functions/methods calling |
cdce8p
left a comment
There was a problem hiding this comment.
Overall, LGTM! Thanks @DanielNoord 🚀
A bit unfortunate, that we came across multiple false-positives in the process.
@Pierre-Sassoulas Do you want to take a final look at it? Maybe with focus on the Exception and deprecation message?
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
|
Thanks for all of your help here @cdce8p! Indeed a bit unfortunate, but I learned a lot from this PR and it should help with some of the future PR's and issues we might come across. |
Pierre-Sassoulas
left a comment
There was a problem hiding this comment.
I only have a small remark regarding the "bad super call" but I don't see anything wrong with the MR otherwise. To be fair I did not find the time to read the whole conversation and this seem really polished :) Look like you took care of a lot of problems, thanks a lot for handling this !
|
|
||
| def __init__(self, target: "nodes.NodeNG") -> None: | ||
| # pylint: disable-next=bad-super-call | ||
| # https://github.com/PyCQA/pylint/issues/2903 |
There was a problem hiding this comment.
I think the spirit of the check is to signal the problem to people that don't really know how to use super(). Here it's a false positive because calling the father's function is what you really want to do, but maybe in most case and especially for beginner the check is right. If we remove this "false positive" the check will only have really obvious cases where the class called is not even an ancestor ?
There was a problem hiding this comment.
🤔 Not sure about that. What it comes done to is if we want to emit errors for valid Python code. Even if it's probably not what was intended, there are still situations where it's useful and has it's place, especially with multi-inheritance.
The check also emits errors in cases that could cause recursion loops:
class SuperWithType(object):
"""type(self) may lead to recursion loop in derived classes"""
def __init__(self):
super(type(self), self).__init__() # [bad-super-call]
class SuperWithSelfClass(object):
"""self.__class__ may lead to recursion loop in derived classes"""
def __init__(self):
super(self.__class__, self).__init__() # [bad-super-call]Any modifications should also make sure to still emit errors if the class is not part of the MRO.
class Getattr(object):
""" crash """
name = NewAaaa
class CrashSuper(object):
""" test a crash with this checker """
def __init__(self):
super(Getattr.name, self).__init__() # [bad-super-call]Test cases taken from the pylint testsuit: super_checks.py
There was a problem hiding this comment.
OK, I was trying to search for the initial issue where this was implemented for the rational but I can't find it. It makes sense to not warn for valid code, and the check has a lot of valid test cases, so let's fix this false positive.
astroid/nodes/scoped_nodes.py
Outdated
| # pylint: disable-next=signature-differs | ||
| # https://github.com/PyCQA/pylint/issues/5264 |
There was a problem hiding this comment.
| # pylint: disable-next=signature-differs | |
| # https://github.com/PyCQA/pylint/issues/5264 |
It seems like our luck didn't outran us yet. This actual now triggers a useless-suppression message. Guess we found a false-negative by adding * 😅 pylint-dev/pylint#5270
There was a problem hiding this comment.
Is useless-suppression not enabled for astroid? CI is passing.
We might want to enable that in another PR.
There was a problem hiding this comment.
It's enabled, but not a failure condition! I don't think we should change that.
There was a problem hiding this comment.
Sure? Seems like something that will be easily missed in review
There was a problem hiding this comment.
I also think we should make it a failure condition so it's handled in the proper MR each time.
There was a problem hiding this comment.
We might want to enable that in another PR.
It would be as easy as adding --fail-on=useless-suppression to the pylint command.
The issue is that some errors might be Python version specific. It could create a situation where only one, pre-commit or CI, will succeed when tested with different versions. The py-version option should help, but I don't know if it's enough.
Additionally, this would prevent us from having the latest pylint version installed in a local env as this is likely to have fixed some false-positives. -> Which would then trigger useless-suppression.
There was a problem hiding this comment.
That last point will indeed be difficult to avoid. I'll try and think of something to fix that, but in the meantime let's let this rest!
There was a problem hiding this comment.
Do you maybe want to track this as issue in pylint?
in the meantime let's let this rest!
I completely agree!
There was a problem hiding this comment.
Shouldn't this be tracked in astroid? pylint has its own issues with useless-suppression (pylint-dev/pylint#5040) 😄
There was a problem hiding this comment.
😂 Though if we enable it, we'll likely want to do it for pylint too. Tbh I don't really care about it due to the issues mentioned earlier.
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Steps
Description
statement()is only defined twice. Once onNodeNGand once onModule. The one onNodeNGrefers toself.is_statementwhich is only set toTrueonStatement. As a resultstatement()can either returnModuleorStatement.Added because of comment by @cdce8p here:
pylint-dev/pylint#5163 (review)
I think I will not wait for this version to be added to
pylintto update the typing there.Type of Changes
Related Issue