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

bpo-32888: Improve exception message in ast.literal_eval #340

Closed
wants to merge 4 commits into from

Conversation

Rosuav
Copy link
Contributor

@Rosuav Rosuav commented Feb 27, 2017

When a non-literal is given to literal_eval, attempt to be more
helpful with the message, rather than calling it 'malformed'.

https://bugs.python.org/issue32888

@merwok
Copy link
Member

merwok commented Feb 18, 2018

This looks good, do you mind opening a bugs.python.org ticket and adding a news entry?

@Rosuav Rosuav changed the title Improve exception message in ast.literal_eval bpo-32888: Improve exception message in ast.literal_eval Feb 20, 2018
When a non-literal is given to literal_eval, attempt to be more
helpful with the message, rather than calling it 'malformed'.
@Rosuav
Copy link
Contributor Author

Rosuav commented Feb 20, 2018

Rebased onto current master, which includes shifting where the actual change happens. NEWS entry added, issue number added.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Please add a special test in test_ast.

@@ -52,6 +52,8 @@ def _convert_num(node):
return node.value
elif isinstance(node, Num):
return node.n
elif isinstance(node, AST):
raise ValueError('%s not allowed in literal' % type(node).__name__)
raise ValueError('malformed node or string: ' + repr(node))
Copy link
Member

Choose a reason for hiding this comment

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

In what cases this exception is raised? Is it covered by tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The easiest way to trigger it is to use something that isn't permitted in literal_eval, but is valid syntax. There's already one such test - proving that f-strings aren't literal_evallable - and I guess there could be more, but since this is a whitelist, the error is basically "anything else, raise". The only difference in this patch is that the message is different for "syntactically valid but not acceptable by literal_eval" and "syntactically invalid".

Copy link
Member

Choose a reason for hiding this comment

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

I meant what input triggers the "malformed node or string" error now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Calling literal_eval with an AST node that has non-AST children in it. I don't think it's possible (post-patch) to trigger this error by calling literal_eval with a string, but I can't say for sure, so I don't want to say just "malformed node".

@@ -0,0 +1,2 @@
Improve wording of error messages from ast.literal_eval, distinguishing
valid-but-unacceptable nodes from those that are actually malformed.
Copy link
Member

Choose a reason for hiding this comment

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

Please add "Patch by..."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should that be done by amending the current commit, or adding an additional commit?

Copy link
Member

Choose a reason for hiding this comment

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

It is better for review to add a new commit. In any case all commits are squashed before merging into the repository.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

Copy link
Member

@briancurtin briancurtin left a comment

Choose a reason for hiding this comment

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

Overall this looks good, just one suggestion on the test implementation.

ast.BinOp(ast.Num(1), ast.Add(), 'oops'): "malformed node or string: 'oops'",
}
for test, message in tests.items():
with self.assertRaises(ValueError) as cm:
Copy link
Member

Choose a reason for hiding this comment

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

A helpful thing to use in between lines 594 and 595 would be with self.subTest(test=test, message=message): and then indent the parts below it. That way each of these end up being tested independently rather than potentially having the items loop have a case fail and cause the rest of the cases to fail to execute.

Check out https://docs.python.org/3/library/unittest.html#distinguishing-test-iterations-using-subtests

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@csabella
Copy link
Contributor

@Rosuav, please address the last change request by @briancurtin and also rebase to fix the conflict. Thanks!

@Rosuav
Copy link
Contributor Author

Rosuav commented May 13, 2019

BPO cites another couple of issues, one of which is still open, as dependencies. And it's not really a high priority - it's nothing more than a nice-to-have. There are a lot of barriers to getting patches accepted and it's often not worth the effort unless you REALLY care about the patch.

@merwok
Copy link
Member

merwok commented May 13, 2019

(Note that you can rebase or do a regular merge, which is often easier! All PRs are squash merged in the end @csabella)

@merwok
Copy link
Member

merwok commented May 13, 2019

@serhiy-storchaka Should this wait for bpo-32893 per your last message on the ticket or could it be merged soon? (when conflicts are resolved + suggestion from Brian)

@vstinner
Copy link
Member

A different approach using ast.dump(): PR #16620.

@vstinner
Copy link
Member

What's the status of this PR?

@Rosuav: Are you still working on this?


A different approach using ast.dump(): PR #16620.

Oh, I just closed it since it could produce very long error message: https://bugs.python.org/issue38396#msg354132

@Rosuav
Copy link
Contributor Author

Rosuav commented Dec 14, 2019

No, I'm not. It was a nice-to-have but between the pushback (no core dev seemed to want it to happen) and the constant need to fix merge conflicts meant that I abandoned this proposal ages ago. If someone else wants to champion this fix, then sure, but I don't have the energy to push everything, and this one wasn't worth it.

@vstinner
Copy link
Member

@isidentical: Are you interested to take over this change? I'm not the good candidate to review it. Maybe @pablogsal or @serhiy-storchaka.

@isidentical
Copy link
Sponsor Member

Sure.

@csabella
Copy link
Contributor

Closing this pull request as it is now GH-17662.

@csabella csabella closed this Jan 12, 2020
jaraco pushed a commit that referenced this pull request Dec 2, 2022
Bumps [celery](https://github.com/celery/celery) from 4.4.3 to 4.4.4.
- [Release notes](https://github.com/celery/celery/releases)
- [Changelog](https://github.com/celery/celery/blob/master/Changelog.rst)
- [Commits](celery/celery@4.4.3...v4.4.4)

Signed-off-by: dependabot-preview[bot] <[email protected]>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants