-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Python: Modernize special method query #18956
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: Modernize special method query #18956
Conversation
5b1e08a to
f63c7cf
Compare
Moves a bunch of `owner.declaredAttribute(name) = f` instances to the top level, in the process greatly cleaning up the code. The behaviour should be the unchanged. Having done this, there's only one place where we depend on points-to, and that's in the remaining `declaredAttribute` call. This should greatly simplify the move away from points to.
Adds a new boolean parameter `is_unused_default` that indicates whether the given result is one where a parameter to a special method has a default value (which will never be used when invoked in the normal way). These results are somewhat less useful (because the special method _might_ be invoked directly, in which case the default value would still be relevant), but it seemed like a shame to simply remove the code, so instead I opted to disable it in this way.
Instances of this include - Bodies that contain just a docstring (common in Zope interfaces) - Bodies that do nothing but raise an exception.
f63c7cf to
f3353dc
Compare
Technically we still depend on points-to in that we still mention `PythonFunctionValue` and `ClassValue` in the query. However, we immediately move to working with the corresponding `Function` and `Class` AST nodes, and so we're not really using points-to. (The reason for doing things this way is that otherwise the `.toString()` for all of the alerts would change, which would make the diff hard to interpret. This way, it should be fairly simple to see which changes are actually relevant.) We do lose some precision when moving away from points-to, and this is reflected in the changes in the `.expected` file. In particular we no longer do complicated tracking of values, but rather look at the syntactic structure of the classes in question. This causes us to lose out on some results where a special method is defined elsewhere, and causes a single FP where a special method initially has the wrong signature, but is subsequently overwritten with a function with the correct signature. We also lose out on results having to do with default values, as these are now disabled. Finally, it was necessary to add special handling of methods marked with the `staticmethod` decorator, as these expect to receive fewer arguments. This was motivated by a MRVA run, where e.g. sympy showed a lot of examples along the lines of ``` @staticmethod def __abs__(): return ... ```
Also adds `quality` to the list of tags for the query.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR modernizes the special method query by refactoring it to remove reliance on the points-to API and reducing false positives.
- Updated test cases in om_test.py to return concrete values instead of placeholder pass statements.
- Added a new test class with a static abs method.
- Updated the change notes to document the query modernization and adjustments to default parameter handling.
Reviewed Changes
Copilot reviewed 2 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| python/ql/test/query-tests/Functions/general/om_test.py | Updated test cases for special methods to return concrete values, aligning with the modernized query behavior. |
| python/ql/src/change-notes/2025-03-20-modernize-special-method-wrong-signature-query.md | Documented the refactoring and reduced false positives in the query. |
Files not reviewed (2)
- python/ql/src/Functions/SignatureSpecialMethods.ql: Language not supported
- python/ql/test/query-tests/Functions/general/SignatureSpecialMethods.expected: Language not supported
Tip: Copilot only keeps its highest confidence comments to reduce noise and keep you focused. Learn more
joefarebrother
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good 👍
Not sure about keeping around the unused_default handling in a disabled form; personally I would remove it but that's up to you.
Yeah, I'm still not entirely sure. I think I'll keep it around for now. Removing it later on should be trivial. |
(Best reviewed commit-by-commit.)
Refactors the
py/special-method-wrong-signaturequery to no longer rely on the points-to API.Additionally, a few changes were made to reduce the number of false positives: