I'm attempting a fairly typical if/else
-> CONSTANT = ...
pattern in python but getting a pyright error that "... is constant (because it is uppercase) and cannot be redefined" (reportConstantRedefinition). While this is not a syntax error, I'm trying to understand how to avoid the pyright warning and -- more importantly -- what the proper pattern is.
My constant is defined based on an if statement. E.g.:
if os.getenv('DEBUG'):
API_HOST = 'http://STAGING/'
else:
API_HOST = 'http://PROD/'
It's on the last line that pyright gives the error ""API_HOST" is constant (because it is uppercase) and cannot be redefined".
I've always thought that was a valid pattern. Is there a better way to do that which isn't just a way to outwit pyright? Why does pyright think I should do?
E.g., APIHOST = 'x' if os.getenv('DEBUG') else 'y'
would work but is harder to read and not scalable.
(I also tried to predefine the variable (API_HOST: str
) so that it's clear that it's not unbound (which it can't be) but that just gives me a "redefine" error on both lines.)