facebook / facebook/pyrefly

Overload resolution doesn't do "expected type" literal narrowing through a comprehension

Open
#4,167 3 comments 1 reaction 1 assignee Claimed by @markselby9 View on GitHub
narrowing quansight stale typechecking
Dominant language
Rust
Stars
7k
Forks
516
PR merge metrics
No merged PRs in 30d

Description

### Describe the Bug

```py
from __future__ import annotations

from collections.abc import Sequence
from typing import Literal, overload

Order = Literal["ascending", "descending"]

@overload
def sort_indices(sort_keys: Sequence[tuple[str, Order]]) -> str: ...
@overload
def sort_indices(sort_keys: str) -> int: ...
def sort_indices(sort_keys: Sequence[tuple[str, Order]] | str) -> str | int:
return 0

names = ["a", "b"]
result = sort_indices([(name, "ascending") for name in names])
```

I think `sort_indices` should be able to accept `[(name, "ascending") for name in names]`, as it's a `Sequence` of `tuple[str, Order]`?

```console
(scratch) mgorelli@marcoslaptop:~/scratch$ mypy t.py
Success: no issues found in 1 source file
(scratch) mgorelli@marcoslaptop:~/scratch$ pyright t.py
0 errors, 0 warnings, 0 informations
(scratch) mgorelli@marcoslaptop:~/scratch$ ty check t.py
All checks passed!
(scratch) mgorelli@marcoslaptop:~/scratch$ pyrefly check t.py
ERROR No matching overload found for function `sort_indices` called with arguments: (list[tuple[str, str]]) [no-matching-overload]
--> t.py:18:22
|
18 | result = sort_indices([(name, "ascending") for name in names])
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
Possible overloads:
(sort_keys: Sequence[tuple[str, Order]]) -> str [closest match]
(sort_keys: str) -> int
Argument `list[tuple[str, str]]` is not assignable to parameter `sort_keys` with type `Sequence[tuple[str, Order]]` in function `sort_indices`
INFO 1 error
```

Spotted in [Narwhals](https://github.com/narwhals-dev/narwhals). Got a bit of help from Claude for narrowing down a dependency-free reproducer

### Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEA%2Bg2AK4AuLlMTdENxuSmzoZ0uNqjYRc6OAB10CsNXoBjXFFiqpMuIVTZVvfoOEBlGAEcWMdKphKVdNqWIR0Ac2MChdADIQbDCUqFAANHS4AG7BULiomAoKAPKUmMF0ALz%2BgcGhANpyIKhw9ljuHkURRemltpgVRQC6SYroAALRsfGJWDBgdHCmDO4N9nAAFENCDADWMKRwiHQW1rb2%2BRzEsPlwbJQRqemUTU0AlHQAtAB8g-vLhI8KnTGUcQkK6QPTbCPl41NhvNFss9pQLjdeOg2A8nn1vsNRhAAT85gslisrDY7DBNixtriwYc0sFTnQAD53cFXW5gilQmEKOjMuhcDiUdB0AAMrQU6FQNHgWTohWKVToRWwzQUXDgLCgwmyqKRAPyE35guqxTq5U8RQuYEEdA1MChxoF8HOChAYRAZC4YCgpEIbFoUAoAGI6AAFUgOp2DDA4Ah0dToSAeTiSaToQgKL0WU0ACzYbGISwA9Bn7f0nYRBB4M7YM5hcKo4BmwxGozp0Bm6IbKCIoqhoAZYKGZNWQrXIsRa3oFGQ2EmZJdXnAY8KigBmQgARgATEUFPlgtRKHAWugWOg%2BD4gphLg0uNoIDFhQByCAeMRcS988SXLjWCBcI-Ay6oVTjOBXgDuqAcg%2B6AgAAvra35SDEABi0AwBQaBYHgRBkOBQA

### (Only applicable for extension issues) IDE Information

_No response_

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.