Features Tutorials

Long-awaited PEP 8 checks on the fly, improved doctest support and more in PyCharm 2.7

PyCharm integration with PEP 8

It is not a secret that readability is one of the key features of Python. Indeed, a high level of readability is at the heart of Python’s design, following the recognized fact that code is read much more often than it is written. Developers always need to share their code, so it is very important for that code to be easily understood by others. And here come different coding style guides and standards. One of them, PEP 8, is the de facto coding style guide for Python.

PyCharm 2.7 takes care of readability and general beauty of your code. Now it supports continuous checking of your code for PEP 8 compliance on the fly, as you type it in the editor. PyCharm highlights any coding style violations and gives you suggestions, which you can apply instantly with the well known PyCharm quick-fixes:

Following PEP 8 is a good practice, although there are some situations where it is desirable to depart from the standard. The PyCharm team respects your freedom of choice, so you can disable specific PEP 8  warnings if you really need to. You can do that with the following quick-fix:

 

Or you can completely manage your inspections profile with Settings | Project Settings | Inspections:

Automatic imports optimization in PyCharm

Whenever you remove usages, it’s easy to forget to remove the corresponding imports. Now, in addition to standard PEP 8 checks, PyCharm is capable of optimizing imports. You no longer have to stop what you are doing, scroll to the top of the file, find the unused imports, and remove them manually.

This feature formats the existing import statements, reordering them according to PEP 8, and removes unused ones as well. All you need to do is press Ctrl+Alt+O or go to Code|Optimize imports…

 

 

 


PyCharm recognizes Python code in your doctests

There is a nice standard library module called doctests which lets you document your code and write tests for it simultaneously. Now PyCharm recognizes Python code in your doctests, and provides syntax highlighting and even code insight for it:


It correctly recognizes code in doctests even though it has different context. For example, when performing Optimize imports, PyCharm takes care not to remove imports in doctests in case they are required.

New general editing features in PyCharm

PyCharm 2.7 also includes two minor but neat features:

  • Edit | Fill Paragraph allows you to reflow the current string literal, comment or paragraph in a plain text file, so that lines wrap around and don’t exceed the right margin
  • Shadowing built-ins is a new inspection that highlights definitions of names that match the names of Python built-in functions:

Unwrap/Remove editing action in PyCharm

Now PyCharm supports the Unwrap/Remove action for Python, which provides a quick way to remove a control statement surrounding a block of code. To unwrap or remove a statement, place the caret on the expression and press Ctrl+Shift+Delete or go to Code | Unwrap/Remove.

PyCharm shows a pop-up window with all actions that are available in the current context:

 

We’ve also introduced several refactorings and code insight features that you may find interesting. See the What’s new page or these blog posts for more detailed descriptions: Refactoring enhancements in PyCharm 2.7 and Dynamic runtime type inference in PyCharm 2.7.

Develop with Pleasure!
PyCharm team

image description