Relative Content

Tag Archive for pythontype-hinting

Why does “dict[int, int]” is incompatible with “dict[int, int | str]”?

import typing a: dict[int, int] = {} b: dict[int, int | str] = a c: typing.Mapping[int, int | str] = a d: typing.Mapping[int | str, int] = a Pylance reports an error for “b: dict[int, int | str] = a”: Expression of type “dict[int, int]” is incompatible with declared type “dict[int, int | str]” “dict[int, […]

TypeVarTuple Unpack all contained types and use them as type arguments

I would like to use a generic class in Python that is instantiated with a variable number of type parameters. This can be achieved using TypeVarTuple. For each of these type parameters, I want to fill a data structure (e.g., a list). The list can have a different length for each data type. Ideally, I would like to type hint a tuple of lists, each corresponding to a type from the TypeVarTuple.

Python: TypeVarTuple Unpack all contained types and use them as type arguments

I would like to use a generic class in Python that is instantiated with a variable number of type parameters. This can be achieved using TypeVarTuple. For each of these type parameters, I want to fill a data structure (e.g., a list). The list can have a different length for each data type. Ideally, I would like to type hint a tuple of lists, each corresponding to a type from the TypeVarTuple.