Skip to content
Merged
Changes from 1 commit
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
Next Next commit
Complete the tokenize module type hints
  • Loading branch information
Martijn Pieters
Martijn Pieters committed Mar 12, 2017
commit 868a8249bd0bd8f36f052e5bffcf7471272ad314
44 changes: 27 additions & 17 deletions stdlib/3/tokenize.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]: ...

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

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 str if they pass in an encoding, at which point they shouldn't have to deal with bytes. Any it is.

def detect_encoding(readline: Callable[[], bytes]) -> Tuple[str, Sequence[bytes]]: ...
def tokenize(readline: Callable[[], bytes]) -> Generator[TokenInfo, None, None]: ...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 -> Iterator[SomeType].


def open(filename: Union[str, bytes, int]) -> TextIO: ...

Expand Down