Closed
Description
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
.
Metadata
Metadata
Assignees
Labels
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
gvanrossum commentedon Apr 6, 2016
Ah. This probably explains why we ran into problems trying to define the type of certain decorators correctly.
Ensure that generic function type variable ids are fresh
JukkaL commentedon Apr 6, 2016
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.)