Skip to content

keyword.iskeyword() and keyword.issoftkeyword() don't accept the unhashable types list, dict and bytearray but accept set #138154

@hyperkai

Description

@hyperkai

Bug report

Bug description:

keyword.iskeyword() and keyword.issoftkeyword() don't accept the unhashable types list, dict and bytearray as shown below:

from keyword import iskeyword

print(iskeyword([1, 2, 3]))         # TypeError: unhashable type: 'list'
print(iskeyword({'name':'John'}))   # TypeError: unhashable type: 'dict'
print(iskeyword(bytearray(b'ABC'))) # TypeError: unhashable type: 'bytearray'
from keyword import issoftkeyword

print(issoftkeyword([1, 2, 3]))         # TypeError: unhashable type: 'list'
print(issoftkeyword({'name':'John'}))   # TypeError: unhashable type: 'dict'
print(issoftkeyword(bytearray(b'ABC'))) # TypeError: unhashable type: 'bytearray'

But keyword.iskeyword() and keyword.issoftkeyword() accept the unhashable type set as shown below:

from keyword import iskeyword

print(iskeyword({1, 2, 3}))
# False
from keyword import issoftkeyword

print(issoftkeyword({1, 2, 3}))
# False

And, keyword.iskeyword() and keyword.issoftkeyword() accept the hashable types str, bytes, tuple and frozenset as shown below:

from keyword import iskeyword

print(iskeyword('ABC'))
print(iskeyword(b'ABC'))
print(iskeyword((1, 2, 3)))
print(iskeyword(frozenset([1, 2, 3])))
# False
from keyword import issoftkeyword

print(issoftkeyword('ABC'))
print(issoftkeyword(b'ABC'))
print(issoftkeyword((1, 2, 3)))
print(issoftkeyword(frozenset([1, 2, 3])))
# False

So, keyword.iskeyword() and keyword.issoftkeyword() shouldn't accept all unhashable types including set for consistency.

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions