You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
The text was updated successfully, but these errors were encountered:
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:
Output:
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
).The text was updated successfully, but these errors were encountered: