4

I have a piece of code below (belongs to a Django admin view, slightly modified). My IDE (PyCharm) warns me at the last line that Local variable 'ModelForm' might be referenced before assignment.

I don't see how that can ever happen, but when I comment out line 6:

with transaction.commit_manually():

then the warning disappears.

Am I missing something, or is PyCharm missing something?

Here is the chunk of code. I striped it down to just a few lines and it still gives the warning. The original chunk of code is here (70 lines): http://pastebin.com/4UT9hRPb

    ModelForm = self.get_form(request, obj, form=form)

    formsets = []
    if request.method == 'POST':
        # commit only when all forms are valid
        with transaction.commit_manually():
            try:
                objects = queryset.all()
                transaction.commit()
                return self.response_change(request, new_object)

            finally:
                general_error = unicode(sys.exc_info()[1])
                transaction.rollback()

    form = ModelForm()
4

1 回答 1

0

似乎这个警告不应该发生,至少我不明白为什么,也许 PyCharm 在确定with块的范围时有问题。但是您可以通过移动块ModelForm中的分配来消除此警告with

于 2012-11-03T13:22:59.023 回答