-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
pyAutoGui: Correct Return Type of position() to Match Actual Behavior #11267
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
pyAutoGui: Correct Return Type of position() to Match Actual Behavior #11267
Conversation
…chonaute/typeshed into pyautogui-position-wrong-return-type
…chonaute/typeshed into pyautogui-position-wrong-return-type
…chonaute/typeshed into pyautogui-position-wrong-return-type
This comment has been minimized.
This comment has been minimized.
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.
My concern is that this will cause additional casting being needed when a Point
is expected and you loose property access:
from math import hypot
def distance_from_origin(point: Point) -> float:
return hypot(point.x, point.y)
int_point = position(5, 10)
distance = distance_from_origin(int_point) # (variable) int_point: tuple[int, int]
# Argument of type "tuple[int, int]" cannot be assigned to parameter "point" of type "Point" in function "distance_from_origin"
# "tuple[int, int]" is incompatible with "Point" Pylance(reportGeneralTypeIssues)
# Argument 1 to "distance_from_origin" has incompatible type "Tuple[int, int]"; expected "Point" Mypy(arg-type)
I think that by using a covariant Generic this can be solved:
from typing import Generic, NamedTuple, TypeVar
_T = TypeVar("_T", int, float, default=float, covariant=True)
class Point(NamedTuple, Generic[_T]):
x: _T
y: _T
def position(x: int | None = None, y: int | None = None) -> Point[int]: ...
###
# Downstream code example
from math import hypot
def distance_from_origin(point: Point): # Same as Point[float]
return hypot(point.x, point.y)
int_point = position(5, 10)
distance = distance_from_origin(int_point)
This makes possible to pass a Point[int]
to a method expecting a Point[float]
. And still disallows passing a Point[float]
to a method expecting a Point[int]
.
Once implemented, we could probably improve other methods that are also known to always return a Point[int]
. I vaguely remember screen positions being a float only on a single platform (linux?).
The use of default
reduces required changes (especially downstream), given this is an edge case. I'm not sure what's the current state of https://peps.python.org/pep-0696/ support. But typeshed's tests should fail if a supported type-checker is not ready for TypeVar defaults.
(CC @AlexWaygood in case you see any issue with my proposal, but it seems sound to me and my quick testing)
(I'm super busy this week unfortunately, so probably won't be able to take a proper look at this until after the 20th, but I trust your judgement!) |
I don't think we're quite ready for PEP 696 in typeshed. Until then, the current annotation is probably best; int is a subtype of float in the type system, after all. |
TypeVar defaults are now available in typeshed. |
This comment has been minimized.
This comment has been minimized.
@codekoriko Are you willing to update this PR to make use of generic defaults as per #11267 (review) ? If not, I can probably take it over. |
please go ahead 👍🏻 |
@Avasam are you still interested in continuing this PR? |
…gui-position-wrong-return-type
I forgot about this. I just applied my previous suggestion. Although, looking back into this. It looks like PyAutoGUI's So OP's original PR might've been fine as-is. |
This comment has been minimized.
This comment has been minimized.
This reverts commit 8b6253f.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
…python#11267) Co-authored-by: Avasam <samuel.06@hotmail.com>
Point
being aTuple
offloat
it does not match the actual behavior of theposition()
function: