Skip to content

default argument for generic function #2078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
naphatkrit opened this issue Sep 1, 2016 · 1 comment
Closed

default argument for generic function #2078

naphatkrit opened this issue Sep 1, 2016 · 1 comment

Comments

@naphatkrit
Copy link

naphatkrit commented Sep 1, 2016

I'm trying to do something like this:

T = TypeVar('T')

def identity(string):
  # type: (str) -> str
  return string

def f(normalize=identity):
  # type: (Callable[[str], T] -> str)

But mypy says this does not type-check. After adding a cast (normalize=cast(Any, identity), or even normalize=cast(Callable[[str], T], identity), this works fine.

@rwbarton
Copy link
Contributor

rwbarton commented Oct 7, 2016

This is intentional, as the caller of f gets to choose T and identity does not have type Callable[[str], T] for most types T. Also see the discussion at #1721.

Have you simplified the type of your real function f? It's hard to see how you could use normalize with this signature for f anyways.

@ddfisher ddfisher added this to the Questions milestone Oct 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants