From 1190ddf178ecdf30da125659d374b1ba5e048111 Mon Sep 17 00:00:00 2001 From: John Biersdorf Date: Wed, 3 Dec 2025 18:50:42 -0800 Subject: [PATCH 1/2] resolves error examples in common issues page --- docs/source/common_issues.rst | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/docs/source/common_issues.rst b/docs/source/common_issues.rst index e4239bd7a8ee..26656319c353 100644 --- a/docs/source/common_issues.rst +++ b/docs/source/common_issues.rst @@ -134,7 +134,7 @@ module: .. code-block:: python - import frobnicate # Error: No module "frobnicate" + import frobnicate # error: Cannot find implementation or library stub for module named "frobnicate" frobnicate.start() You can add a ``# type: ignore`` comment to tell mypy to ignore this @@ -229,6 +229,13 @@ Mypy may depend on orjson by default in the future. Types of empty collections -------------------------- +Without the annotation mypy can't always figure out the +precise type of ``a``. + +.. code-block:: python + + a = [] # error: Need type annotation for "a" (hint: "a: list[] = ...") + You often need to specify the type when you assign an empty list or dict to a new variable, as mentioned earlier: @@ -236,9 +243,6 @@ dict to a new variable, as mentioned earlier: a: list[int] = [] -Without the annotation mypy can't always figure out the -precise type of ``a``. - You can use a simple empty list literal in a dynamically typed function (as the type of ``a`` would be implicitly ``Any`` and need not be inferred), if type of 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: lst = [A(), A()] # Inferred type is list[A] new_lst = [B(), B()] # inferred type is list[B] - lst = new_lst # mypy will complain about this, because List is invariant + lst = new_lst # error: Incompatible types in assignment (expression has type "list[B]", variable has type "list[A]") + # note: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance + # note: Consider using "Sequence" instead, which is covariant Possible strategies in such situations are: @@ -329,7 +335,7 @@ Possible strategies in such situations are: def f_bad(x: list[A]) -> A: return x[0] - f_bad(new_lst) # Fails + f_bad(new_lst) # error: Argument 1 to "f_bad" has incompatible type List[B]; expected List[A] def f_good(x: Sequence[A]) -> A: return x[0] From cf898daad0bfae50b0115edf9c2ebd18cb8f36c6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 4 Dec 2025 03:12:23 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/source/common_issues.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/common_issues.rst b/docs/source/common_issues.rst index 26656319c353..82d99146e1ee 100644 --- a/docs/source/common_issues.rst +++ b/docs/source/common_issues.rst @@ -233,7 +233,7 @@ Without the annotation mypy can't always figure out the precise type of ``a``. .. code-block:: python - + a = [] # error: Need type annotation for "a" (hint: "a: list[] = ...") You often need to specify the type when you assign an empty list or