Skip to content

add keyword #446

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 3 commits into from
Aug 5, 2016
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
6 changes: 6 additions & 0 deletions stdlib/2and3/keyword.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Stubs for keyword

from typing import Sequence

def iskeyword(s: str) -> bool: ...
Copy link
Contributor

@matthiaskramm matthiaskramm Aug 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also works on bytes, Tuple[str], Tuple[bytes], frozenset[str], etc.

We could use something like Iterable[Union[Text, bytes]], but it also needs to be hashable.
Maybe just Union[Text, bytes] is good enough.

Copy link
Contributor Author

@tharvik tharvik Aug 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, it works on every Hashable, but I'm unable to find a concret example where it would be correct to test that a frozenset is a keyword, it would be better to trigger a warning.

In CPython implementation, the code is simply

iskeyword = frozenset(kwlist).__contains__

I don't think that was intended to handle testing other type of value, it works, but that's not the point.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. But we should at least allow unicode (on Python 2) and bytes (on Python 3).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using AnyStr then.

kwlist = ... # type: Sequence[str]