Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify SyntaxWarning with literal comparison #103492

Closed
hauntsaninja opened this issue Apr 13, 2023 · 1 comment
Closed

Clarify SyntaxWarning with literal comparison #103492

hauntsaninja opened this issue Apr 13, 2023 · 1 comment
Labels
type-feature A feature request or enhancement

Comments

@hauntsaninja
Copy link
Contributor

hauntsaninja commented Apr 13, 2023

I had a coworker slightly confused about the following warning, since None is a literal and is not None is idiomatic:

>>> 'a' is not None
<stdin>:1: SyntaxWarning: "is not" with a literal. Did you mean "!="?
True

I think this would be better as:

>>> 'a' is not None
<stdin>:1: SyntaxWarning: "is not" with str literal. Did you mean "!="?
True

Linked PRs

@hauntsaninja hauntsaninja added the type-feature A feature request or enhancement label Apr 13, 2023
hauntsaninja added a commit to hauntsaninja/cpython that referenced this issue Apr 13, 2023
hauntsaninja added a commit to hauntsaninja/cpython that referenced this issue Apr 13, 2023
@terryjreedy
Copy link
Member

https://docs.python.org/3/reference/lexical_analysis.html#literals

Literals are notations for constant values of some built-in types.
In particular bytes, strings, and numbers are discussed in the following.
https://docs.python.org/3/reference/expressions.html#literals is more specific.
Python supports string and bytes literals and various numeric literals:
Evaluation of a literal yields an object of the given type (string, bytes, integer, floating point number, complex number) with the given value.

None is a constant, but not a 'literal' as defined in the sections above. This is only to say that the current message is not really ambiguous. Still, the added detail may help some.

https://docs.python.org/3/reference/expressions.html#parenthesized-forms
A tuple is a 'parenthesized form' that is either empty or has at least 1 comma. A constant tuple is not really a literal, but testing I see that one is already called a 'literal'.

The test changes look good; can't review the c code.

carljm added a commit to carljm/cpython that referenced this issue Apr 24, 2023
* main:
  pythongh-100227: Only Use deepfreeze for the Main Interpreter (pythongh-103794)
  pythongh-103492: Clarify SyntaxWarning with literal comparison (python#103493)
  pythongh-101100: Fix Sphinx warnings in `argparse` module (python#103289)
carljm added a commit to carljm/cpython that referenced this issue Apr 24, 2023
* superopt:
  pythongh-100227: Only Use deepfreeze for the Main Interpreter (pythongh-103794)
  pythongh-103492: Clarify SyntaxWarning with literal comparison (python#103493)
  pythongh-101100: Fix Sphinx warnings in `argparse` module (python#103289)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants