-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
TYP: Add overload to specify output of Colormap.__call__ when possible #26504
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you so much for the quick patch ! I confirm this fixes the issue
@overload | ||
def __call__( | ||
self, X: float, alpha: float | None = ..., bytes: bool = ... | ||
) -> tuple[float, float, float, float]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m trying to get my head around why the third option below still needs the union. Are there inputs other than X: float, alpha: float
that would return a tuple? Or does the checker check the float, float
combination against both because floats are array-like and the checker doesn’t stop after finding the precise match? Or something else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ArrayLike
is a fair bit broader than I would actually like in many cases. Numpy (understandably) does not provide all of its building blocks as public apis, though, so narrowing it would essentially mean reimplementing or vendoring a lot of the machinery. But I suspect there are things that do qualify under arraylike that may not qualify as Sequence (particularly things that provide a __array__
method, though probably still inadvisable to make such a thing that isn't considered a sequence)
Mypy does not like when multiple signatures are given that are incompatible, though and float is included in arraylike, so it's output must also be in the return type.
One option would be to make the fallback Any
so that users won't have to deal with a union return type but mypy also doesn't complain. It's a bit of a cop out answer, but sometimes best for situations where the return type is known but cannot be differentiated by input types (in this case because we want the catch all arraylike to be sure we didn't miss anything)
The other aspect I didn't fully think about was if other scalars could be passed, though pretty sure it essentially is floats (and int I guess, but type checkers accept int when typed as float)
TLDR it's a conservative catch all that probably isn't going to be selected in most use cases, but needs to be consistent with the other signature options
…Colormap.__call__ when possible
…504-on-v3.8.x Backport PR #26504 on branch v3.8.x (TYP: Add overload to specify output of Colormap.__call__ when possible)
PR summary
Closes #26501
Does much the same as #26434, but for
Colormap.__call__
, noting that the scalar->tuple case is a little different to scalar->scalarPR checklist