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

Callable not deduced correctly #16868

Closed
nschloe opened this issue Feb 4, 2024 · 2 comments
Closed

Callable not deduced correctly #16868

nschloe opened this issue Feb 4, 2024 · 2 comments
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions

Comments

@nschloe
Copy link

nschloe commented Feb 4, 2024

I have a function add(x,y) that adds two floats, and that that the user can specify. If no custom function add is given, default_add is used. If it is given, I'm adding a little wrapper code to assert stuff:

from typing import Callable


def default_add(x: float, y: float, extra: float = 0) -> float:
    return x + y + extra


def wrap_add(
    add: Callable[[float, float], float],
) -> Callable[[float, float], float]:
    def _add(x: float, y: float) -> float:
        assert isinstance(x, (float, int))
        assert isinstance(y, (float, int))

        z = add(x, y)

        assert isinstance(z, (float, int))
        return z

    return _add


fun: Callable[[float, float], float] | None = None

fun2 = default_add if fun is None else wrap_add(fun)

fun2(3, 5)

Running mypy on the above gives

a.py:27: error: Cannot call function of unknown type  [operator]

When adding explicit typing to fun2,

fun2: Callable[[float, float], float] = default_add if fun is None else wrap_add(fun)

one incorrectly gets

a.py:25: error: Incompatible types in assignment (expression has type "function", variable has type "Callable[[float, float], float]") 

  • Mypy version used: 1.8.0
  • Mypy command-line flags: -
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.11.6
@nschloe nschloe added the bug mypy got something wrong label Feb 4, 2024
@JelleZijlstra JelleZijlstra added the topic-join-v-union Using join vs. using unions label Feb 5, 2024
@randolf-scholz
Copy link
Contributor

randolf-scholz commented May 11, 2024

Another example: mypy-playground

from collections.abc import Callable

def pprint(obj: object, repr_fn: Callable[..., str] = NotImplemented) -> str:
    repr_fn = (
        repr_fn   # expression has type "function", variable has type "Callable[..., str]"
        if repr_fn is not NotImplemented
        else repr
    )
    
    return repr_fn(obj)

@hauntsaninja
Copy link
Collaborator

Fixed in #17427

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-join-v-union Using join vs. using unions
Projects
None yet
Development

No branches or pull requests

4 participants