Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-38131: Improve messages when generating AST nodes from objects wi… #17715

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

isidentical
Copy link
Member

@isidentical isidentical commented Dec 27, 2019

@codecov
Copy link

codecov bot commented Feb 14, 2020

Codecov Report

Merging #17715 into master will increase coverage by 1.08%.
The diff coverage is n/a.

Impacted file tree graph

@@             Coverage Diff             @@
##           master   #17715       +/-   ##
===========================================
+ Coverage   82.11%   83.20%    +1.08%     
===========================================
  Files        1956     1571      -385     
  Lines      589319   414699   -174620     
  Branches    44450    44450               
===========================================
- Hits       483924   345046   -138878     
+ Misses      95746    60009    -35737     
+ Partials     9649     9644        -5     
Impacted Files Coverage Δ
Lib/distutils/tests/test_bdist_rpm.py 30.00% <0.00%> (-65.00%) ⬇️
Lib/distutils/command/bdist_rpm.py 7.63% <0.00%> (-56.88%) ⬇️
Lib/test/test_urllib2net.py 76.92% <0.00%> (-13.85%) ⬇️
Lib/test/test_smtpnet.py 78.57% <0.00%> (-7.15%) ⬇️
Lib/ftplib.py 63.85% <0.00%> (-6.06%) ⬇️
Lib/test/test_ftplib.py 87.11% <0.00%> (-4.72%) ⬇️
Tools/scripts/db2pickle.py 17.82% <0.00%> (-3.97%) ⬇️
Tools/scripts/pickle2db.py 16.98% <0.00%> (-3.78%) ⬇️
Lib/test/test_socket.py 71.94% <0.00%> (-3.77%) ⬇️
Lib/test/test_asyncio/test_base_events.py 91.84% <0.00%> (-3.30%) ⬇️
... and 429 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9aeb0ef...9feb855. Read the comment docs.

@isidentical isidentical force-pushed the bpo-38131 branch 6 times, most recently from 3117e45 to a27af44 Compare May 7, 2020 15:13
@isidentical isidentical force-pushed the bpo-38131 branch 3 times, most recently from aeef32c to 8f98bd3 Compare May 14, 2020 23:58
@isidentical
Copy link
Member Author

isidentical commented May 16, 2020

Benchmarks (asked by @pablogsal) (not precise because lack of isolated cpus) (debug build / no pgo or lto):

 $ ./python -m pyperf timeit -s "import ast; f = open('Lib/ast.py'); c = f.read(); f.close(); t = ast.parse(c)" "compile(t, '<test>', 'exec')"
master: Mean +- std dev: 15.0 ms +- 0.4 ms
PR    : Mean +- std dev: 15.5 ms +- 0.5 ms
 $ ./python -m pyperf timeit -s "import ast; f = open('Lib/test/test_socket.py'); c = f.read(); f.close(); t = ast.parse(c)" "compile(t, '<test>', 'exec')"
master: Mean +- std dev: 75.4 ms +- 4.0 ms
PR    : Mean +- std dev: 77.2 ms +- 7.4 ms

@pablogsal
Copy link
Member

pablogsal commented May 16, 2020

Hey @asottile, could you give us a hand here? What is your oppinion on the error messages? Do you find odd/weird that they mention ASDL names? Do you think they may be a bit misleading for users that are not familiar with the ASDL definition? For instance:

field \"%s\" was expecting excepthandler type node, got ...

Do you have any suggestions on how to improve them?

@asottile
Copy link
Contributor

agree with the expecting node of type %s as an improvement on the english standpoint

as for the actual messaging, this will only be exposed to those which are constructing ast manually right (for instance, pytest for assertion rewriting / werkzeug for "JIT")? as long as these aren't exposed to most users I think the quality of error message that's here is fine (though would be better if we could talk about ast types here but understandable that they're not available at this level).

at least for the expr ones I think it'll be fine (ast.expr is a thing after all)

@isidentical
Copy link
Member Author

Thanks for help Paul and Anthony

(though would be better if we could talk about ast types here but understandable that they're not available at this level).

What do you mean by ast types? Aren't all of these are real AST types (like expr, or expr_context etc.)

@pablogsal
Copy link
Member

What do you mean by ast types?

I think he is referring to the python classes.

@asottile
Copy link
Contributor

What do you mean by ast types?

I think he is referring to the python classes.

yeah I mean the classes in the ast module, sorry I should have been more specific :)

@isidentical
Copy link
Member Author

isidentical commented May 17, 2020

yeah I mean the classes in the ast module, sorry I should have been more specific :)

@asottile do you have any kind of error message in your mind (a specific one, let's say for this case)

>>> import ast
>>> t = ast.parse("node", mode="eval")
>>> t.body = [t.body]
>>> compile(t, "<test>", "eval")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: required field "lineno" missing from expr

Maybe we could improve it

(update) the current version:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: field "body" was expecting expr type node, got list

@asottile
Copy link
Contributor

yeah I mean the classes in the ast module, sorry I should have been more specific :)

@asottile do you have any kind of error message in your mind (a specific one, let's say for this case)

>>> import ast
>>> t = ast.parse("node", mode="eval")
>>> t.body = [t.body]
>>> compile(t, "<test>", "eval")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: required field "lineno" missing from expr

Maybe we could improve it

(update) the current version:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: field "body" was expecting expr type node, got list

yeah the new code is definitely a huge improvement either way 👍

@isidentical
Copy link
Member Author

After applying suggestions, current state of the PR:

>>> import ast
>>> t = ast.parse("x", mode="eval")
>>> t.body = [t.body]
>>> compile(t, "<test>", "eval")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: field 'body' was expecting node of type 'expr', got 'list'
>>> t = ast.parse("'str'", mode="eval")
>>> t.body.kind = 0xFF
>>> compile(t, "<test>", "eval")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: field 'kind' was expecting a string or bytes object

Copy link
Contributor

@MaxwellDupre MaxwellDupre left a comment

Choose a reason for hiding this comment

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

Run of test_ast produced:
Ran 123 tests in 8.888s
OK
Looks ok.

Copy link
Member

@iritkatriel iritkatriel left a comment

Choose a reason for hiding this comment

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

This has merge conflicts now.

@bedevere-bot
Copy link

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@ghost
Copy link

ghost commented Dec 13, 2023

The following commit authors need to sign the Contributor License Agreement:

Click the button to sign:
CLA not signed

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.

LGTM, but I'll wait until #113035 is merged to make backporting easier.

@serhiy-storchaka serhiy-storchaka dismissed iritkatriel’s stale review December 25, 2023 18:07

Merge conflicts were resolved.

@serhiy-storchaka
Copy link
Member

@isidentical, it seems you need to sign the CLA again. It is now tied to the email address.

@serhiy-storchaka serhiy-storchaka enabled auto-merge (squash) December 25, 2023 18:09
@encukou
Copy link
Member

encukou commented Sep 28, 2024

This now has a nontrivial conflict.
@isidentical, do you want to resolve it, or should I look into it?

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.