WARNING: This blog entry was imported from my old blog on blogs.sun.com (which used different blogging software), so formatting and links may not be correct.
In my last entry I showed that the Fix Imports action will alert you to an unresolved symbol. This is really helpful since it can help you track down runtime problems.
Obviously, you shouldn't have to go run Fix Imports on every file, periodically, to look for unresolved symbols!
NetBeans has a special semantic checker for this. It is off by default, for two reasons:
- I added it just a couple of days ago - during the stabilization period for EA, so I didn't dare enable it in case it's seriously broken.
- There are cases where it doesn't yet work. I'll describe that later.
To enable it, open up the Editor options dialog, and under Hints, select Python. You should now see the current batch of Python hints. Enable the "Unresolved Symbol" hint:
Now let's open up the datetime.py
file again:
As you can see, the file is marked erroneous (by the red square above the scrollbar), and the scrollbar indicates where in the file the error is. Clicking on it to expose the error location you can see an underline under the unresolved symbol, and a tooltip explaining what the problem is. The error is also shown in the tasklist, so you don't have to open each file to look for problems - just let the tasklist scan your project for problems.
Here's another example. I opened up the Django 1.0 sources, and NetBeans pointed out this problem to me, in django.utils.simplejson.tool
:
This shows that the import is unused, and that the attempts to call methods on the import do not work. The call would need to be changed to
obj = django.utils.simplejson.load(infile)
or the import changed to be
import django.utils.simplejson as simplejson
Here's one final one... Perhaps helpful to people having done a lot of Java development recently:
Finding unresolved symbols means that NetBeans must be able to find all the libraries you are importing. This means you can't just point NetBeans at a plain Python file - your project has to contain all the libraries you are using, and, you have to configure your source path correctly such that NetBeans computes the correct package names for your python modules. (I accidentally pointed to the "django" folder instead of its parent folder at first, which meant django.
wasn't part of the package prefix and that meant that all kinds of symbols couldn't be resolved.)
Second, this won't work for dynamically generated symbols (eval etc). I haven't run into this a lot yet - but I'm sure there are areas where the unresolved detection can't find symbols that really do exist.
P.S. Is there a canonical Python term for what I've called "unresolved" here that we should use instead?
No comments:
Post a Comment