Skip to content

functools.partial can't infer the return type of generic type fucntion #15053

@monchin

Description

@monchin

Hello, I found that mypy can't infer to right return type when there is a generic type function in partial. Below is the code:

from functools import partial
from typing import List

def test(val:float, step:float, range_:List[float]) -> float:
    assert step != 0
    op = partial(max, range_[0]) if step > 0 else partial(min, range_[1])
    return op(val) # error: Incompatible return value type (got "SupportsRichComparisonT", expected "float")  [return-value]
    # return max(val, range_[0]) # OK

if __name__ == '__main__':
    print(test(5,-1, [3,6]))

It seems that partial will infer to the generic return type of max and min, i.e., "SupportsRichComparisonT", but it should infer to float because args in max nad min are floats.

Programs version:

  • Python 3.10.1
  • mypy 1.2.0 (compiled: yes)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions