False-positive [inconsistent-overload] with non-constant parameter mapping
- Dominant language
- Rust
- Stars
- 7k
- Forks
- 516
- PR merge metrics
- No merged PRs in 30d
Description
### Describe the Bug
```python
from typing import overload
@overload
def f(string: str, /) -> None: ...
@overload
def f(lower: int, upper: int) -> None: ...
def f(
lower_or_string: str | int | None = None,
/,
lower: int | None = None,
upper: int | None = None,
) -> None: ...
```
The implementation of `f` is compatible with all legal call signatures of the overloads:
- `f(str)`
- `f(int, int)`
- `f(int, upper=int)`
- `f(lower=int, upper=int)`
yet pyrefly errors (xref: https://github.com/python/typing/issues/2224)
```
ERROR sandbox.py:6:5-6: Implementation signature `(lower_or_string: int | str | None = None, /, lower: int | None = None, upper: int | None = None) -> None`does not accept all arguments that overload signature `(lower: int, upper: int) -> None` accepts [inconsistent-overload]
```
Full MWE with actual implementation:
```python
from typing import overload
@overload
def f(string: str, /) -> None: ...
@overload
def f(lower: int, upper: int) -> None: ...
def f(
lower_or_string: str | int | None = None,
/,
lower: int | None = None,
upper: int | None = None,
) -> None:
if lower_or_string is None:
assert isinstance(lower, int), "illegal call"
assert isinstance(upper, int), "illegal call"
print(f"({lower=}, {upper=})")
elif isinstance(lower_or_string, int):
if lower is None:
assert isinstance(upper, int), "illegal call"
lower = lower_or_string
else:
assert upper is None, "illegal call"
upper = lower
lower = lower_or_string
print(f"({lower=}, {upper=})")
else:
assert isinstance(lower_or_string, str), "illegal call"
assert lower is None, "illegal call"
assert upper is None, "illegal call"
string = lower_or_string
print(f"({string=})")
f("a")
f(1, 2)
f(1, upper=2)
f(lower=1, upper=2)
```
### Sandbox Link
https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN24AbjEpRcqTAB100gAJCRYidMwwwdMAAo4DSqzaI6OygBo6AegCUdALQA%2BOgDlc6GIcIe5C0eKlY1GppiAO4ihqwMZgCuxMRhnOgM1vZOLm50HoQqAVrSdPl0ISIA%2BrzFxvqGxnQAPgn8dc6udAC8qa4meQXmnegFhbihlOGJte0wreO9-TFxw-VjTRNtS73JDkvunjLoICYgZJRqUKSEDLRQFADEdAAKpEdgJ0YYOAR0AMYukGxRlKgMCAuLLoG4AZRgEwAFgwGMQ4IhzOZDsdTrw2OYYOhzJhcB84OYvugfn8AUDsRpeHRUIJUNBUNhYJ9vhBfv9AS4BMQOeg4CCyAwoS4bMJKHByZNJCAAMyEACMACYpdIANoiahigC60ii6C4PD4MEwNkwECOH0BwkmAHJWeheDBrdJ7QwbEcAI5RM1GmwAaxgpBsqA%2BH3gcBtwVQlHQTt2AF99sHLTAAGLQGAUNBYPBEMggONAA
### (Only applicable for extension issues) IDE Information
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.