Search code examples
pythonimportpycharm

PyCharm "unused import statement" message regarding functions called "indirectly"


I've defined a bunch of functions, each of them starting with "ask_...", like ask_name(), ask_comments(), etc. Each function asks the user for input, which is then assigned to corresponding class attributes, see snippet below:

@dataclass
class Person:
    name: str = ""  
    comments: str = ""  
    etc...

def __post_init__(self):
    self.attr_list = [i for i in self.__dict__.keys()]

In order to call the functions in succesion, I've written the following:

for attribute_name in Person.attr_list:
    globals()[''.join(['ask_', attribute_name])]()
    
...

I've now decided to move the "ask" functions to a different module and import them. However, since they're not called explicitly, PyCharm greys out the function names and considers them unused. Can anything be done to remedy the situation? I know it's not breaking the code, but it's a nuisance nonetheless.


Solution

  • You can turn off the warning by unchecking Unresolved references by going to File> Editor> Inspections > Unresolved References and unchecking it, however I'd highly recommend just ignoring the lines being greyed out.