Repository navigation
Complete the tokenize module type hints #984
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Next
Next commit
Complete the tokenize module type hints
- Loading branch information
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,34 +2,44 @@ | |
| # | ||
| # NOTE: This dynamically typed stub was automatically generated by stubgen. | ||
|
|
||
| from typing import Any, Union, TextIO | ||
| from typing import Any, Callable, Generator, Iterable, List, NamedTuple, Union, Sequence, TextIO, Tuple | ||
| from builtins import open as _builtin_open | ||
| from token import * # noqa: F403 | ||
|
|
||
| COMMENT = ... # type: Any | ||
| NL = ... # type: Any | ||
| ENCODING = ... # type: Any | ||
| COMMENT = 0 | ||
| NL = 0 | ||
| ENCODING = 0 | ||
|
|
||
| class TokenInfo: | ||
| _Position = Tuple[int, int] | ||
|
|
||
| class TokenInfo(NamedTuple): | ||
| type: int | ||
| string: str | ||
| start: _Position | ||
| end: _Position | ||
| line: str | ||
| @property | ||
| def exact_type(self): ... | ||
| def exact_type(self) -> int: ... | ||
|
|
||
| # Backwards compatible tokens can be sequences of a shorter length too | ||
| _Token = Union[TokenInfo, Sequence[Union[int, str, _Position]]] | ||
|
|
||
| class TokenError(Exception): ... | ||
| class StopTokenizing(Exception): ... | ||
|
|
||
| class Untokenizer: | ||
| tokens = ... # type: Any | ||
| prev_row = ... # type: Any | ||
| prev_col = ... # type: Any | ||
| encoding = ... # type: Any | ||
| def __init__(self): ... | ||
| def add_whitespace(self, start): ... | ||
| def untokenize(self, iterable): ... | ||
| def compat(self, token, iterable): ... | ||
| tokens = ... # type: List[str] | ||
| prev_row = ... # type: int | ||
| prev_col = ... # type: int | ||
| encoding = ... # type: Optional[str] | ||
| def __init__(self) -> None: ... | ||
| def add_whitespace(self, start: _Position) -> None: ... | ||
| def untokenize(self, iterable: Iterable[_Token]) -> str: ... | ||
| def compat(self, token: Sequence[Union[int, str]], iterable: Iterable[_Token]) -> None: ... | ||
|
|
||
| def untokenize(iterable): ... | ||
| def detect_encoding(readline): ... | ||
| def tokenize(readline): ... | ||
| def untokenize(iterable: Iterable[_Token]) -> Union[str, bytes]: ... | ||
| def detect_encoding(readline: Callable[[], bytes]) -> Tuple[str, Sequence[bytes]]: ... | ||
| def tokenize(readline: Callable[[], bytes]) -> Generator[TokenInfo, None, None]: ... | ||
|
Contributor
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. @mjpieters This is fine but in the future, if you're type hinting the return value of a simple generator, simply use |
||
|
|
||
| def open(filename: Union[str, bytes, int]) -> TextIO: ... | ||
|
|
||
|
|
||
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.
We should almost never return a Union because Union handling is often buggy in mypy and using Unions forces calling code into tedious isinstance checks. I think this one should just return Any.
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.
Ah, no, we should use
AnyStr.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.
AnyStr doesn't make sense here because it's a type variable; see Jukka's discussion in #871.
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.
Ah, good point. The caller will only get
strif they pass in an encoding, at which point they shouldn't have to deal withbytes.Anyit is.