facebook / facebook/pyrefly

generic inference fails in some cases with function argument type like `Foo[T, object] | Foo[object, T]`

Open
#3,583 5 comments 1 reaction 1 assignee Claimed by @ak4-sh View on GitHub
typechecking
Dominant language
Rust
Stars
7k
Forks
516
PR merge metrics
No merged PRs in 30d

Description

### Describe the Bug

```python
class Foo[T, U]:
def t(self) -> T: ...
def u(self) -> U: ...

def fn[T](foo: Foo[T, object] | Foo[object, T], value: list[T]) -> T: ...

def fn2[T](value: list[T]) -> T:
return fn(Foo[T, object](), value)
```

```
ERROR sandbox.py:10:12-39: Returned type `object` is not assignable to declared return type `T` [bad-return]
ERROR sandbox.py:10:33-38: Argument `set[T]` is not assignable to parameter `value` with type `set[object]` in function `fn` [bad-argument-type]
```

Python: 3.13

- this works correctly in [pyright](https://basedpyright.com/?typeCheckingMode=all&reportUnusedParameter=false&reportUnusedVariable=false&code=MYGwhgzhAEBiD28DaAVANNAqgXQFwFgAoaE6AEwFMAzaAFwAoIKQqBKaAWgD5oVdoAdEKKly1aAFdGzNpx6Z%2BQgURWFKNKgDtU2elUT8EydNHgAjAFYVgtbNAA%2BcREnNWbGFNgwA3MCAkU-Ey0OuzcvIrChKrq0FoATDr0vv6B0MGhchEipABOFLQSuZpxmvRGqBiu1rb0rD5%2BAaxAA).
- [ty has the same issue](https://play.ty.dev/a101db20-52d9-4aec-96c3-c6c9a3cd518e) (see https://github.com/astral-sh/ty/issues/3533) but unlike ty, pyrefly works correctly when the second argument type is just `T` instead of `list[T]`

### Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIAxlKnHAAQBiuuA2gCoA09AqgLqIAOunqj6mGGHoAXABRwYUMAEp6AWgB89donqF9wseMn0ArvMUr1Wnrv2FhjrCbDoOfWWBa7mbLvVxsACsYSmk%2BegAfJhZWQJCw7nY%2BbgA3VChTGF0oCDhpd1VNbTsDdCcJKVcAJndZdMzs%2Blz8wusSwzEAJxhpUy6RV1lfDm540PDZZTSMrOUQThAyHrAoUkJpXABbKAoyaQALXHQ1VJguuAhj%2BgBeekEQAGZCAEZqh%2BFWc67cC75hDDoXDSVDSK4nLZ5LagygHW70ADk31%2BCOEIAAvotUGEIGdGNAYBQ0Fg8EQyBigA

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