Search code examples
pythonpycharmglobal-variables

PyCharm no AutoCompleting / TypeHints global variable


I want to edit a variable that is global but PyCharm does not give me any hints about what operation I can do.

Here is some example code I've written:

def run_game_loop():
    global g
    for i in range(0, g.rounds_total):
        g.new_round() # no auto-complete

I do not know what to do? Did I config my PyCharm wrong or am I not supposed to use global like that?


Solution

  • It adds some bulk, but you can add a type hinted line:

    def run_game_loop():
        global g
        g: YOUR_TYPE
    
        for i in range(0, g.rounds_total):
            g.new_round()
    

    Where YOUR_TYPE is the type of g. It should now know what the type is, and should be able to make better suggestions.