Type hinting a union of callables does not work as hoped #5835
Unanswered
Hugovdberg
asked this question in
Q&A
Replies: 1 comment 1 reply
-
If |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to implement a robust version of the builtin
max
function by performing a reduction on an iterable with a given initial value (which eventually will be curried using theexpression
package):where
_SupportsLT
and_SupportsGT
are identical to the protocols defined in the stdlib typeshed.Now the problem is that Pylance raises a reportOperatorIssue on the
keyfunc(acc) > keyfunc(value)
part. This is becausekeyfunc(acc)
has typeSupportsLT | _SupportsGT
, and the same forkeyfunc(value)
, and_SupportsLT
and_SupportsGT
cannot be compared. However, given the fact that thekeyfunc
either always returns a_SupportsLT
or always a_SupportsGT
the reported error condition would never occur.Is there a way to modify the type hints such that Pylance can infer that the comparison will always succeed?
Beta Was this translation helpful? Give feedback.
All reactions