Skip to content

type variable escapes in application of higher-order function to a generic function #1506

Closed
@rwbarton

Description

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

The type of y is not allowed to contain an unbound type variable. There should have been an error Need type annotation for variable on the assignment to y.

(With apply having type def apply(f: Callable[[int], int]) -> S, mypy does give such an error.)

Activity

self-assigned this
on May 7, 2016
changed the title [-]type variable escapes in application of higher-order function[/-] [+]type variable escapes in application of higher-order function to a generic function[/+] on May 8, 2016
rwbarton

rwbarton commented on May 8, 2016

@rwbarton
ContributorAuthor

Also, even when the input program has a type error, mypy should still not produce ill-formed types like S-1` here. That way we can run mypy in a mode with extra consistency checks on the test suite, which is full of intentional type errors, to find more bugs like this one.

removed their assignment
on Jul 1, 2016
added this to the 0.5 milestone on Jul 7, 2016
removed this from the 0.5 milestone on Mar 29, 2017
erictraut

erictraut commented on Aug 10, 2023

@erictraut

Mypy is arguably doing the right thing in this case. The code sample contains a malformed generic function g that returns a generic S but doesn't use S in the types of any input parameters. Mypy correctly reports this error.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @rwbarton@gnprice@gvanrossum@erictraut@ilevkivskyi

        Issue actions

          type variable escapes in application of higher-order function to a generic function · Issue #1506 · python/mypy