Skip to content
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

Add future=True to all NodeNG.statement() calls #5310

Merged
merged 7 commits into from
Dec 16, 2021

Conversation

DanielNoord
Copy link
Collaborator

  • Add yourself to CONTRIBUTORS if you are a new contributor.
  • Write a good description on what the PR does.

Type of Changes

Type
βœ“ πŸ”¨ Refactoring

Description

Ref pylint-dev/astroid#1235

@DanielNoord DanielNoord added the Maintenance Discussion or action around maintaining pylint or the dev workflow label Nov 15, 2021
@coveralls
Copy link

coveralls commented Nov 15, 2021

Pull Request Test Coverage Report for Build 1586518271

  • 18 of 18 (100.0%) changed or added relevant lines in 6 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.004%) to 93.646%

Files with Coverage Reduction New Missed Lines %
pylint/checkers/typecheck.py 1 94.88%
Totals Coverage Status
Change from base Build 1583088214: -0.004%
Covered Lines: 14236
Relevant Lines: 15202

πŸ’› - Coveralls

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.13.0 milestone Nov 15, 2021
@DanielNoord DanielNoord modified the milestones: 2.13.0, 2.12.2 Nov 16, 2021
@DanielNoord
Copy link
Collaborator Author

Opening and closing to run primer.

@Pierre-Sassoulas I put this in 2.12.2 but since we don't actually change any code here (only change AttributeError to StatementMissing + one new and node_scope.parent) this could also go in 2.12 without much risk of breaking things.
It would stop the warnings from displaying in our tests.

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ‘

@Pierre-Sassoulas
Copy link
Member

primers bringing value again πŸ˜„

@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.12.0, 2.13.0 Nov 24, 2021
@Pierre-Sassoulas
Copy link
Member

Moving to 2.13 because of the test fail (could be 2.12.x too).

@DanielNoord
Copy link
Collaborator Author

Looking into crash as we speak!

@DanielNoord
Copy link
Collaborator Author

Blocked by #5382.

@DanielNoord DanielNoord added the Blocked 🚧 Blocked by a particular issue label Nov 24, 2021
@Pierre-Sassoulas Pierre-Sassoulas added Blocked 🚧 Blocked by a particular issue and removed Blocked 🚧 Blocked by a particular issue labels Dec 3, 2021
pylint/checkers/format.py Outdated Show resolved Hide resolved
]:
missingattr.add((owner, name))
continue
except exceptions.StatementMissing:
continue
except AttributeError:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also catching another AttributeError besides the missing parent attribute that is now captured in exceptions.StatementMissing. Thanks to the primer for finding this potential crash before committing to main πŸ˜„

@@ -1890,29 +1889,36 @@ def _loopvar_name(self, node: astroid.Name) -> None:
# the variable is not defined.
scope = node.scope()
if isinstance(scope, nodes.FunctionDef) and any(
asmt.statement().parent_of(scope) for asmt in astmts
asmt.scope().parent_of(scope) for asmt in astmts
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to check if the scope of asmt is the parent scope of node's scope. Previously this relied on nodes.Module.statement() returning itself. Using scope seems to be more sensical here.

astmts[0].is_statement
or not isinstance(astmts[0].parent, nodes.Module)
and astmts[0].statement(future=True).parent_of(node)
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was particularly trigger to get right, but this is testing what we want. If parent is the root Module and the parent of node we don't do _astmts = astmts[:1]. The other or is simply what we did previously.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh I think you know more about this particular method than I do. It looks like it could work πŸ˜…



else:
ellipsis = type(Ellipsis)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a regression test for code I found while working on this.

@@ -602,8 +602,10 @@ def visit_default(self, node: nodes.NodeNG) -> None:
isinstance(node.parent, nodes.TryFinally) and node in node.parent.finalbody
):
prev_line = node.parent.body[0].tolineno + 1
elif isinstance(node.parent, nodes.Module):
prev_line = 0
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same effect as doing node.parent.fromlineno but avoids the StatementMissing exception from calling statement.

@DanielNoord DanielNoord removed the Blocked 🚧 Blocked by a particular issue label Dec 13, 2021
@DanielNoord
Copy link
Collaborator Author

@cdce8p This is finally ready for review πŸŽ‰

@DanielNoord DanielNoord requested a review from cdce8p December 13, 2021 22:26
@DanielNoord DanielNoord marked this pull request as ready for review December 13, 2021 22:27
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just some small suggestions.

pylint/checkers/variables.py Show resolved Hide resolved
@DanielNoord DanielNoord added the Needs review πŸ” Needs to be reviewed by one or multiple more persons label Dec 15, 2021
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I'm going to wait for @cdce8p review to merge.

@@ -72,7 +72,7 @@
from typing import Any, Callable, Iterator, List, Optional, Pattern, Tuple

import astroid
from astroid import bases, nodes
from astroid import bases, exceptions, nodes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should use this import here.
There is a pylint.exceptions package too which is often imported with

from pylint import exceptions

Thus doing the same for astroid will lead to name conflicts.

At some point @Pierre-Sassoulas and I discussed this I think, although we didn't reach a conclusion for a good name. For the time being, I would recommend this

import astroid.exceptions

try:
    ...
except astroid.exceptions...:
    ...

It's a bit lengthy but already used elsewhere. Should be good enough util we find something else.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added an import astroid.exceptions line. There is already a import astroid statement, but astroid.exceptions.StatementMissing could not be resolved by VS Code (/ Pylance).. I think because of the wildcard import in __init__.py in astroid.

Comment on lines 1036 to 1037
except exceptions.StatementMissing:
continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the discussion about not adding the FrameMissing error in astroid, I've been thinking if we should deprecate / remove the StatementMissing error, too.

Is there a benefit here with it being more specific? I would probably recommend to just catch ParentMissingError here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if there is any benefit, perhaps when doing something like [(i.frame(), j.statement()) for i,j in zip(nodes, other_nodes)] but that is really really convoluted...
On the other hand, we have already gone to the effort of adding it and I don't think it adds too much boilerplate to the astroid codebase. We never know when we might need it in the future so I'd say let's just keep it in?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's keep it. Only because it's already in astroid πŸ˜‰

astmts[0].is_statement
or not isinstance(astmts[0].parent, nodes.Module)
and astmts[0].statement(future=True).parent_of(node)
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh I think you know more about this particular method than I do. It looks like it could work πŸ˜…

@cdce8p cdce8p removed the Needs review πŸ” Needs to be reviewed by one or multiple more persons label Dec 15, 2021
@DanielNoord DanielNoord merged commit c285fae into pylint-dev:main Dec 16, 2021
@DanielNoord DanielNoord deleted the statement-future branch December 31, 2021 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Maintenance Discussion or action around maintaining pylint or the dev workflow
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants