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

Different behavior when switching arguments using same generic type? #7914

Closed
Holt59 opened this issue May 14, 2024 · 1 comment
Closed

Different behavior when switching arguments using same generic type? #7914

Holt59 opened this issue May 14, 2024 · 1 comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@Holt59
Copy link

Holt59 commented May 14, 2024

Disclaimer: Not sure if this is a bug or expected behavior but that was at least unexpected for me and I did not manage to find relevant information here or on the typing repository (or documentation).

Describe the bug
In the example below, f1 and f2 are similar except for the order of arguments, in f1, the callable is after the container, and the type of the callable is properly deduced, but in f2, the callable comes first and its type is not properly deduced.

from collections.abc import Iterable
from typing import Callable, Mapping, cast


def f1[K, V](vs: Iterable[Mapping[K, V]], fn: Callable[[Iterable[V]], V]): ...
def f2[K, V](fn: Callable[[Iterable[V]], V], vs: Iterable[Mapping[K, V]]): ...


# both ok
f1(({"a": 1, "b": 2},), sum)
f2(sum, ({"a": 1, "b": 2},))

# f1 ok, f2 ko
f1(({"a": {1}, "b": {2}},), lambda s: cast(set[int], set()).union(*s))
f2(lambda s: cast(set[int], set()).union(*s), ({"a": {1}, "b": {2}},))

Full error:

.\test2.py
  .\test2.py:15:4 - error: Argument type is partially unknown
    Argument corresponds to parameter "fn" in function "f2"
    Argument type is "(s: Iterable[set[int | Unknown]]) -> set[int | Unknown]" (reportUnknownArgumentType)
  .\test2.py:15:11 - error: Type of parameter "s" is partially unknown (reportUnknownLambdaType)
  .\test2.py:15:14 - error: Return type of lambda, "set[int | Unknown]", is partially unknown (reportUnknownLambdaType)
  .\test2.py:15:43 - error: Argument type is partially unknown
    Argument corresponds to parameter "s" in function "union"
    Argument type is "set[int | Unknown]" (reportUnknownArgumentType)
  .\test2.py:15:47 - error: Argument type is partially unknown
    Argument corresponds to parameter "vs" in function "f2"
    Argument type is "tuple[dict[str, set[int | Unknown]]]" (reportUnknownArgumentType)
5 errors, 0 warnings, 0 informations

VS Code extension or command-line

CLI, pyright 1.1.362

@Holt59 Holt59 added the bug Something isn't working label May 14, 2024
@erictraut erictraut added constraint solver addressed in next version Issue is fixed and will appear in next published version and removed constraint solver labels May 17, 2024
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.364.

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