CallableFunction as a drop-in replacement for Callable
#2191
Unanswered
hunterhogan
asked this question in
Q&A
Replies: 1 comment
-
Three cases when the type doesn't workty says invalid-assignment in all three cases. See playground. One function is decorated with functools.cache and Pylance says: Type "list[_lru_cache_wrapper[int] | ((dd: int, /) -> int)]" is not assignable to declared type "list[CallableFunction[(int), int]]"
"_lru_cache_wrapper[int]" is incompatible with protocol "CallableFunction[(int), int]"
"__closure__" is not present
"__code__" is not present
"__defaults__" is not present
"__globals__" is not present
"__name__" is not present
"__annotate__" is not present
"__kwdefaults__" is not presentIn the other two cases, the functions are not decorated, and Pylance objects to # Code
direction: CallableFunction[[int], int] = pos if 0 <=index else neg
# Diagnostic message
Type "((a: _SupportsPos[_T_co@pos], /) -> _T_co@pos) | ((a: _SupportsNeg[_T_co@neg], /) -> _T_co@neg)" is not assignable to declared type "CallableFunction[(int), int]"
Type "((a: _SupportsPos[_T_co@pos], /) -> _T_co@pos) | ((a: _SupportsNeg[_T_co@neg], /) -> _T_co@neg)" is not assignable to type "CallableFunction[(int), int]"
"FunctionType" is incompatible with protocol "CallableFunction[(int), int]"
"__wrapped__" is not present
"CallableFunction[P@CallableFunction, R@CallableFunction]" is not assignable to "FunctionType"
"CallableFunction[P@CallableFunction, R@CallableFunction]" is not assignable to "FunctionType"
"CallableFunction[P@CallableFunction, R@CallableFunction]" is not assignable to "FunctionType"If I remove |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Not all
Callablehave the__name__attribute. See, for example, "Why does ty sayCallablehas no attribute__name__?". That looks like a difficult problem to solve, so I've attempted to create a type I can use temporarily while the experienced programmers work on a permanent solution.I learned about this issue because of a function in my tests. The type below works as a replacement for my one function. See ty playground. But that doesn't mean much.
I've mashed-up annotations from typeshed, then "refined" the code by running it through different LLMs and trying to separate the good advice from the bad advice.
Frankly, this is beyond my ability to evaluate. The type below may be so flawed that I should start over. If this is a decent start, I don't know how to find the parts that need improvement. Therefore, I would love some feedback.
Beta Was this translation helpful? Give feedback.
All reactions