Skip to content

gh-134733: fix: ast.dump( show_empty=True ) now displays all None values #134743

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

Closed
wants to merge 5 commits into from

Conversation

hunterhogan
Copy link
Contributor

@hunterhogan hunterhogan commented May 26, 2025

Ensure that ast.dump includes all None values when the show_empty flag is set to True.

Also, changed if to elif for consistency.

@sharktide
Copy link
Contributor

Hmmm... It seems as if the ast tests are failing. The tests want res and expect to be the same and it isn't getting what it wants

Copy link
Contributor

@sharktide sharktide left a comment

Choose a reason for hiding this comment

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

@hunterhogan The issue is that the tests haven't been updated to handle the kind="None" attr.

We could either drop that or update the tests.

@hunterhogan
Copy link
Contributor Author

I think I am missing something because the existing tests seem sparse. I made some tests, but I didn't finish integrating them. I don't really know what I am doing. I could use a little help, to be honest.

@hunterhogan hunterhogan marked this pull request as draft May 27, 2025 05:37
@@ -0,0 +1,1477 @@
import ast

check_node(
Copy link
Member

Choose a reason for hiding this comment

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

What is check_node?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From the existing tests.

def test_dump_show_empty(self):
def check_node(node, empty, full, **kwargs):
with self.subTest(show_empty=False):
self.assertEqual(
ast.dump(node, show_empty=False, **kwargs),
empty,
)
with self.subTest(show_empty=True):
self.assertEqual(
ast.dump(node, show_empty=True, **kwargs),
full,
)
def check_text(code, empty, full, **kwargs):
check_node(ast.parse(code), empty, full, **kwargs)
check_node(
ast.arguments(),
empty="arguments()",
full="arguments(posonlyargs=[], args=[], kwonlyargs=[], kw_defaults=[], defaults=[])",
)
check_node(
# Corner case: there are no real `Name` instances with `id=''`:
ast.Name(id='', ctx=ast.Load()),
empty="Name(id='', ctx=Load())",
full="Name(id='', ctx=Load())",
)

@sharktide
Copy link
Contributor

sharktide commented May 27, 2025

I think I am missing something because the existing tests seem sparse. I made some tests, but I didn't finish integrating them. I don't really know what I am doing. I could use a little help, to be honest.

I'll see what I can do. Busy now will get to this

Copy link
Contributor

@sharktide sharktide left a comment

Choose a reason for hiding this comment

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

I'm going file by file.

In the news entry we don't need the entire second sentence.

Copy link
Contributor

@sharktide sharktide left a comment

Choose a reason for hiding this comment

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

@hunterhogan You never defined check_node.

I'm writing one now. Also, could you please add me as a collaborator to your branch so I can try a few things?

@sharktide
Copy link
Contributor

@hunterhogan
I got a working version of check_node, but I think we should implement this the following way:

test_ast/cases.py
<-- add test cases here in a list -->

test_ast/utils.py
<-- add my version of check_node here -->

test_ast/test_ast.py
<-- Call the cases with my check_node in utils -->

Is it okay if I do this? My version of check_node needs a specific format which would require rewriting all your tests

@hunterhogan
Copy link
Contributor Author

I was attempting to integrate with the existing tests.

def test_dump_show_empty(self):
def check_node(node, empty, full, **kwargs):
with self.subTest(show_empty=False):
self.assertEqual(
ast.dump(node, show_empty=False, **kwargs),
empty,
)
with self.subTest(show_empty=True):
self.assertEqual(
ast.dump(node, show_empty=True, **kwargs),
full,
)
def check_text(code, empty, full, **kwargs):
check_node(ast.parse(code), empty, full, **kwargs)
check_node(
ast.arguments(),
empty="arguments()",
full="arguments(posonlyargs=[], args=[], kwonlyargs=[], kw_defaults=[], defaults=[])",
)
check_node(
# Corner case: there are no real `Name` instances with `id=''`:
ast.Name(id='', ctx=ast.Load()),
empty="Name(id='', ctx=Load())",
full="Name(id='', ctx=Load())",
)

I'm in over my head.

@hunterhogan
Copy link
Contributor Author

I am not currently capable of accomplishing this task.

#134718 will probably resolve it, though.

@sharktide
Copy link
Contributor

@hunterhogan We'll see. Thanks for opening this PR and giving it a shot though!

@sharktide
Copy link
Contributor

I'm going to try to revive this

@serhiy-storchaka
Copy link
Member

Please, ensure that tests are passed before creating a PR. I suspect also that the change proposed in this PR has flaws. Tests could expose them.

@sharktide
Copy link
Contributor

I'll do my best

@sharktide
Copy link
Contributor

I'm going to cherry-pick your code into a branch on my fork

sharktide added a commit to sharktide/cpython that referenced this pull request May 28, 2025
@hunterhogan
Copy link
Contributor Author

Please, ensure that tests are passed before creating a PR. I suspect also that the change proposed in this PR has flaws. Tests could expose them.

@serhiy-storchaka The existing tests didn't expose the obvious flaw in ast.dump. And the existing tests are erroneously failing when the function is doing what it is supposed to do.

Are you willing to put some skin in the game? ast.dump may have flaws, but my changes didn't introduce them. US$500? That's ~20% of all of my money.

@sharktide
Copy link
Contributor

@hunterhogan Please do not bet on github.

@hunterhogan
Copy link
Contributor Author

Changes made

image

Baseless response

image

Calling out bad behavior

image

Community

image
image

I'm out

@brettcannon
Copy link
Member

Also please keep things civil.

@hunterhogan
Copy link
Contributor Author

Also please keep things civil.

Thank you for quickly responding with false equivalence. It will help me have accurate expectations if I try to contribute again.

@sharktide
Copy link
Contributor

@hunterhogan No offense, but seems like a bit of an overreaction. I am taking the time to write this because no one is denying that you are a bad programmer, but it is a bit 'uncivil' to get mad at people and bet money over PRs and Issues.

Nothing personal, but please, there was nothing wrong with @serhiy-storchaka's comment IMO, so don't overreact if it just questioned this issue/PR. It is just called triaging and there was nothing against you.

@brettcannon
Copy link
Member

Based on the consistent tone in this PR I'm going to lock it down.

@python python locked as too heated and limited conversation to collaborators May 31, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants