-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Update error examples in documentation #3404
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8e4296b
use actual error messages when giving examples
vilmibm 9a32a15
add section on variable type declaration
vilmibm 94aa1ba
minor whitespace/punctuation changes
vilmibm 49ec476
Favor pre 3.6 variable type annotation
vilmibm 208ced4
finish auditing errors in docs
vilmibm File filter
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
finish auditing errors in docs
- Adds an actual error to code samples where appropriate - Fixes some grammatical issues - Fixes some whitespace - Removes some inconsistencies in formatting - Fixes some illegal Python samples (ie forgetting `pass`)
- Loading branch information
commit 208ced4d34d17b89778e6eb7bfd42f4cf8cc83b5
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,7 +83,7 @@ Functions | |
| # type: (int) -> None | ||
| pass | ||
| quux(3) # Fine | ||
| quux(__x=3) # Error | ||
| quux(__x=3) # error: Unexpected keyword argument "__x" for "quux" | ||
|
|
||
| # This is how you annotate a function value. | ||
| x = f # type: Callable[[int, float], float] | ||
|
|
@@ -106,7 +106,7 @@ Functions | |
| body=None # type: List[str] | ||
| ): | ||
| # type: (...) -> bool | ||
| <code> | ||
| pass | ||
|
|
||
|
|
||
| When you're puzzled or when things are complicated | ||
|
|
@@ -119,7 +119,7 @@ When you're puzzled or when things are complicated | |
| # To find out what type mypy infers for an expression anywhere in | ||
| # your program, wrap it in reveal_type. Mypy will print an error | ||
| # message with the type; remove it again before running the code. | ||
| reveal_type(1) # -> error: Revealed type is 'builtins.int' | ||
| reveal_type(1) # error: Revealed type is 'builtins.int' | ||
|
|
||
| # Use Union when something could be one of a few types. | ||
| x = [3, 5, "test", "fun"] # type: List[Union[int, str]] | ||
|
|
@@ -146,8 +146,8 @@ When you're puzzled or when things are complicated | |
| a = [4] | ||
| b = cast(List[int], a) # passes fine | ||
| c = cast(List[str], a) # passes fine (no runtime check) | ||
| reveal_type(c) # -> error: Revealed type is 'builtins.list[builtins.str]' | ||
| print(c) # -> [4] the object is not cast | ||
| reveal_type(c) # error: Revealed type is 'builtins.list[builtins.str]' | ||
| print(c) # [4] the object is not cast | ||
|
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. Perhaps, it would be good to give an indication that print(c) # prints [4] because the cast did not change the object's type at runtime |
||
|
|
||
| # TODO: explain "Need type annotation for variable" when | ||
| # initializing with None or an empty container | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,7 +78,7 @@ Python 3 introduces an annotation syntax for function declarations in `PEP 3107 | |
| def quux(__x: int) -> None: | ||
| pass | ||
| quux(3) # Fine | ||
| quux(__x=3) # Error | ||
| quux(__x=3) # error: Unexpected keyword argument "__x" for "quux" | ||
|
|
||
| # This is how you annotate a function value. | ||
| x = f # type: Callable[[int, float], float] | ||
|
|
@@ -113,7 +113,7 @@ When you're puzzled or when things are complicated | |
| # To find out what type mypy infers for an expression anywhere in | ||
| # your program, wrap it in reveal_type. Mypy will print an error | ||
| # message with the type; remove it again before running the code. | ||
| reveal_type(1) # -> error: Revealed type is 'builtins.int' | ||
| reveal_type(1) # error: Revealed type is 'builtins.int' | ||
|
|
||
| # Use Union when something could be one of a few types. | ||
| x = [3, 5, "test", "fun"] # type: List[Union[int, str]] | ||
|
|
@@ -139,8 +139,8 @@ When you're puzzled or when things are complicated | |
| a = [4] | ||
| b = cast(List[int], a) # passes fine | ||
| c = cast(List[str], a) # passes fine (no runtime check) | ||
| reveal_type(c) # -> error: Revealed type is 'builtins.list[builtins.str]' | ||
| print(c) # -> [4] the object is not cast | ||
| reveal_type(c) # error: Revealed type is 'builtins.list[builtins.str]' | ||
| print(c) # [4] the object is not cast | ||
|
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. See comment for py2 cheatsheet. |
||
|
|
||
| # TODO: explain "Need type annotation for variable" when | ||
| # initializing with None or an empty container | ||
|
|
@@ -190,8 +190,6 @@ Classes | |
| def my_method(self, num: int, str1: str) -> str: | ||
| return num * str1 | ||
|
|
||
|
|
||
|
|
||
| # User-defined classes are written with just their own names. | ||
| x = MyClass() # type: MyClass | ||
|
|
||
|
|
@@ -228,7 +226,7 @@ Other stuff | |
|
|
||
| # forward references are useful if you want to referemce a class before it is designed | ||
|
|
||
| def f(foo: A) -> int: # this will fail | ||
| def f(foo: A) -> int: # this will fail at runtime | ||
| ... | ||
|
|
||
| class A: | ||
|
|
@@ -253,7 +251,7 @@ Mypy brings limited support for PEP 526 annotations. | |
| name: str = "Eric Idle" | ||
|
|
||
| # class instances can be annotated as follows | ||
| mc : MyClass = MyClass() | ||
| mc: MyClass = MyClass() | ||
|
|
||
| # tuple packing can be done as follows | ||
| tu: Tuple[str, ...] = ('a', 'b', 'c') | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think it might be better to keep
<code>here because if you putpassit technically doesn't typecheck. Or, perhaps, we can make the return type of the functionNoneinstead ofbool.Uh oh!
There was an error while loading. Please reload this page.
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.
Whoops, just tested it and
passdoes typecheck :)I think it's okay to leave
passthere then.