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: Lib/argparse.py uses `is` for string comparison
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: izbyshev, rth, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2018-12-06 20:27 by rth, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 11002 closed rth, 2018-12-06 20:46
Messages (4)
msg331246 - (view) Author: Roman Yurchak (rth) * Date: 2018-12-06 20:27
Lib/argparse.py uses `is` for string comparison,

`
221:            if self.heading is not SUPPRESS and self.heading is not None:
247:        if text is not SUPPRESS and text is not None:
251:        if usage is not SUPPRESS:
256:        if action.help is not SUPPRESS:
290:                        if part and part is not SUPPRESS])
679:            if action.default is not SUPPRESS:
1130:        if self.dest is not SUPPRESS:
1766:            if action.dest is not SUPPRESS:
1768:                    if action.default is not SUPPRESS:
1851:            if argument_values is not SUPPRESS:
2026:                             if action.help is not SUPPRESS]
`

where `SUPPRESS = '==SUPPRESS=='`. Unless I'm missing something this can produce false negatives if the variable that we compare against is a slice from another string. Using equality is probably safer in any case.

Detected with LGTM.com analysis.
msg331250 - (view) Author: Alexey Izbyshev (izbyshev) * (Python triager) Date: 2018-12-06 21:12
argparse.SUPPRESS is an opaque value to be used by argparse clients. It could be anything, it just happens to be a string. So the code doesn't compare strings but checks whether a supplied object *is* the opaque value. I do not see any problem with this code, though one way to avoid LGTM diagnostics would be to use a non-string opaque value instead.
msg331252 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-12-06 21:21
I concur with Alexey. This is a correct use of the 'is' operator.
msg331253 - (view) Author: Roman Yurchak (rth) * Date: 2018-12-06 21:25
Thanks, Alexey and Serhiy! Looking at the code more closely I would agree. I guess changing the value of the suppress object to something else to avoid the warning, has a potential of breaking code that relies on the current functionality and is not worth it...
History
Date User Action Args
2022-04-11 14:59:09adminsetgithub: 79611
2018-12-06 21:25:02rthsetmessages: + msg331253
2018-12-06 21:21:55serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg331252

resolution: not a bug
stage: patch review -> resolved
2018-12-06 21:12:27izbyshevsetnosy: + izbyshev
messages: + msg331250
2018-12-06 20:46:46rthsetkeywords: + patch
stage: patch review
pull_requests: + pull_request10241
2018-12-06 20:27:58rthcreate