For the following code
"""Test pylint on undefined variable""" import random def main(): """Use undefined variable""" if random.randint(0, 10) == 6: thing = "hi" print(thing) if __name__ == '__main__': main()
PyCharm correctly reports the problem.
pylint (2.0.0, Python 3.6.6) however does not recognize it:
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)
But I would like it to find it, in order to let my CI fail in these cases.
So in fact I have two questions:
pylintto enable so that it can find this kindf of error?
pylintunder the hood.)
Is there an option for pylint to enable so that it can find this kind of error?
Pylint is currently not able to detect possibly-undefined variables in a conditional or control flow block. A future version of Pylint may be able to recognize these kinds of errors. As of the time of your question, there is an open issue to add support for recognizing possible undefined variables inside of control flow blocks like your example.
Pylint does recognize variables that are definitely undefined before they are used, as in this example
print(x) x = "Hello, world"
or this one
print(y) if random.randint(0,10) == 3: y = "ok"
What linting is PyCharm using by default? (I always thought it's pylint under the hood.)
PyCharm uses its own internal inspection library by default. PyCharm is implemented in Java and its inspection library is as well.
It is possible to use Pylint with PyCharm, but it is not built in or used by default. A solution for configuring Pylint as an external tool is shown here, and there is also a Pylint plugin for PyCharm available.