diff --git a/docs/source/additional_features.rst b/docs/source/additional_features.rst index c4922d330ae4..321a926461ef 100644 --- a/docs/source/additional_features.rst +++ b/docs/source/additional_features.rst @@ -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]" Mypy will detect special methods (such as :py:meth:`__lt__ `) depending on the flags used to define dataclasses. For example: @@ -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") Dataclasses can be generic and can be used in any other way a normal class can be used: @@ -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: @@ -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 ==================== @@ -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: ... diff --git a/docs/source/casts.rst b/docs/source/casts.rst index 61eeb3062625..6d2d417e5cfe 100644 --- a/docs/source/casts.rst +++ b/docs/source/casts.rst @@ -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 @@ -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) diff --git a/docs/source/class_basics.rst b/docs/source/class_basics.rst index 3a1f731fa8dd..263327c0cb67 100644 --- a/docs/source/class_basics.rst +++ b/docs/source/class_basics.rst @@ -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__ ` attribute. This is only enforced during type @@ -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 *************************** @@ -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:: @@ -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): @@ -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:: diff --git a/docs/source/common_issues.rst b/docs/source/common_issues.rst index 22c2a2a45820..8ed85db1d0b2 100644 --- a/docs/source/common_issues.rst +++ b/docs/source/common_issues.rst @@ -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: @@ -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 @@ -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:: @@ -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]") Possible strategies in such situations are: @@ -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] To silence the linter on the same line as a type comment @@ -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 @@ -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