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 variable in container of callable is not solved #1639

Closed
layday opened this issue Mar 17, 2021 · 3 comments
Closed

Type variable in container of callable is not solved #1639

layday opened this issue Mar 17, 2021 · 3 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@layday
Copy link

layday commented Mar 17, 2021

Describe the bug
When a type variable T is inside a container which is a Callable parameter (a callback), which is itself a parameter of a function whose return type is the return type of the Callable, an unsolved copy of T is left dangling in a union.

To Reproduce
Type check the snippet below.

Expected behavior
That the type of qux([1.], bar) would be float.

Screenshots or Code

from typing import Callable, Sequence, TypeVar

T = TypeVar("T")
U = TypeVar("U")

def foo(value: T) -> T:
    ...

def bar(values: Sequence[T]) -> T:
    ...

def baz(
    value: T,
    callback: Callable[[T], U],
) -> U:
    ...

def qux(
    values: Sequence[T],
    callback: Callable[[Sequence[T]], U],
) -> U:
    ...

reveal_type(baz(1., foo))       # Type of "baz(1, foo)" is "float"
reveal_type(qux([1.], foo))     # Type of "qux([1], foo)" is "Sequence[float]"
reveal_type(qux([1.], bar))     # Type of "qux([1], bar)" is "float | T@bar" - should be "float"

VS Code extension or command-line
Either, 1.1.122 - used to work in versions prior to 1.1.122.

@erictraut
Copy link
Collaborator

Thanks for the bug report. This will be fixed in the next release.

Interestingly, mypy reports errors in all three of these cases:

test7.py:29: error: Argument 2 to "baz" has incompatible type "Callable[[T], T]"; expected "Callable[[float], T]"
test7.py:30: note: Revealed type is 'T`-1'
test7.py:30: error: Argument 2 to "qux" has incompatible type "Callable[[T], T]"; expected "Callable[[Sequence[float]], T]"
test7.py:32: note: Revealed type is 'T`-1'
test7.py:32: error: Argument 2 to "qux" has incompatible type "Callable[[Sequence[T]], T]"; expected "Callable[[Sequence[float]], T]"

@erictraut erictraut added addressed in next version Issue is fixed and will appear in next published version bug Something isn't working labels Mar 18, 2021
@layday
Copy link
Author

layday commented Mar 18, 2021

Huh, interesting, can you not type generic callbacks at all with mypy?

@erictraut
Copy link
Collaborator

This is now addressed in pyright 1.1.123, which I just published. It will also be included in the next release of pylance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants