Consider the following code:
def foo(a: dict[str | tuple[str, str], str]) -> None:
pass
def bar(b: dict[str, str]) -> None:
foo(b)
def baz(b: dict[tuple[str, str], str]) -> None:
foo(b)
foo({"foo": "bar"})
foo({("foo", "bar"): "bar"})
When checked with mypy in strict mode it produces the following errors:
file.py:6: error: Argument 1 to "foo" has incompatible type "Dict[str, str]"; expected "Dict[Union[str, Tuple[str, str]], str]"
file.py:9: error: Argument 1 to "foo" has incompatible type "Dict[Tuple[str, str], str]"; expected "Dict[Union[str, Tuple[str, str]], str]"
Which doesn't seem to make sense to me. The parameter is defined to accept a dict with either a string or a tuple as keys and strings as values. However, both variants are not accepted when explicitly annotated as such. They do however work when passing a dict like this directly to the function. It seems to me that mypy expects a dict that has to be able to have both options of the union as keys. I fail to understand why? If the constraints for the key are to be either a string or a tuple of to strings, passing either should be fine. Right? Am I missing something here?