Code inspection in a PyCharm project

In this section, we will be considering a number of specific instances where PyCharm's code analyzer points out common errors and warnings in Python programs. We will then address these problems accordingly.

To follow this example, import the Chapter04/Inspection project into your workspace and open the main.py file, or copy and paste the following code into a PyCharm project:

def is_even(x):
if x % 2 == 0:
return True
else:
return False

print('Function finished.')


def foo(bar):
return 0


def main():
print(math.sqrt(4))

if __name__ == '__main__':
main()

The first thing to note here is that there are several errors and warnings that we need to address in this file, as indicated in the top-right corner and along the scroll bar of the editor window. This is indicated by a green box in the following screenshot:

Code inspection in PyCharm

For each error or warning, you can hover your cursor over the code that is associated with that problem to see more details regarding the issue. For example, if you hover over the math.sqrt() function call in the main() function, a popup will appear saying Unresolved reference 'math' to indicate that we need to import the math module to be able to utilize its sqrt() function.

On the topic of hovering, you can also hover over indications of problems in the scroll bar to display the same information. This feature is especially useful in large files and projects.

Now, let's try our hand at resolving each of these problems. The goal, again, is to obtain a green checkmark in the top-right corner of our editor window (which is currently a red exclamation mark).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.213.209