Search code examples
pythonpycharmpython-typing

What is the difference between dict[[int], int] and dict[int, int]?


I have the following code:

from typing import *

Args = ParamSpec('Args')
ReturnType = TypeVar('ReturnType')
TestSet = Dict[Args, ReturnType]
ComplexTestSet = Dict[Args, Tuple[bool, Exception | None, ReturnType | None]]

# Some stuff

def test_func_simple(func: Callable[[Args], ReturnType], test_cases: TestSet) -> ComplexTestSet:
    pass # Some stuff

# Some stuff

if __name__ == "__main__":
    print(test_func_simple(f, {
        1: 1,
        2: 4,
        3: 9
    }))

The code runs fine and produces the correct output, but PyCharm is highlighting the dictionary of test cases yellow with the message "Expected type 'dict[[int], int]' (matched generic type 'dict[ParamSpec("Args"), ReturnType]'), got 'dict[int, int]' instead". I can't seem to figure out why it's expecting a dict[[int], int] instead of a dict[int, int] or even what a dict[[int], int] would actually match with. Nothing like it seems to appear in the documentation and nothing I tried matched it. Could someone explain why it's coming up with dict[[int], int] and what exactly that means?


Solution

  • As pointed out by user2357112 supports Monica, this is due to incorrect use of ParamSpec. The correct code is to replace ParamSpec with TypeVar.