In my journey from Java hacker to a Django developer, I’ve test driven a bunch of Python IDEs (ranging from true dev tools like Aptana, Eclipse/PyDev, PyCharm to simple editors like Notepad++).
Recent I was adding some django-profile code I found via Scot Hacker’s very helpful “django-profiles: The Missing Manual“.
When I added this bit of code for a custom form object:
def __init__(self, *args, **kwargs):
super(ProfileForm, self).__init__(*args, **kwargs)
try:
self.fields['email'].initial = self.instance.user.email
except User.DoesNotExist:
pass
Aptana would call out “DoesNotExist” with the error: “Undefined variable from import: DoesNotExist”. Thanks to Google and the DjangoBot I learned that this is because “DoesNotExist” is added by the metaclass.
Here’s the fix
- Open up Aptana Studio (I’m v2.0.3 btw)
- Open Window > Preferences > PyDev > Editor > Code Analysis
- Select the “Undefined” tab
- Add DoesNotExist at the end of the “Consider the following names as globals” list
- Apply and restart
Error gone. In the immortal words of ubernostrum: “Bah. It’s not like metaclasses are *that* hard to statically figure out.” I’ll take your word for that, James.
3 Responses to “Aptana Studio and “Undefined variable from import: DoesNotExist””
So I went and looked up the context for that quote; in this case, the metaclass is:
1. Defining a subclass of an existing exception class.
2. Calling setattr() to make that new subclass an attribute of the model class.
Which, you have to admit, isn’t exactly rocket science — even if it doesn’t work out the type of the attribute, the fact that there’s a setattr() call using the (hard-coded, as a string, in the source) name “DoesNotExist” should be enough to clue in the static analysis.
Or, as I said in the followup on IRC: “I know for a fact that Eclipse has to do more complex analysis on *Java* code than is required to work this one out.”
Thanks! I’ve dealt with this forever and just never taken the time to figure it out! This will be useful!
Keyton
Great, had this problem for a while now. Finally those x’s are gone.