PythonHints

Contents


author : Jean-Yves Mengant

Python Hints And Quick Fixes

As many other languages in Netbeans, the Python editor supports pluggable quickfixes and hints. Accessing the Python hints configuration is straightforward from the Netbeans MainMenu :

select Tools/Options/Editor select Python as the language

The Python available hints will be displayed as follow :
http://wiki.netbeans.org/wiki/attach/PythonHints/optionshints_PythonHints.png

Hints in dynamic languages

In dynamic languages like Python, the role of hints feature is more important than in compiled languages(like C++ or java) : since you do not have any visible compiler step to check your errors, you need a complementary tool to check your code : hints feature comes to the rescue dynamically pinpointing errors , warnings or informations while you're typing. In this ways hint tools inside an IDE like Netbeans may be compared to kind of dynamic 'lint' features made available from inside the editor

The list of available hints is far from being frozen and new hints will be added in the near future to provide more and more dynamic coding facilities ; the only limit for hints implementors is do reasonable syntax/semantic controls to keep the Python editor responsive.

Please contribute your ideas there or via the issue tracker for new Hints.

Standard Hints

Symbol defined in
'''all'''
does not exist

the
'''all'''
python variable is used inside a module to defined public symbols exposed by the module ; this implies that all names located inside the {all} list must be existing symbols. Here's the editor showing a snippet which has a code fragment containing a
'''all'''
builtin containing undefined names

http://wiki.netbeans.org/wiki/attach/PythonHints/undefinedall_PythonHints.png

Assign expression to a variable

This warning is issued as soon as you have an orphan expression on a python line source which would be better assign to a local or global variable ; for instance in the screen shot below not assigning the expression 2+3 on line 7 to a variable does not make any sense at all

http://wiki.netbeans.org/wiki/attach/PythonHints/assignexpression_PythonHints.png

Find unused imports

A warning is issued by this hint when an imported python module is no used in a given python source

Fixes:

  • removes the unused import
  • removes all unused imports

http://wiki.netbeans.org/wiki/attach/PythonHints/unusedimport_PythonHints.png


Multiple imports per import statement is discouraged

Cleanly separating python imports using one import statement per import is a recommended coding style , this hint issues a warning and proposes to reformat the imports as a fix.

http://wiki.netbeans.org/wiki/attach/PythonHints/multipleimportsperline_PythonHints.png

Access protected attributes

By convention attributes starting with a single
_
are assumed to be protected in Python.

This hint issues a warning when protected class attributes are acceded by non child classes.


http://wiki.netbeans.org/wiki/attach/PythonHints/accesstoprotected_PythonHints.png


Parent Child circular redundancy

This hint shows an error (you can switch it to a warning by decreasing this hint severity option) as soon as a circular redundancy is detected in parent / child relationships of python classes.

NB : the case may be more intricated than the simple one showed by the code fragment below

http://wiki.netbeans.org/wiki/attach/PythonHints/circularredundancy_PythonHints.png

Document comment

When defining a function , a class or a method activating this hint provides the opportunity to create associated documentations using different fixes options :

Fixes:

  • add a one liner docstring
  • add a multi liner docstring
  • preview the chosen option

http://wiki.netbeans.org/wiki/attach/PythonHints/createdocument_PythonHints.png


Naming convention

The naming convention hint is there to facilitate the python coding rules as defined by PEP-008 Complementary options for this hint may be customized from the hints option panel.

As soon a naming convention rule is violated inside a python source a warning is issued as shown below and replacement alternatives are provided accordingly


http://wiki.netbeans.org/wiki/attach/PythonHints/namingconvention_PythonHints.png

Relative imports for intra packages imports are actively discouraged

Relative imports are imports whose imported package start with at least one '.' are defined as relative to the package containing the import statement (example : from .foo import name ) Most of the time relative imports are ambiguous an decrease the readability of your code and it's better to avoid them.

This hints will warn when such imports are encountered


http://wiki.netbeans.org/wiki/attach/PythonHints/relativeimports_PythonHints.png

Deprecated

When checked the deprecated hints warns as soon as an obsolete (deprecated) python package is used inside a python module

The following code sample shows the problem:

http://wiki.netbeans.org/wiki/attach/PythonHints/deprecated_PythonHints.png

Attribute defined outside
'''init'''_

clean definitions of class attributes are achieved by initing then in the init class constructor This hints tracks for class attributes which are not inited in the init method.


http://wiki.netbeans.org/wiki/attach/PythonHints/initattribute_PythonHints.png

Find unused variables

This hint is very useful to cleanup your code by detecting the variables that have been assigned a value and which are not been used again , or defined function arguments which are never used inside the function unused function or method argument are just underlined by the editor.

http://wiki.netbeans.org/wiki/attach/PythonHints/unusedvariable_PythonHints.png

Find unresolved class attributes and parentages

This hint locates class attributes which are used in expression without being previously initted and also the unresolved parent classes in children declarations
http://wiki.netbeans.org/wiki/attach/PythonHints/unresolvedparent_PythonHints.png
http://wiki.netbeans.org/wiki/attach/PythonHints/undefinedattribute_PythonHints.png

Find undefined names

This hint locates variables which have been used but have never been initted to a value
http://wiki.netbeans.org/wiki/attach/PythonHints/undefinedname_PythonHints.png
Tor has provided a more detailed example about unused variables on his blog here


Selection hints

Extract Code

The Extract Code hint applies to text selection. If you select a set of lines, you will get a lightbulb which offers to extract the code into a separate method. In other words, this is an Extract Method refactoring. In addition to the option of applying the extract method operation, you have the option of previewing the refactoring which will bring up a diff view showing the extracted method and the call site modification.

Once you run the extract method refactoring, you are placed in instant rename refactoring mode where you can enter the new name of the method and press return to commit.


Surround With

The Surround With refactoring is a text selection hint, like Extract Code above. As soon as you've selected a set of lines, a lightbulb is shown which when clicked on (or when you press Alt Enter) allows you to choose between several surround-with options. You can surround the set of lines with try/except, try/finally, and so on. There is also an option to preview the operation.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo