-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Update mypy error messages in code examples, part 1 #7706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6e3523b
5b23ebd
49f5059
bd24079
f542adb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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__ <object.__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") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, this is a bit long. Maybe leave out |
||
|
|
||
| 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: | ||
| ... | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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]") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
||
|
|
||
| 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] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the original example, |
||
|
|
||
|
|
||
| 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 | ||
|
|
||
There was a problem hiding this comment.
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: