How to annotate function that takes a tuple of variable length? (variadic tuple type annotation)

We can annotate variable-length homogeneous tuples using the … literal (aka Ellipsis) like this: def process_tuple(t: Tuple[str, …]): … or for Python3.9+ def process_tuple(t: tuple[str, …]): … After that, the errors should go away. From the docs: To specify a variable-length tuple of homogeneous type, use literal ellipsis, e.g. Tuple[int, …]. A plain Tuple is … Read more

How to type hint a generator in Python 3?

You need to import the typing module. As per docs: The return type of generator functions can be annotated by the generic type Generator[yield_type, send_type, return_type] provided by typing.py module Try this way instead: from typing import Generator def generate() -> Generator[int, None, None]: for i in range(10): yield i The above will have the … Read more

Type hinting / annotation (PEP 484) for numpy.ndarray

Update Check recent numpy versions for a new typing module https://numpy.org/doc/stable/reference/typing.html#module-numpy.typing dated answer It looks like typing module was developed at: https://github.com/python/typing The main numpy repository is at https://github.com/numpy/numpy Python bugs and commits can be tracked at http://bugs.python.org/ The usual way of adding a feature is to fork the main repository, develop the feature till … Read more

How can I tell PyCharm what type a parameter is expected to be?

Yes, you can use special documentation format for methods and their parameters so that PyCharm can know the type. Recent PyCharm version supports most common doc formats. For example, PyCharm extracts types from @param style comments. See also reStructuredText and docstring conventions (PEP 257). Another option is Python 3 annotations. Please refer to the PyCharm … Read more

Type hints with user defined classes

The former is correct, if arg accepts an instance of CustomClass: def FuncA(arg: CustomClass): # ^ instance of CustomClass In case you want the class CustomClass itself (or a subtype), then you should write: from typing import Type # you have to import Type def FuncA(arg: Type[CustomClass]): # ^ CustomClass (class object) itself Like it … Read more

Using List/Tuple/etc. from typing vs directly referring type as list/tuple/etc

Until Python 3.9 added support for type hinting using standard collections, you had to use typing.Tuple and typing.List if you wanted to document what type the contents of the containers needed to be: def f(points: Tuple[float, float]): return map(do_stuff, points) Up until Python 3.8, tuple and list did not support being used as generic types. … Read more

Type hints in namedtuple

The prefered Syntax for a typed named tuple since 3.6 is from typing import NamedTuple class Point(NamedTuple): x: int y: int = 1 # Set default value Point(3) # -> Point(x=3, y=1) Edit Starting Python 3.7, consider using dataclasses (your IDE may not yet support them for static type checking): from dataclasses import dataclass @dataclass … Read more