Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 6 additions & 6 deletions docs/source/additional_features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ They can be defined using the :py:func:`@dataclasses.dataclass
plugins: List[str] = field(default_factory=list)

test = Application("Testing...") # OK
bad = Application("Testing...", "with plugin") # Error: List[str] expected
bad = Application("Testing...", "with plugin") # error: Argument 2 to "Application" has incompatible type "str"; expected "List[str]"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd try to keep lines a bit shorter to avoid horizontal scrolling in most cases. Maybe keep them under 100 characters? I think that it's fine to shorten messages. For example leaving out to "Application" above seems reasonable. Also, it would be fine to split he comment into multiple lines. Maybe like this:

    bad = Application("Testing...", "with plugin")  # error: Argument 2 has incompatible type "str"; 
                                                    # expected "List[str]"


Mypy will detect special methods (such as :py:meth:`__lt__ <object.__lt__>`) depending on the flags used to
define dataclasses. For example:
Expand All @@ -44,7 +44,7 @@ define dataclasses. For example:
y: int

OrderedPoint(1, 2) < OrderedPoint(3, 4) # OK
UnorderedPoint(1, 2) < UnorderedPoint(3, 4) # Error: Unsupported operand types
UnorderedPoint(1, 2) < UnorderedPoint(3, 4) # error: Unsupported left operand type for < ("UnorderedPoint")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Again, this is a bit long. Maybe leave out ("UnorderedPoint")?


Dataclasses can be generic and can be used in any other way a normal
class can be used:
Expand Down Expand Up @@ -103,8 +103,8 @@ do **not** work:
"""
attribute: int

AliasDecorated(attribute=1) # error: Unexpected keyword argument
DynamicallyDecorated(attribute=1) # error: Unexpected keyword argument
AliasDecorated(attribute=1) # error: Unexpected keyword argument "attribute" for "AliasDecorated"
DynamicallyDecorated(attribute=1) # error: Unexpected keyword argument "attribute" for "DynamicallyDecorated"

.. _attrs_package:

Expand Down Expand Up @@ -153,7 +153,7 @@ That enables this to work:
class A:
one: int = attr.ib(8)
two: Dict[str, str] = attr.Factory(dict)
bad: str = attr.ib(16) # Error: can't assign int to str
bad: str = attr.ib(16) # error: Incompatible types in assignment (expression has type "int", variable has type "str")

Caveats/Known Issues
====================
Expand All @@ -169,7 +169,7 @@ Caveats/Known Issues

import attr
YES = True
@attr.s(init=YES)
@attr.s(init=YES) # error: "init" argument must be True or False.
class A:
...

Expand Down
4 changes: 2 additions & 2 deletions docs/source/casts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ quite understand what is going on.
.. code-block:: python

def foo(o: object) -> None:
print(o + 5) # Error: can't add 'object' and 'int'
print(o + 5) # error: Unsupported operand types for + ("object" and "int")
assert isinstance(o, int)
print(o + 5) # OK: type of 'o' is 'int' here

Expand All @@ -44,6 +44,6 @@ any operations on the result. For example:
from typing import cast, Any

x = 1
x.whatever() # Type check error
x.whatever() # error: "int" has no attribute "whatever"
y = cast(Any, x)
y.whatever() # Type check OK (runtime error)
12 changes: 6 additions & 6 deletions docs/source/class_basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ initialized within the class. Mypy infers the types of attributes:

a = A(1)
a.x = 2 # OK!
a.y = 3 # Error: 'A' has no attribute 'y'
a.y = 3 # error: "A" has no attribute "y"

This is a bit like each class having an implicitly defined
:py:data:`__slots__ <object.__slots__>` attribute. This is only enforced during type
Expand Down Expand Up @@ -76,7 +76,7 @@ to it explicitly using ``self``:
def __init__(self) -> None:
self.y = 1 # Define 'y'
a = self
a.x = 1 # Error: 'x' not defined
a.x = 1 # error: "A" has no attribute "x"

Annotating __init__ methods
***************************
Expand Down Expand Up @@ -124,7 +124,7 @@ particular attribute should not be set on instances:
A.x += 1 # OK

a = A()
a.x = 1 # Error: Cannot assign to class variable "x" via instance
a.x = 1 # error: Cannot assign to class variable "x" via instance
print(a.x) # OK -- can be read through an instance

.. note::
Expand Down Expand Up @@ -183,11 +183,11 @@ override has a compatible signature:
...

class Derived1(Base):
def f(self, x: str) -> None: # Error: type of 'x' incompatible
def f(self, x: str) -> None: # error: Argument 1 of "f" is incompatible with supertype "Base"; supertype defines the argument type as "int"
...

class Derived2(Base):
def f(self, x: int, y: int) -> None: # Error: too many arguments
def f(self, x: int, y: int) -> None: # error: Signature of "f" incompatible with supertype "Base"
...

class Derived3(Base):
Expand Down Expand Up @@ -258,7 +258,7 @@ function decorator. Example:
def can_walk(self) -> bool:
return True

x = Animal() # Error: 'Animal' is abstract due to 'eat' and 'can_walk'
x = Animal() # error: Cannot instantiate abstract class 'Animal' with abstract attributes 'can_walk' and 'eat'
y = Cat() # OK

.. note::
Expand Down
16 changes: 8 additions & 8 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 @@ -582,7 +582,7 @@ the same line as the import:

# to silence complaints about unused imports
from typing import List # noqa
a = None # type: List[int]
a = [] # type: List[int]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the original example, mypy reports error: Incompatible types in assignment (expression has type "None", variable has type "List[int]").



To silence the linter on the same line as a type comment
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