'"'NoneType' object has no attribute ..." when object checked not to be None?

How is possible that in Python if they first check if some object is not None (if popResult:) they use that object in else branch to get its Attribute messages list? I am not an expert for Python but I am using some already very reliable GitHub script - https://github.com/NavicoOS/ac2git/

def TryPop(self, streamName, transaction):
    logger.debug("**** entered TryPop method *******")
    for i in range(0, 2):
        popResult = accurev.pop(verSpec=streamName, location=self.gitRepo.path,elementList='.')
        if popResult:
            break
        else:
            logger.error("accurev pop failed:")
            for message in popResult.messages:
                if message.error is not None and message.error:
                    logger.error("  {0}".format(message.text))
                else:
                    logger.info("  {0}".format(message.text))
    
    return popResult

With this script I got the following error:

  File "/home/ac2git/ac2git.py", line 965, in TryPop
    for message in popResult.messages:
AttributeError: 'NoneType' object has no attribute 'messages'

Again - as this is one of the basic methods for this project, I really doubt that developers made such a beginner's mistake, because this project has been used in many years for migrating from some other SCM to Git, but on the other hand I really cannot see how they could use object (popResult) to retrieve its attribute if that object is None? Is it possible to put something like this in else branch?

Could you please provide me guidance for this or correction of the code if needed?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source