1

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 ?

Beliaev Maksim
  • 968
  • 12
  • 21
  • 1
    An argument with a default (so optional from the perspective of the caller) is different than an argument with type `Optional`. `dict_2: Optional[Dict[str, str]] = None` should make `mypy`'s check pass. – Mario Ishac Oct 19 '21 at 00:24
  • as you see, I am using that, I just have typing in stub file. and that does not work – Beliaev Maksim Oct 19 '21 at 13:29
  • 1
    You are using `dict_2: Optional[Dict[str, str]]` without the ` = None` that comes after it (or ` = ...` if in a stub file). – Mario Ishac Oct 19 '21 at 19:46

1 Answers1

1

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]] = ...
Mario Ishac
  • 5,060
  • 3
  • 21
  • 52