@@ -134,7 +134,7 @@ module:
134134
135135.. code-block :: python
136136
137- import frobnicate # Error: No module "frobnicate"
137+ import frobnicate # error: Cannot find implementation or library stub for module named "frobnicate"
138138 frobnicate.start()
139139
140140 You can add a ``# type: ignore `` comment to tell mypy to ignore this
@@ -229,16 +229,20 @@ Mypy may depend on orjson by default in the future.
229229Types of empty collections
230230--------------------------
231231
232+ Without the annotation mypy can't always figure out the
233+ precise type of ``a ``.
234+
235+ .. code-block :: python
236+
237+ a = [] # error: Need type annotation for "a" (hint: "a: list[<type>] = ...")
238+
232239 You often need to specify the type when you assign an empty list or
233240dict to a new variable, as mentioned earlier:
234241
235242.. code-block :: python
236243
237244 a: list[int ] = []
238245
239- Without the annotation mypy can't always figure out the
240- precise type of ``a ``.
241-
242246 You can use a simple empty list literal in a dynamically typed function (as the
243247type of ``a `` would be implicitly ``Any `` and need not be inferred), if type
244248of the variable has been declared or inferred before, or if you perform a simple
@@ -306,7 +310,9 @@ unexpected errors when combined with type inference. For example:
306310
307311 lst = [A(), A()] # Inferred type is list[A]
308312 new_lst = [B(), B()] # inferred type is list[B]
309- lst = new_lst # mypy will complain about this, because List is invariant
313+ lst = new_lst # error: Incompatible types in assignment (expression has type "list[B]", variable has type "list[A]")
314+ # note: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
315+ # note: Consider using "Sequence" instead, which is covariant
310316
311317 Possible strategies in such situations are:
312318
@@ -329,7 +335,7 @@ Possible strategies in such situations are:
329335
330336 def f_bad (x : list[A]) -> A:
331337 return x[0 ]
332- f_bad(new_lst) # Fails
338+ f_bad(new_lst) # error: Argument 1 to "f_bad" has incompatible type List[B]; expected List[A]
333339
334340 def f_good (x : Sequence[A]) -> A:
335341 return x[0 ]
0 commit comments