Python: Improve modelling of overloaded methods#21419
Draft
Conversation
python/ql/test/library-tests/dataflow/calls-overload/OverloadCallTest.ql
Fixed
Show fixed
Hide fixed
Adds a test showing that `@typing.overload` stubs are spuriously resolved as call targets alongside the actual `__init__` implementation.
Adds `hasOverloadDecorator` as a predicate on functions. It looks for decorators called `overload` or `something.overload` (usually `typing.overload` or `t.overload`). These are then filtered out in the predicates that (approximate) resolving methods according to the MRO. As the test introduced in the previous commit shows, this removes the spurious resolutions we had before.
05d99b0 to
0561a63
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
@typing.overloaddecorator allows the user to specify precise types for polymorphic functions. Unfortunately, our call graph does not account for this, which means methods such asfindFunctionAccordingToMrowill happily return all of the resolved methods with the specified name -- even the ones that don't do anything.This is a problem for queries that check that the correct arguments are being passed. Given
it looks like the first
__init__expects a single argument, and the second expects two, when in fact it can accept either (because in the method that actually gets called,yhas a default value).To get around this, we add a new predicate
hasOverloadDecoratorwhich checks (syntactically, because it's used in the call graph computation) whether a function is likely to be overloaded. We then use that predicate to filter out resolved methods where the target is a typing overload.To test this, I added a new test separate from the usual call graph tests, primarily because we need to know not only that some call is resolved properly, but also what target it is resolved to.