I have a module which I called entities.py - there are 2 classes within it and 2 global variables as in below pattern:
FIRST_VAR = ...
SECOND_VAR = ...
class FirstClass:
[...]
class SecondClass:
[...]
I also have another module (let's call it main.py for now) where I import both classes and constants as like here:
from entities import FirstClass, SecondClass, FIRST_VAR, SECOND_VAR
In the same "main.py" module I have another constant: THIRD_VAR = ..., and another class, in which all of imported names are being used.
Now, I have a function, which is being called only if a certain condition is met (passing config file path as CLI argument in my case). As my best bet, I've written it as following:
def update_consts_from_config(config: ConfigParser):
global FIRST_VAR
global SECOND_VAR
global THIRD_VAR
FIRST_VAR = ...
SECOND_VAR = ...
THIRD_VAR = ...
This works perfectly fine, although PyCharm indicates two issues, which at least I don't consider accurate.
from entities import FirstClass, SecondClass, FIRST_VAR, SECOND_VAR - here it warns me that FIRST_VAR and SECOND_VAR are unused imports, but from my understanding and testing they are used and not re-declared elsewhere unless function update_consts_from_config is invoked.
Also, under update_consts_from_config function:
global FIRST_VAR - at this and next line, it says
Global variable FIRST_VAR is undefined at the module level
My question is, should I really care about those warnings and (as I think the code is correct and clear), or am I missing something important and should come up with something different here?
I know I can do something as:
import entities
from entities import FirstClass, SecondClass
FIRST_VAR = entities.FIRST_VAR
SECOND_VAR = entities.SECOND_VAR
and work from there, but this look like an overkill for me, entities module has only what I have to import in main.py which also strictly depends on it, therefore I would rather stick to importing those names explicitly than referencing them by entities. just for that reason
What do you think would be a best practice here? I would like my code to clear, unambiguous and somehow optimal.