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-38131: Improve messages when generating AST nodes from objects wi… #17715

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

Conversation

isidentical
Copy link
Sponsor 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
@isidentical isidentical force-pushed the bpo-38131 branch 3 times, most recently from aeef32c to 8f98bd3 Compare May 14, 2020
@isidentical
Copy link
Sponsor 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

Parser/asdl_c.py Outdated Show resolved Hide resolved
Parser/asdl_c.py Outdated Show resolved Hide resolved
@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

asottile commented May 17, 2020

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
Sponsor Member Author

isidentical commented May 17, 2020

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

pablogsal commented May 17, 2020

What do you mean by ast types?

I think he is referring to the python classes.

@asottile
Copy link
Contributor

asottile commented May 17, 2020

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
Sponsor 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

Parser/asdl_c.py Outdated Show resolved Hide resolved
@asottile
Copy link
Contributor

asottile 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

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

@isidentical
Copy link
Sponsor Member Author

isidentical commented May 17, 2020

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

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

Copy link
Member

@iritkatriel iritkatriel left a comment

This has merge conflicts now.

@bedevere-bot
Copy link

bedevere-bot commented Dec 7, 2022

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

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.

None yet

9 participants