New submission from Serhiy Storchaka <storchaka+cpyt...@gmail.com>:

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.

----------
components: Interpreter Core
messages: 326714
nosy: barry, gregory.p.smith, gvanrossum, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Emit a syntax warning for "is" with a literal
type: enhancement
versions: Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue34850>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to