Skip to content

functools.partial not well supported #2967

Closed
@rhettinger

Description

@rhettinger

Given this code:

from typing import Tuple
from math import hypot
from functools import partial

def dist(p: Tuple[float, float], q: Tuple[float, float]) -> float:
    px, py = p
    qx, qy = q
    return hypot(px - qx, py - qy)

base = (1.9, 7.5)
targets = [(3,7), (2,6), (1,8), (0,7)]

def make_fixed_dist(q):
    def fixed_dist(p):
	return dist(p, q)
    return fixed_dist

print( min(targets, key=make_fixed_dist(base)) )
print( min(targets, key=partial(dist, base)) )

mypy cheerfully accepts the closure but chokes on the equivalent partial:

$ mypy tmp.py
tmp.py:19: error: No overload variant of "min" matches argument types [builtins.list[Tuple[builtins.int, builtins.int]], functools.partial[builtins.float*]]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions