Skip to content

Python: Improve modelling of overloaded methods#21419

Draft
tausbn wants to merge 3 commits intomainfrom
tausbn/python-improve-overloaded-method-resolution
Draft

Python: Improve modelling of overloaded methods#21419
tausbn wants to merge 3 commits intomainfrom
tausbn/python-improve-overloaded-method-resolution

Conversation

@tausbn
Copy link
Contributor

@tausbn tausbn commented Mar 5, 2026

The @typing.overload decorator allows the user to specify precise types for polymorphic functions. Unfortunately, our call graph does not account for this, which means methods such as findFunctionAccordingToMro will 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

import typing

class OverloadedInit:
    @typing.overload
    def __init__(self, x: int) -> None: ...

    @typing.overload
    def __init__(self, x: str, y: str) -> None: ...

    def __init__(self, x, y=None):
        pass

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, y has a default value).

To get around this, we add a new predicate hasOverloadDecorator which 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.

@tausbn tausbn closed this Mar 5, 2026
@tausbn tausbn reopened this Mar 5, 2026
@github-actions github-actions bot added the Python label Mar 5, 2026
tausbn added 3 commits March 5, 2026 22:20
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.
@tausbn tausbn force-pushed the tausbn/python-improve-overloaded-method-resolution branch from 05d99b0 to 0561a63 Compare March 5, 2026 22:26
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.

1 participant