Skip to content

type not inferred in application of higher-order function to a generic function #1507

Closed
@rwbarton

Description

@rwbarton
from typing import TypeVar, Callable
S = TypeVar('S')
def apply(f: Callable[[int], S]) -> S: pass
def g(x: S) -> S: pass
y = apply(g) # E: Argument 1 to "apply" has incompatible type Callable[[S], S]; expected Callable[[int], S]
reveal_type(y) # E: Revealed type is 'S`-1'

The call to apply should type check, with both types S and the type of y being int.

I encountered this sort of situation in mypy/codec/pytokenize.py in the lines

tokenprog, pseudoprog, single3prog, double3prog = map(
    re.compile, (Token, PseudoToken, Single3, Double3))

#1471 was an attempt to fix this, but since it fixes this case but not #1506, I'm filing these as two related but distinct issues.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions