This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: str.identifier shouldn't accept Python keywords
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, flox, mrabarnett, python-dev, r.david.murray, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2013-03-17 18:22 by rhettinger, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (8)
msg184389 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-03-17 18:22
>>> 'def'.isidentifier()
True
msg184391 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2013-03-17 18:38
According to the documentation, the reserved words are classified as identifiers:
http://docs.python.org/3/reference/lexical_analysis.html#keywords

There's an easy workaround:

>>> from keyword import iskeyword
>>> def is_valid_identifier(s):
...     return s.isidentifier() and not iskeyword(s)
... 
>>> is_valid_identifier('def')
False
msg184394 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-03-17 19:40
Or maybe 'is_usable_identifier' :)
msg184398 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-03-17 20:40
Hmm. I were going to use this method for re's named group (see issue14462). There is a possibility that some third-party code uses it for checking on general Unicode-aware identifiers. The language specifification says that keywords is a subset of identifiers. However in most places in stdlib (collections.namedtuple, unittest.mock, inspect.Parameter) is_usable_identifier() should be used instead of isidentifier().
msg184412 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2013-03-18 01:24
I already use it in the regex module for named groups. I don't think it would ever be a problem in practice because the names are invariably handled as strings.
msg185049 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-03-23 14:12
IMHO str.isidentifier() should not return False for Python keywords, since it's often used in contexts where Python keywords would otherwise be valid.  A keyword arg to exclude keywords could be added though, assuming there are enough use cases.
msg185065 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-03-23 15:22
New changeset a7fe48dfbfe9 by Raymond Hettinger in branch '3.3':
Issue 17447:  Clarify that str.isidentifier doesn't check for reserved keywords.
http://hg.python.org/cpython/rev/a7fe48dfbfe9
msg185066 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-03-23 15:23
I've added a clarification to the docs.  Closing this report as invalid.
History
Date User Action Args
2022-04-11 14:57:42adminsetgithub: 61649
2013-03-23 15:23:29rhettingersetstatus: open -> closed
resolution: not a bug
messages: + msg185066
2013-03-23 15:22:19python-devsetnosy: + python-dev
messages: + msg185065
2013-03-23 14:12:24ezio.melottisetversions: - Python 3.2, Python 3.3
nosy: + ezio.melotti

messages: + msg185049

type: behavior -> enhancement
2013-03-18 01:24:18mrabarnettsetnosy: + mrabarnett
messages: + msg184412
2013-03-17 20:40:38serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg184398
2013-03-17 19:40:34r.david.murraysetnosy: + r.david.murray
messages: + msg184394
2013-03-17 18:38:04floxsetnosy: + flox
messages: + msg184391
2013-03-17 18:22:54rhettingercreate