Several years into Python, I am trying to get better about writing testable code and following the right coding conventions, and I am confused how to handle this situation. I would appreciate anybody who could lend their insight.

In my code, I have functions which depend on/reference global variables. These variables must be global. I want to write unit tests for these functions, and in order to fully test them, that means I must be able to manipulate those global variables.

Let’s use this example code:

GLOBAL_DICT={'A':1}

def my_func()->None:
     GLOBAL_DICT[A]=GLOBAL_DICT[A]+1

I could, of course, write the function so that I can pass the global variable to it and that by default it will just add in the global variable. This makes it easier to write tests. But then PyCharm will complain that the argument is mutable and that it shadows the name from the outer scope. And if I do this, I need to define what happens if the variable is not passed (provide a default). I could set them to None, but then for doing type hinting I would have to do Union[dict,None] which makes the type hints look much more cluttered, and then pycharm will complain that I didn’t handle a possible input of None in the function’s code, but I shouldn’t need to since I know it will never be None.

GLOBAL_DICT={'A':1}

def my_func(GLOBAL_DICT:dict=GLOBAL_DICT)->None:
     GLOBAL_DICT[A]=GLOBAL_DICT[A]+1

What is the best way to write these functions so they don’t require passing in a bunch of globals every time I call and remain easy to write tests for?