Skip to content
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

Type inference problems with unions, overloads and map #390

Closed
rockneurotiko opened this issue Aug 16, 2014 · 7 comments
Closed

Type inference problems with unions, overloads and map #390

rockneurotiko opened this issue Aug 16, 2014 · 7 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-2-low topic-overloads topic-union-types

Comments

@rockneurotiko
Copy link
Contributor

I'm playing with mypy, trying to do some things, and I've getted some strange behaviour (I think).

I'm trying to do a function that takes a list and then apply map function. The trick is that the list can be of two different types.

The first try was something like that:

def f(L: Union[List[int], List[str]]) -> None:
    l = map(t, L)

@overload
def t(L: int) -> List[int]:
    return [L]
@overload
def t(L: str) -> List[str]:
    return [L]

The fail that getted was this one:

test.py, line 8: Argument 2 to "map" has incompatible type "Union[List[int], List[str]]"; expected Iterable[None]

Then, I tried to do it with a typevar:

T = typevar('T', values=(int, str))
def f(L: List[T]) -> None:
    l = map(t, L)

@overload
def t(L: int) -> List[int]:
    return [L]
@overload
def t(L: str) -> List[str]:
    return [L]

And now I've getted that error:

test.py, line 8: Argument 1 to "map" has incompatible type functionlike; expected Function[["str"] -> List[int]]

If I add the function that 'mypy' says:

@overload
def t(L: str) -> List[int]:
    return [1]

And now the typecheck pass.
If I write the overloaded functions backwards, mypy expects other thing:

@overload
def t(L: str) -> List[str]:
    return [L]
@overload
def t(L: int) -> List[int]:
    return [L]

Expected:

test.py, line 8: Argument 1 to "map" has incompatible type functionlike; expected Function[["int"] -> List[str]]

I don't really know why that's going on, but I think that is a strange behaviour :)

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 17, 2014

Thanks for reporting this!

There are at least a few known type inference related bugs and this could be one of these or a related issue. They tend to revolve around overloading and higher-order functions. I'm going to look at them one of these days once things settle down a bit...

@JukkaL JukkaL changed the title Strange behaviour. Type inference problems with overloads and map Aug 17, 2014
@JukkaL JukkaL changed the title Type inference problems with overloads and map Type inference problems with unions, overloads and map Aug 17, 2014
@JukkaL
Copy link
Collaborator

JukkaL commented Nov 8, 2014

Added issue #495 derived from the first example.

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 8, 2014

Added issue #496 derived from the second example.

@JukkaL JukkaL removed the priority label Nov 8, 2014
@JukkaL
Copy link
Collaborator

JukkaL commented Nov 8, 2014

Lowering priority. The related issue #496 has priority but #495 is less urgent unless there are more reports about people are hitting it.

@JukkaL
Copy link
Collaborator

JukkaL commented May 17, 2018

This is still failing (updated to use the current syntax):

from typing import List, Union, overload

def f(x: Union[List[int], List[str]]) -> None:
    l = map(f, x)  # Cannot infer type argument 1 of "map"
@overload
def f(x: int) -> List[int]: ...
@overload
def f(x: str) -> List[str]: ...
def f(x):
    return [x]

@JukkaL JukkaL added priority-2-low false-positive mypy gave an error on correct code labels May 17, 2018
@ilevkivskyi
Copy link
Member

@Michael0x2a I think this might be a good test for your union math PR.

@ilevkivskyi
Copy link
Member

Both examples pass on current master (most likely because of union math).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-2-low topic-overloads topic-union-types
Projects
None yet
Development

No branches or pull requests

3 participants