Can this variable really “refer before assignment”,

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

I don’t see how this can happen, but when I comment on line 6:

 with transaction.commit_manually(): 

then the warning disappears.

Am I missing something, or is PyCharm missing something?

Here is a piece of code. I split it into several lines and it still gives a warning. Source code snippet 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
source share
1 answer

This seems to not be a warning, at least I don’t understand why, maybe PyCharm has problems figuring out the size of the with block. But you can turn off this warning by moving the ModelForm assignment to a with block.

0
source

Source: https://habr.com/ru/post/1443817/


All Articles