Skip to content

Generic function type argument inference problem #230

Closed

Description

@JukkaL

The type checker can't infer the type argument in the call to f below, even though there seems to be no reason why it shouldn't be able to do it:

from typing import List, TypeVar
T = TypeVar('T')
def f(a: List[T], b: List[T]) -> None: pass
x = ['']
f(x, [])  # Cannot infer type argument 1 of "f"

Activity

  1. JukkaL commented on Mar 15, 2015

    @JukkaL
    CollaboratorAuthor

    After #556 is done, this becomes more annoying.

  2. added this to the milestone on Mar 2, 2016
  3. rwbarton commented on May 20, 2016

    @rwbarton
    Contributor

    This doesn't seem so easy to fix. We need to either make use of information learned from type checking the first argument to infer a type variable in the second argument (but what if the arguments were in the other order?) or be able to return a polymorphic type when type checking the second argument. I think the latter is easier and it's related to some other changes I want to make anyways (e.g. #1551), but it will require some reorganization.

  4. modified the milestones: , on Jul 14, 2016
  5. gvanrossum commented on Jul 14, 2016

    @gvanrossum
    Member

    Based on Reid's comment I'm moving this to 0.5.0 (or maybe even later?).

  6. removed this from the milestone on Mar 29, 2017
  7. earshinov commented on Aug 14, 2019

    @earshinov

    Is there some syntax to pass type arguments explicitly when Python cannot infer the types? I am looking for something like this: f[str](x, []). If not, is there an issue or document when this "explicit passing of type arguments" feature is tracked?

  8. ilevkivskyi commented on Aug 14, 2019

    @ilevkivskyi
    Member

    @earshinov I think the best recent issue to discuss allowing explicit type application for functions is #6073.

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

      Issue actions