How to use python to type hint a function with same parameter types but different return types?
I have a function, it’s return type is tuple[bool, set[int] | str]
. If the 0th item is True
, then the 1st item is the result set[int]
, else the 1st item is a str showing the reason why it failed. It’s like this
Type hints for subset of class
I’m writing functions that act on pandas.Series
with a DateTimeIndex
. I can do type hints, like so:
Override return type hint of function from Python dependency
I am using a dependency that has type hints but doesn’t test them and some of them are just broken. For example:
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.
Specifying only one of a group of parameters is required in a TypedDict
Below is an example of a TypedDict:
How to typehint a collections namedtuple as a function return?
I genuinely cannot find an answer to this and related questions on here (such as 1, 2) are for NamedTuple
from the typing
module.
Use decorator for repeated overloading
The functions have in common, that the type of the first argument determines the type of the return value.
How to type hint `**kwargs` inside a dataclass?
I have a class like this with field members consisting of different types, and inside my class I have __new__(cls, **kwargs)
and I would like to type hint **kwargs
.