Skip to content

Conversation

@brianschubert
Copy link
Contributor

@brianschubert brianschubert commented Aug 9, 2025

@brianschubert
Copy link
Contributor Author

@picnixz As mentioned in #121162 (comment), now that the constructors raise on missing and unknown fields, some of the checks that were added for copy.replace support are no longer needed, so I went ahead and removed them (de5564a). Based on the tests, it looks like things are working correctly, but I'd appreciate if you could take a look to make sure I didn't break anything :-)

@brianschubert brianschubert linked an issue Aug 9, 2025 that may be closed by this pull request
@picnixz
Copy link
Member

picnixz commented Aug 10, 2025

This takes me back as the original PR is one of my very first contributions to CPython!

@picnixz picnixz self-requested a review August 10, 2025 06:55
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

I'd suggest using msg = ... + re.escape() for assertRaisesRegex() where 80 chars limit is exceeded.

brianschubert and others added 2 commits August 18, 2025 17:20
@JelleZijlstra
Copy link
Member

@picnixz do you have any further feedback?

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

I do not but I actually had a pending review that I started 1 month ago. They are mainly for improving coverage and wording but they are optional.

self.assertEqual(x.right, n3)

# Random attribute allowed too
x.foobarbaz = 5
Copy link
Member

Choose a reason for hiding this comment

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

I don't know if we already cover this, but maybe we should instead:

  • Find a way to construct all nodes "correctly" or have at least one instance of each nodes.
  • For all these instances, check that we can set random attributes and delete them properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, added a test in the style of the existing test_field_attr_existence test.

Comment on lines 1 to 5
The constructors of node types in the :mod:`ast` module now raise a
:exc:`TypeError` when a required argument is omitted or when a
keyword argument that does not map to a field on the AST node is passed.
These cases had previously raised a :exc:`DeprecationWarning` since Python
3.13. Patch by Brian Schubert.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
The constructors of node types in the :mod:`ast` module now raise a
:exc:`TypeError` when a required argument is omitted or when a
keyword argument that does not map to a field on the AST node is passed.
These cases had previously raised a :exc:`DeprecationWarning` since Python
3.13. Patch by Brian Schubert.
:mod:`ast`: The constructors of AST nodes now raise a
:exc:`TypeError` when a required argument is omitted or when a
keyword argument that does not map to a field on the AST node is passed.
These cases had previously raised a :exc:`DeprecationWarning` since Python
3.13. Patch by Brian Schubert.

And you can reflow the paragraph as you deem fit (and also you can reuse that message in the NEWS).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, thanks!

@brianschubert
Copy link
Contributor Author

@JelleZijlstra friendly ping! Anything else needed here?

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Thanks, left some suggestions for the tests.

# Custom attribute assignment is allowed
x.foo = 5
self.assertEqual(x.foo, 5)
del x.foo
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed? Doesn't the object get deleted anyway?


with self.assertRaises(AttributeError):
x.value
msg = "ast.Constant.__init__ missing 1 required positional argument: 'value'"
Copy link
Member

Choose a reason for hiding this comment

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

This test now feels out of scope for the test function. If we're already sufficiently testing that this case errors out (I assume we are), let's just remove this line.

# Arbitrary keyword arguments are supported (but deprecated)
with self.assertWarns(DeprecationWarning):
self.assertEqual(ast.Constant(1, foo='bar').foo, 'bar')
# Arbitrary keyword arguments are not supported
Copy link
Member

Choose a reason for hiding this comment

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

Same here, this is now testing something unrelated to what this function is about.

Copy link
Member

Choose a reason for hiding this comment

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

My intent here was to be sure we were ignoring kwargs.

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.

Promote ast node constructor deprecation warnings to errors

4 participants