Files
codeql/python/ql/test/library-tests/dataflow/calls-overload/test.py
Taus fa61f6f3df Python: Model @typing.overload in method resolution
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.
2026-03-05 22:20:03 +00:00

40 lines
846 B
Python

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
OverloadedInit(1) # $ init=OverloadedInit.__init__:11
OverloadedInit("a", "b") # $ init=OverloadedInit.__init__:11
from typing import overload
class OverloadedInitFromImport:
@overload
def __init__(self, x: int) -> None: ...
@overload
def __init__(self, x: str, y: str) -> None: ...
def __init__(self, x, y=None):
pass
OverloadedInitFromImport(1) # $ init=OverloadedInitFromImport.__init__:28
OverloadedInitFromImport("a", "b") # $ init=OverloadedInitFromImport.__init__:28
class NoOverloads:
def __init__(self, x):
pass
NoOverloads(1) # $ init=NoOverloads.__init__:36