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

This entry was posted in Cool Feature. Bookmark the permalink.

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

  1. istlota says:

    I admit to being an incorrigible PyCharm fan. Version 2.7 makes a great IDE even better. I absolutely love the new Darcula appearance. My last formal programming class was three decades ago, years before Python was a twinkle in Guido van Rossum’s Mind-Eye, even before I had written my first line of OOP code. Since then, open source software has been my Guru, with PyCharm being my Sat Guru for all things Python.

    PyCharm’s PEP8 inspection feature, in particular, has become my second most valuable tool [first being the I in I] for leading I from writing working Python code to writing elegant Python code.

    I love the new inspection feature for detecting ‘shadowed’ variables — yet another bad habit of mine PyCharm nudged me to break.

    All of Man’s suffering can be traced to his misconception of What is Real. Math is the language of the One, with it All that is Real may be modeled. As Above, So Below. Programming is a process by which mortals may transition between Math — pure and undefiled — and the Real World. PyCharm is among the best available environments for modeling the All.

  2. Kashif Ali says:

    I noticed that pep8 does show up in the code inspection tab. I had it set to ERROR so it under lines any pep8 errors in red, however once I did a inspection it didn’t show up in the tab but did highlight in the document. Is this how it is suppose to work ?

  3. Bhavani A B says:

    Thanks a lot for the informative post

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>