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

Discussion around a proper implementation of control-flow in pylint #4795

Open
Pierre-Sassoulas opened this issue Aug 3, 2021 · 1 comment
Labels
Control flow Requires control flow understanding Discussion 🤔 Enhancement ✨ Improvement to a component Needs design proposal 🔒 This is a huge feature, some discussion should happen before a PR is proposed

Comments

@Pierre-Sassoulas
Copy link
Member

Pierre-Sassoulas commented Aug 3, 2021

Current problem

The main limitation regarding control flow is the fact that the variable checkers rely on NamesConsumer's to_consume/consumed states, and these are updated "all-or-nothing" per variable, with additional special-case checks here and there. To give a simpler example, pylint doesn't report any error on:

def function():
    x = 10
    del x
    print(x)

function()

Desired solution

I don't know the history of the Variables checker, but it seems to me like pylint will keep running into issues around control flow as long as this basic approach is used. I wouldn't say that this is a limitation of the AST visitor pattern though. I think improvements could be made by doing any of (in order of increasing code complexity and running time, but also correctness IMO):

  • Modifying NamesConsumer so that variables can be processed more than once.
  • Using astroid's variable lookup functionality, which handles control flow better. [1]
  • Building a control flow graph and then running VariablesChecker checks on the graph rather than the AST itself.

But all of these would be really significant changes so I'm not exactly advocating for them, just thought I'd mention. Personally I'm still very happy using pylint with the current functionality!

[1] Example:

code = """
def function():
    x = 10
    del x
    print(x)
"""
import astroid
ast = astroid.parse(code)
n = [n for n in ast.nodes_of_class(astroid.Name) if n.name == "x"][0]
print(n.lookup('x'))  # Correctly doesn't return any AssignNames

Additional context

This issue is taken entirely from a comment by @david-yz-liu here and will serve as the reference for discussion regarding control-flow implementation.

@jacobtylerwalls
Copy link
Member

Just noting for anyone following the discussion I've proposed baby steps in #5402 (and eventually #5384) following approach 1, above:

Modifying NamesConsumer so that variables can be processed more than once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Control flow Requires control flow understanding Discussion 🤔 Enhancement ✨ Improvement to a component Needs design proposal 🔒 This is a huge feature, some discussion should happen before a PR is proposed
Projects
None yet
Development

No branches or pull requests

2 participants