-
-
Notifications
You must be signed in to change notification settings - Fork 3k
"Cannot determine type of ..." when using decorators #11212
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
Comments
Surprisingly, with a regular identity as a decorator it works fine: from typing import Callable, TypeVar
C = TypeVar('C', bound=Callable)
def dec(f: C) -> C:
return f
lambda: foo() == 2
@dec
def foo() -> int:
return 2 Another surprising discovery: it also works fine when both the function and the lambda are enclosed: from typing import Callable, TypeVar
C = TypeVar('C', bound=Callable)
def dec(val: None) -> Callable[[C], C]:
def wrapper(f):
return f
return wrapper
def f() -> None:
lambda: foo() == 2
@dec(None)
def foo() -> int:
return 2 If you're wondering what can be a use case, I'm making a decorator for providing function usage examples for deal: @deal.example(lambda: double1(2) == 4)
def double1(x: int) -> int:
return x * 2 Here, lambda is passed as an argument into the decorator and it produces the same error as in the issue description. |
@orsinium Hi 👋 😊 Glad to see you here (but, I am sorry that something does not work for you!) I will try to solve this, I have some ideas about the possible cause, but no promises here: inference is always quite hard and any changes there can have massive influence on other features. |
I'm glad to see you there too :) When you mentioned inference, I made another surprising discovery: it also works well when I use a function instead of a lambda: from typing import Callable, TypeVar
C = TypeVar('C', bound=Callable)
def dec(val: None) -> Callable[[C], C]:
def wrapper(f):
return f
return wrapper
def f() -> None:
foo() == 2
@dec(None)
def foo() -> int:
return 2 What an interesting case! Something very specific to lambdas in a global namespace, hmm. |
…e_type` (#11215) Closes #11212 Previously `mypy` was ignoring `lambda` scope in `handle_cannot_determine_type`. I guess it was done to fix some `lambda` related crashes. I've also noticed one crash with this new solution in existing test. I happened because when right `or` part is unreachable, there's not `binder.frame` for it. So, I've made a temporary scope of it.
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
To Reproduce
Content of
tmp.py
Running mypy:
Your Environment
mypy.ini
(and other config files): --5.4.0-81-generic #91-Ubuntu SMP Thu Jul 15 19:09:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
The text was updated successfully, but these errors were encountered: