警告 - 赋值前引用的变量

Warning - variable referenced before assignment

具有以下代码段:

if use_cache and (cache_data := cache.get(cache_name)) is not None:
       logger.info("Returning objects from Cache")
       return cache_data

我收到 Pycharm 警告

Local variable cache_data might be used before assignment.

能否请您解释一下在这种情况下这怎么可能发生,或者这是一个检查错误?

不幸的是,这似乎是一个known issue in PyCharm that was reported back in Nov 2019. It seems the inspection code added to handle assignment expressions无法识别此条件中的代码只能在设置局部变量后才能到达。

报告指出,它也出现在理解条件中,目前似乎没有任何可用的(或计划中的)修复。