FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-103492: Clarify SyntaxWarning with literal comparison by hauntsaninja · Pull Request #103493 · python/cpython · GitHub

/ cpython Public

gh-103492: Clarify SyntaxWarning with literal comparison - #103493

Merged
hauntsaninja merged 10 commits into
python:mainfrom
hauntsaninja:gh-103492
Apr 24, 2023
Merged

gh-103492: Clarify SyntaxWarning with literal comparison#103493
hauntsaninja merged 10 commits into
python:mainfrom
hauntsaninja:gh-103492

Conversation

hauntsaninja commented Apr 13, 2023
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Contributor

hauntsaninja marked this pull request as ready for review April 13, 2023 00:59

sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Good idea, the code looks correct to me as well :)
Thank you!

Comment thread Lib/test/test_codeop.py Outdated
# Test that the warning is only returned once.
with warnings_helper.check_warnings(
('"is" with a literal', SyntaxWarning),
('"is" with str literal', SyntaxWarning),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

would it be clearer to have "is" with literal of type 'str'?

Note that types are often in `:

>>> 1+'d'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

But consistently with only double quotes, or only single quotes?

hauntsaninja Apr 24, 2023
edited
Loading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks, I've added quotes to the types. I kept the phrase 'str' literal instead of literal of type 'str', since I found it a little easier to read and is similar to the way we just mention the types in the add example you give (instead of e.g. saying value of type 'int' and value of type 'str')

I'm happy with whatever quote type, let me know!

Comment thread Python/compile.c Outdated
: "\"is not\" with '%.200s' literal. Did you mean \"!=\"?";
expr_ty literal = !left ? e->v.Compare.left : right_expr;
return compiler_warn(
c, LOC(e), msg, Py_TYPE(literal->v.Constant.value)->tp_name

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
c, LOC(e), msg, Py_TYPE(literal->v.Constant.value)->tp_name
c, LOC(e), msg, infer_type(literal)->tp_name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I had this originally, but it needed a forward reference / we know it's a Constant_kind so we could skip the switch :-)

I'll add it back!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Addressed, let me know if I should move the function instead

Comment thread Python/compile.c Outdated
return compiler_warn(c, LOC(e), msg);
? "\"is\" with '%.200s' literal. Did you mean \"==\"?"
: "\"is not\" with '%.200s' literal. Did you mean \"!=\"?";
expr_ty literal = !left ? e->v.Compare.left : right_expr;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

left gets updated in each iteration, but e->v.Compare.left is always the same thing. Is this correct?

Should we just check the first item before the loop and then here just look at right_expr?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I might be missing something, but bool left = check_is_arg(e->v.Compare.left); happens once outside the loop and it's only right that is updated in each iteration.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

line 2296?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Ah, of course! Thank you so much, this is buggy. I added the fix and a test case to catch the issue.

I think we can't do the first item check before the loop since all comparators may not be is, e.g. x == 3 is y, but let me know if I'm missing something!

Comment thread Python/compile.c

Copy link
Copy Markdown
Contributor Author

Thank you for the review!

hauntsaninja merged commit ae25855 into python:main Apr 24, 2023
carljm added a commit to carljm/cpython that referenced this pull request 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 pull request 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)
hauntsaninja deleted the gh-103492 branch April 29, 2023 07:53
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants


Back | FazBrowse Home | New Git URL