Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Flaw with type aliases containing type variables #2690

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
gvanrossum opened this issue Jan 13, 2017 · 6 comments
Closed

Flaw with type aliases containing type variables #2690

gvanrossum opened this issue Jan 13, 2017 · 6 comments
Labels
bug mypy got something wrong

Comments

@gvanrossum
Copy link
Member

Reported by @mohabusama on gitter -- short version:

from typing import *
T = TypeVar('T', List[int], Dict)
Processor = Callable[[T], None]
def output2(inp: T, processor: Processor) -> T:
    processor(inp)
def process_dict(inp: Dict) -> None:
    print(inp.keys())
output2([1, 2, 3], process_dict)

There should be an error on the last line, but there isn't. Surrounding the line with reveal_type() shows that the inferred value for T is List[int].

If we expand the type alias Processor inline in the signature of output2() we get the expected error:

__tmp__.py:8: error: Argument 2 to "output2" has incompatible type Callable[[Dict[Any, Any]], None]; expected Callable[[List[int]], None]

This feels like a flaw in @ilevkivskyi's #2378 (which added support for type aliases containing type variables).

@gvanrossum gvanrossum added the bug mypy got something wrong label Jan 14, 2017
@ilevkivskyi
Copy link
Member

In don't think this is a flaw. The function output2 should be annotated as:

def output2(inp: T, processor: Processor[T]) -> T:
    ...

Then everything works as expected. Note that that it is Processor[T], not Processor, because plain generic alias Processor is treated as Processor[Any].

We discussed this some time ago and everyone agreed that generic aliases should use the same rules as generic types. This is how it is now documented in PEP 484, for example:

Vector = Iterable[Tuple[T, T]]

def inproduct(v: Vector[T]) -> T:
    return sum(x*y for x, y in v)

and also in mypy and typing docs. Note it is Vector[T] not Vector. I still believe this is the right way to do it.

@gvanrossum
Copy link
Member Author

gvanrossum commented Jan 14, 2017 via email

@roganov
Copy link

roganov commented Jan 14, 2017

I was also hit by this issue once or twice. I think it would make sense to a have a flag that disables implicit Any and forces to 'instantiate' all generic types.

@mohabusama
Copy link

@gvanrossum @ilevkivskyi Thanks for the swift responses and explanations.

The assumption came from the constrained type variable, which I guess one would assume Any won't be applied implicitly in that case.

Do you think it is possible for mypy to produce a warning in that specific case?

@ilevkivskyi
Copy link
Member

@mohabusama @roganov
In principle it is possible to add --disable-implicit-any and there is probably no conceptual difficulty. However, this will require many small changes in many places. In any case, if you would like to see this feature/flag, then I would recommend opening a separate issue.

@mohabusama
Copy link

@ilevkivskyi @roganov I have created an issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

4 participants