Search code examples
pythonmypytypinglinter

mypy: Missing positional argument


I am using mypy as linter and a bit confused how default arguments are treated.
I use following type annotation:

def func(dict_1: Dict[str, Any], dict_2: Optional[Dict[str, str]]) 

and real function is signature is:

def func(dict_1, dict_2=None)

in mypy docs I see that if my argument default value is None, then I am allowed to use Optional (see 3rd note at the end of the chapter)
but I get following error:

error: Missing positional argument "dict_2" in call to "func"  [call-arg]

am I allowed by linter to call functions like this func(dict_val) and leave default argument unfilled ?


Solution

  • You are including the type hints in a stub file, which means you should write the below to indicate to mypy that dict_2 has a default argument:

    dict_2: Optional[Dict[str, str]] = ...