Skip to content

Nested call to generic overloaded function solves type variable too early? #1270

@rchen152

Description

@rchen152

Describe the Bug

Context: I minimized this from a false positive on this line in pandas-stubs: https://github.com/pandas-dev/pandas-stubs/blob/1cdecd90fa1bd992dd3392bdd0f27d192eec21da/tests/series/test_series.py#L170.

In this code snippet, the call to the overloaded read_csv function should resolve to the second overload, which returns Kitten. But when we call the first overload to check if it matches, T in check seems to get fixed to Puppy:

from typing import Literal, overload

def check[T](actual: T) -> T: ...

class Kitten: ...
class Puppy: ...

@overload
def read_csv[S](
    filepath_or_buffer: str,
    *,
    iterator: Literal[True],
) -> Puppy: ...
@overload
def read_csv(
    filepath_or_buffer: str,
    *,
    iterator: Literal[False],
) -> Kitten: ...
def read_csv(*args, **kwargs) -> Puppy | Kitten: ...

def test_types_csv(path: str) -> None:
    check(read_csv(path, iterator=False))  # false positive: Argument `Kitten` is not assignable to parameter `actual` with type `Puppy` in function `check`

Note that the problem goes away if I remove [S] from the first overload and make it non-generic.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0AZCAxiVUUADR1cAN1FRcqTAB10qzDDB0AxgAsY2gNYBtACoBdABSptDAK7jEdUwEo6AWgB8zp4T%2BrVbShUODg6AGlhEXRff3QgkLCABTtiMljCAPQAAVl5RRUsTTpKGCUAfW04GWMAZStVOia6SFhiVAZdct5y7DswMFEnOAZKCUbmgCpx9GbOETEGXichBfEzSjsYcxm3LzoUtNIM1Vy5SgUldWLSiqqZSwmm1ph2zu7KXv7BymHRmbm0ye81EHWWgmEoKgxgAYuI4Ntdh5vJEGNETkUtLdMJVqpZJqhKGw4FJJpNDAB3QnEvbeQ5kOgAHwiURgMTofkyakxjHgDHKTGI8FxDzeuj%2BlFpdAAcrh0DBEMC9AZDJZsSLLGKpJDFrwALxwqAIlwuVQgCQgMilMBQUiEJY0KAUADEB1I1ttdDQWDw%2BB0csgbDsiwgcq5rtqMBgdF0aOIcEQAHpE1bNLbCLw2Im2YnMLgqontAGIEGQ3LEy1eHRUDJUNBUNhYP70IHgx1Q7NcMQGB24FyyJ05e5znAO3Q9XRlCAAMyEACMACYp2aAL4Wmw9uQw6AwCjenAEEjkFdAA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions