I wrote a python code to give a value to a variable if it is not defined, but nevertheless PyCharm warns me that the variable can be not defined:
if 'TEST' not in globals():
TEST = "test"
print(TEST)
Name 'TEST' can be not defined
Is there an other way to define undefined variables such that PyCharm understand it?
else: TEST = globals().get('TEST')
I get rid of the warning. I think the problem comes more from the fact that we access to the variable via a string, which I find ugly but apparently there is no way to do otherwise.else
block. PyCharm can't understand the logicif 'TEST' not in globals()
isFalse
then it is already definedx = 1 ; if x == 1: foo = 2 ; print(foo)
Here we also get the warning thatfoo
may not be defined even though we hardcoded the condition which definesfoo
to always beTrue
. Pycharm follows all possible branches according to the syntax, not logically.