-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Conversation
Can you fix the Travis failures? Looks like mypy doesn't actually support variable annotation syntax in pre-3.6 stubs. |
@ambv Is it true that you plan to move all stubs to 3.6 variable annotations? It was out my focus for few months, but I am going to improve it and add opposite transformation (annotation to comments), support for function type comments (2.7 syntax), support for docstrings etc. |
Even if we were in agreement that we should use the 3.6 notation for all stubs I still would not want to do a mass conversion. Instead I would just do it opportunistically over time. |
I am a bit mystified by the test failures. I can repro these too and there seem to be two sets:
|
I only did the runtime implementation in |
Sure, I can work on that once python/mypy#2719 is in mypy. |
So now that python/mypy#3000 has landed, there are two remaining issues:
|
Would an instance variable annotation work? It'd communicate what type to expect when accessing the attribute: class TokenInfo(NamedTuple):
# ...
# @property
exact_type: int Otherwise, is the next option to retain |
stdlib/3/tokenize.pyi
Outdated
def untokenize(iterable): ... | ||
def detect_encoding(readline): ... | ||
def tokenize(readline): ... | ||
def untokenize(iterable: Iterable[_Token]) -> Union[str, bytes]: ... |
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 str
if they pass in an encoding, at which point they shouldn't have to deal with bytes
. Any
it is.
I think the best option would be to make the NamedTuple class private and inherit from that, something like this:
I haven't tested it but I believe mypy supports this. It's also consistent with the way this class is actually implemented in tokenize.py. |
It's a NamedTuple -- a plain attribute sounds like a fine idea to me. |
If we make it a plain attribute, type checkers will think that you need to pass in six arguments (including |
Fair enough, so the solution with the helper class should work. (We also
should fix this in mypy so it won't be necessary.)
But there's still the issue of the PEP 526 syntax not being supported
(yet?) by mypy in stubs for Python 3.2--3.5. If we're not fixing that we
should just cave and use the classic NamedTuple syntax.
…On Wed, Mar 15, 2017 at 8:18 AM, Jelle Zijlstra ***@***.***> wrote:
If we make it a plain attribute, type checkers will think that you need to
pass in six arguments (including exact_type) in order to construct a
TokenInfo, but you really only need five.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#984 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ACwrMnknn7Ycfg6H9BK-C1BmyzrMDrZkks5rmAEogaJpZM4MaZx1>
.
--
--Guido van Rossum (python.org/~guido)
|
I'll cave and use the 3.5 style for now. @ambv can upgrade it to 3.6 if and when support is there. |
def tokenize(readline): ... | ||
def untokenize(iterable: Iterable[_Token]) -> Any: ... | ||
def detect_encoding(readline: Callable[[], bytes]) -> Tuple[str, Sequence[bytes]]: ... | ||
def tokenize(readline: Callable[[], bytes]) -> Generator[TokenInfo, None, None]: ... |
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.
@mjpieters This is fine but in the future, if you're type hinting the return value of a simple generator, simply use -> Iterator[SomeType]
.
stdlib/3/tokenize.pyi
Outdated
ENCODING = 0 | ||
COMMENT = ... # type: int | ||
NL = ... # type: int | ||
ENCODING = ... # type: int |
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.
You'll want to update token.py too in that case; I followed the style from that stub here.
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.
Will do :)
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.
Might be good to add this to CONTRIBUTING.md or the linter; I've been unsure in the past what style to use for this kind of thing.
Whee! Thanks everyone for the truly multidisciplinary review and improvements. |
If this is not clear from the comments below:
In other words, if we change variables in tokenize.pyi to use PEP 526 annotations, everything works fine when running with
|
Aha! I had missed the distinction. If you think this is worth fixing please
file a mypy bug!
|
Note that I used the 3.6 syntax for the
TokenInfo
namedtuple; @ambv stated he plans to move all of typeshed to that syntax.