facebook / facebook/pyrefly

nonsensical error with valid overload

Open
#1,150 6 comments 1 reaction 0 assignees View on GitHub
needs-discussion overloads typechecking
Dominant language
Rust
Stars
7k
Forks
516
PR merge metrics
No merged PRs in 30d

Description

### Describe the Bug

```py
from typing import overload

@overload
def foo(i: str) -> object: ... # Overload return type `object` is not assignable to implementation return type `int`
@overload
def foo(i: int) -> object: ... # Overload return type `object` is not assignable to implementation return type `int`

def foo(i: int | str) -> int:
return 1
```

`object` is not assignable to `int` 🤣

this check is the wrong way around!

```py
from typing import overload

@overload
def foo(i: str) -> int: ... # no error
@overload
def foo(i: int) -> str: ... # no error

def foo(i: int | str) -> object:
return None
```

I think a better solution would be:

- The impl return type must be assignable to each signature, allowing for union distribution (i.e. impl: `int | str` is assignable to `int` if considering each union member)
- the impl return type must be a subtype of the union of overload return types

this will prevent too-wide and too-narrow types

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.