Message326714
Gregory have noticed that the "is" and "is not" operator sometimes is used with string and numerical literals. This code "works" on CPython by accident, because of caching on different levels (small integers and strings caches, interned strings, deduplicating constants at compile time). But it shouldn't work on other implementations, and can not work even on early or future CPython versions.
https://discuss.python.org/t/demoting-the-is-operator-to-avoid-an-identity-crisis/86
I think that the adequate solution of this issue is not demoting the "is" operator, but emitting a syntax warning. In general, this is a work for third-party checkers. But many people don't use checkers for their one-time scripts, using "is" with a literal is always a mistake, and it is easy to add a check for this in the compiler.
Currently the compiler emits SyntaxWarning only for parenthesis in assert: `assert(x, "msg")`. But in earlier versions there were more warnings (they are errors). In 3.8 yet one SyntaxWarning will be added instead of DeprecationWarning for unknown escape sequences in string literals.
The proposed PR makes the compiler emitting a SyntaxWarning when the "is" or "is not" operators are used with a constant (except singletons None, False, True and ...). A warning will be replaced with a SyntaxError if the warnings system is configured to raise errors. This is because SyntaxError contains more information and provides better traceback. The same change was made for "assert(...)". Added tests, including tests for "assert(...)".
Barry have noted that using the "==" operator with None can be also classified as an error. But I think that in this case there may be legal uses of this, and the compiler should not complain. It is a work for third-party checkers, which can provide configuration options for enabling and disabling particular kinds of checks. |
|
Date |
User |
Action |
Args |
2018-09-30 08:15:19 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, gvanrossum, barry, gregory.p.smith |
2018-09-30 08:15:19 | serhiy.storchaka | set | messageid: <1538295319.77.0.545547206417.issue34850@psf.upfronthosting.co.za> |
2018-09-30 08:15:19 | serhiy.storchaka | link | issue34850 messages |
2018-09-30 08:15:19 | serhiy.storchaka | create | |
|