Skip to content

Returned/passed lambdas ignore previous casts #9136

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
barisione opened this issue Jul 12, 2020 · 3 comments
Closed

Returned/passed lambdas ignore previous casts #9136

barisione opened this issue Jul 12, 2020 · 3 comments

Comments

@barisione
Copy link

I found a weird behaviour where mypy doesn't understand correctly the type of objects used inside lambdas which are returned/passes/etc. if a typing.cast was used on them.

Reproducer:

from typing import (
    Callable,
    Optional,
    TYPE_CHECKING,
    cast,
)


class Base:
    pass


class Deriv(Base):
    def deriv_method(self) -> None:
        print("Hello world")


global_fn: Optional[Callable] = None
global_lambda: Optional[Callable] = None


def func(obj: Base) -> Callable:
    obj = cast(Deriv, obj)
    if TYPE_CHECKING:
        reveal_type(obj)  # The revealed type is Deriv.

    obj.deriv_method()  # Mypy is fine with this.

    inner = lambda: obj.deriv_method()  # And also fine with this.
    inner()

    global global_lambda
    global_lambda = lambda: obj.deriv_method()  # error: "Base" has no attribute "deriv_method"

    def fn():
        obj.deriv_method()  # But this is fine!

    global global_fn
    global_fn = fn

    return lambda: obj.deriv_method()  # error: "Base" has no attribute "deriv_method"


callback = func(Deriv())
callback()
assert global_fn is not None
global_fn()
assert global_lambda is not None
global_lambda()

Output:

$ python3 --version
Python 3.8.2

$ python3 ./casttest.py
Hello world
Hello world
Hello world
Hello world
Hello world

$ mypy --version
mypy 0.790+dev.08cd1d66e82d2ca81cc014716f1a9b864b30f31f

$ mypy casttest.py
casttest.py:25: note: Revealed type is 'casttest.Deriv'
casttest.py:33: error: "Base" has no attribute "deriv_method"
casttest.py:41: error: "Base" has no attribute "deriv_method"
Found 2 errors in 1 file (checked 1 source file)

I would expect no error and I don't understand why nested non-lambda functions and lambdas which are noly used locally don't produce the same error.

I observed this initially with mypy 0.720 but the same happens with current master (0.790+dev.08cd1d66e82d2ca81cc014716f1a9b864b30f31f).

@Akuli
Copy link
Contributor

Akuli commented Jul 25, 2020

minimal example of possibly the same problem:

from typing import Any, Callable, Union

def func1(arg: Any) -> None:
    pass

def func2(arg: Callable[[], Any]) -> None:
    pass

f: Any
g: Callable[[], Any]

def lol(foo: Union[str, int]) -> None:
    global f, g
    if isinstance(foo, str):
        func1(lambda: reveal_type(foo))  # str
        func2(lambda: reveal_type(foo))  # Union[str, int]
        f = lambda: reveal_type(foo)     # str
        g = lambda: reveal_type(foo)     # Union[str, int]

@Akuli
Copy link
Contributor

Akuli commented Jul 26, 2020

I found a weird workaround for this. If I define

def mypy_fix(arg: Any) -> Any:
    return arg

and then do func2(mypy_fix(lambda: ...)) and g = mypy_fix(lambda: ...) instead, the revealed types become str.

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 31, 2020

Closing as duplicate of #4973.

@JukkaL JukkaL closed this as completed Jul 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants