Skip to content

Type variables of a nested generic function are confused with type variables of surrounding generic function #1335

Closed
@rwbarton

Description

@rwbarton
Contributor
from typing import TypeVar
T = TypeVar('T')
U = TypeVar('U')

def outer(x: T) -> T:
    def inner(y: U) -> T: ...
    return inner(1)    # error: Incompatible return value type: expected T`-1, got builtins.int*

I should be able to pass inner a value of any type and get back a T to return from outer. But internally the type variables U and T of inner have the same id (-1) so effectively mypy thinks inner has a type like (U) -> U.

Activity

gvanrossum

gvanrossum commented on Apr 6, 2016

@gvanrossum
Member

Ah. This probably explains why we ran into problems trying to define the type of certain decorators correctly.

added this to the 0.3.2 milestone on Apr 6, 2016
added a commit that references this issue on Apr 6, 2016
f95ce2a
JukkaL

JukkaL commented on Apr 6, 2016

@JukkaL
Collaborator

Yeah, type variables were originally implemented with various restrictions such as no nested (generic) functions that weren't enforced or were lifted later without fixing the underlying issues. The current way of doing type inference of generics also causes the two-namespace-approach to not work correctly in other contexts. (Though I'm not quite sure if generics ever worked without any scoping issues, and the namespace hack was questionable from the beginning.)

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

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @rwbarton@JukkaL@gvanrossum

        Issue actions

          Type variables of a nested generic function are confused with type variables of surrounding generic function · Issue #1335 · python/mypy