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

Python: Fix bug in handling of **kwargs in class bases #17809

Merged
merged 2 commits into from
Oct 23, 2024

Conversation

tausbn
Copy link
Contributor

@tausbn tausbn commented Oct 18, 2024

This caused a dataset check error on the python/cpython database, as we had a DictUnpacking node whose parent was not a dict_item_list, but rather an expr_list.

Investigating a bit further revealed that this was because in a construction like

class C[T](base, foo=bar, **kwargs): ...

we were mistakenly adding **kwargs to the same list as base (which is just a list of expressions), rather than the same list as foo=bar (which is a list of dictionary items)

The ultimate cause of this was the use of ! name in python.tsg to distinguish between bases and keyword arguments (only the latter of which have the name field). Because dictionary_splat doesn't have a name field either, these were mistakenly put in the wrong list, leading to the error.

Also, because our previous test of class statements did not include a **kwargs construction, we were not checking that the new parser behaved correctly in this case. For the most part this was not a problem, but on files that use syntax not supported by the old parser (like type parameters on classes), this became an issue. This is also
why we did not see this error previously.

To fix this, we added ! value (which is a field present on dictionary_splat nodes) as a secondary filter, and added a third stanza to handle dictionary_splat nodes.

Pull Request checklist

All query authors

Internal query authors only

  • Autofixes generated based on these changes are valid, only needed if this PR makes significant changes to .ql, .qll, or .qhelp files. See the documentation (internal access required).
  • Changes are validated at scale (internal access required).
  • Adding a new query? Consider also adding the query to autofix.

This caused a dataset check error on the `python/cpython` database, as
we had a `DictUnpacking` node whose parent was not a `dict_item_list`,
but rather an `expr_list`.

Investigating a bit further revealed that this was because in a
construction like

```python
class C[T](base, foo=bar, **kwargs): ...
```
we were mistakenly adding `**kwargs` to the same list as `base` (which
is just a list of expressions), rather than the same list as `foo=bar`
(which is a list of dictionary items)

The ultimate cause of this was the use of `! name` in `python.tsg` to
distinguish between bases and keyword arguments (only the latter of
which have the `name` field). Because `dictionary_splat` doesn't have a
`name` field either, these were mistakenly put in the wrong list,
leading to the error.

Also, because our previous test of `class` statements did not include a
`**kwargs` construction, we were not checking that the new parser
behaved correctly in this case. For the most part this was not a
problem, but on files that use syntax not supported by the old parser
(like type parameters on classes), this became an issue. This is also
why we did not see this error previously.

To fix this, we added `! value` (which is a field present on
`dictionary_splat` nodes) as a secondary filter, and added a third
stanza to handle `dictionary_splat` nodes.
@tausbn tausbn force-pushed the tausbn/python-fix-kwargs-in-class-bases branch from c8635c2 to 9803bbd Compare October 21, 2024 15:35
@tausbn tausbn added the no-change-note-required This PR does not need a change note label Oct 21, 2024
@tausbn tausbn marked this pull request as ready for review October 21, 2024 15:55
@tausbn tausbn requested a review from a team as a code owner October 21, 2024 15:55
Copy link
Contributor

@yoff yoff left a comment

Choose a reason for hiding this comment

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

LGTM

@tausbn
Copy link
Contributor Author

tausbn commented Oct 23, 2024

New test run validates that the number of dataset check errors goes down on cpython and django. 💪

@tausbn tausbn merged commit 24ae548 into main Oct 23, 2024
10 checks passed
@tausbn tausbn deleted the tausbn/python-fix-kwargs-in-class-bases branch October 23, 2024 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-change-note-required This PR does not need a change note Python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants