Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
revisited mypy error messages in common_issues.rst
Signed-off-by: Oleg Höfling <[email protected]>
  • Loading branch information
hoefling committed Oct 13, 2019
commit bd24079bc513f5f1a631e3b8169f8b4bb7a51ddc
14 changes: 7 additions & 7 deletions docs/source/common_issues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ flagged as an error.
.. code-block:: python

def foo(a: str) -> str:
return '(' + a.split() + ')'
# error: Unsupported operand types for + ("str" and List[str])
return '(' + a.split() + ')' # error: Unsupported operand types for + ("str" and "List[str]")

If you don't know what types to add, you can use ``Any``, but beware:

Expand Down Expand Up @@ -110,7 +109,7 @@ module:

.. code-block:: python

import frobnicate # Error: No module "frobnicate"
import frobnicate # error: Cannot find module named 'frobnicate'
frobnicate.start()

You can add a ``# type: ignore`` comment to tell mypy to ignore this
Expand Down Expand Up @@ -234,7 +233,7 @@ with the ``Any`` type.
def f() -> None:
n = 1
...
n = 'x' # Type error: n has type int
n = 'x' # error: Incompatible types in assignment (expression has type "str", variable has type "int")

.. note::

Expand Down Expand Up @@ -270,7 +269,8 @@ 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
# 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]")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please advise whether keeping the explanation comments is useful, and whether the selected format is acceptable:
<code> # original explanation becomes

# original explanation
<code>  # mypy error

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine to me. Though here the explanation is pretty informal, and it may be better to be more explicit about what this refers to. Here is one idea:

    # Mypy rejects the following assignment, because List is invariant
    lst = new_lst  # error: ...


Possible strategies in such situations are:

Expand Down Expand Up @@ -611,7 +611,7 @@ Consider this example:
class C:
x = 42
c = C()
fun(c) # This is not safe
fun(c) # This is not safe: # error: Argument 1 to "fun" has incompatible type "C"; expected "P"
c.x << 5 # Since this will fail!

To work around this problem consider whether "mutating" is actually part
Expand Down Expand Up @@ -646,7 +646,7 @@ method signature. E.g.:
class Message:
def bytes(self):
...
def register(self, path: bytes): # error: Invalid type "mod.Message.bytes"
def register(self, path: bytes): # error: Function "__main__.Message.bytes" is not valid as a type
...

The third line elicits an error because mypy sees the argument type
Expand Down