Skip to content

Commit f9eb240

Browse files
committed
Added documentation for starred types
1 parent 7b8ba4e commit f9eb240

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

docs/source/tutorial.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,24 @@ The type Tuple[t, ...] represents a tuple with the item types t, ...:
248248
t = 1, 'foo' # OK
249249
t = 'foo', 1 # Type check error
250250
251+
Starred expressions
252+
******************************
253+
254+
In most cases, mypy can infer the type of starred expressions from the right-hand side of an assignment, but not always:
255+
256+
.. code-block:: python
257+
258+
a, *bs = 1, 2, 3 # OK
259+
p, q, *rs = 1, 2 # Error: Type of cs cannot be inferred
260+
261+
On first line, the type of ``bs`` is inferred to be ``List[int]``. However, on the second line, mypy cannot infer the type of ``rs``, because there is no right-hand side value for ``rs`` to infer the type from. In cases like these, the starred expression needs to be annotated with a starred type:
262+
263+
.. code-block:: python
264+
265+
p, q, *rs = 1, 2 # type: int, int, *List[int]
266+
267+
Here, the type of ``rs`` is set to ``List[int]``.
268+
251269
Class name forward references
252270
*****************************
253271

0 commit comments

Comments
 (0)