facebook / facebook/pyrefly

Multiple overloads marked as "[closest match]" in [no-matching-overload] error

Open
#4,238 3 comments 0 reactions 1 assignee Claimed by @xaskii View on GitHub
stale typechecking
Dominant language
Rust
Stars
7k
Forks
516
PR merge metrics
No merged PRs in 30d

Description

### Describe the Bug

See:
```
import re

x: re.Pattern[str] = re.compile("")

x.match(b"oops") # error!
```

The error message is:
```
ERROR sandbox.py:5:8-17: No matching overload found for function `re.Pattern.match` called with arguments: (Literal[b'oops']) [[no-matching-overload](https://pyrefly.org/en/docs/error-kinds/#no-matching-overload)]
Possible overloads:
(string: str, ...) -> Match[str] | None [closest match]
(string: Buffer, ...) -> Match[bytes] | None
(string: str, ...) -> Match[str] | None [closest match]
Argument `Literal[b'oops']` is not assignable to parameter `string` with type `str` in function `re.Pattern.match`
```

The type signature for `re.Pattern.match` is:
```
@overload
def match(self: Pattern[str], string: str, pos: int = 0, endpos: int = sys.maxsize) -> Match[str] | None:
"""Matches zero or more characters at the beginning of the string."""
...
@overload
def match(self: Pattern[bytes], string: ReadableBuffer, pos: int = 0, endpos: int = sys.maxsize) -> Match[bytes] | None:
"""Matches zero or more characters at the beginning of the string."""
...
@overload
def match(self, string: AnyStr, pos: int = 0, endpos: int = sys.maxsize) -> Match[AnyStr] | None:
"""Matches zero or more characters at the beginning of the string."""
...
```

It looks like what's happening is that when `AnyStr=str`, the first and third overloads look identical to Pyrefly, so both end up with the "closest match" label if either of them is the closest match.

### Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIEAtsbgE4AuABHTADrof6IsyEAKqBgxh10AbTgM6AXSYBeXoQDGuGtBgAKNiB0BKDl0JUhygBabsO3LmJx9IADQgyrMFFKEGaqBQDETPykbh5MaFh4%2BEyq6JAA5gCudEIQuOiEHAEAyjAwTGbCdogA9MWuMO6e9HHFMOjFmLjKcMUx8UkpacVMYPRMqABuqNCo2LDRae3JDKnoTLYzaXAZ6GQMZmkAtAOicLMKTDoAzIQAjABMOhzionT0cDIcCejUtIwwmJuYEKzKMzsHADkEDi6HoMEBHDBDE2rAAjgkfh9NgBrGCkTaoZTKeBwIEAd1QYkh6Cc4BsJHIiHAdyoEygsD%2Bs2Wo2UTFe9GYAFlUMRiBB0HE2HQOGBaUwGKR%2BYL2TROUwACpSmAAQSgEFQ9hFnG1yigmrxACEeIRTcLDNqADIwVBgHhK4iq9Wag48vkCuLiAUMRzs9AMOQAHyYbulnqkdF96qk4kNMke2pAAF9nFj-jAAGIaCjhHAESnJoA

### (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.