Skip to content

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

Closed

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.

Activity

  1. self-assigned this
    on May 8, 2016
  2. removed their assignment
    on Jul 1, 2016
  3. added this to the milestone on Jul 7, 2016
  4. removed this from the milestone on Mar 29, 2017
  5. davidhalter commented on Jan 21, 2024

    @davidhalter

    This is working now. So I feel like this issue should be closed.

    See https://mypy-play.net/?mypy=latest&python=3.12&gist=2265ae341b2994f28577afc8788c0879

  6. hauntsaninja commented on Jan 21, 2024

    @hauntsaninja
    Collaborator

    Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions