Skip to content
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

added Vector2.from_polar and Vector3.from_spherical classmethods #2141

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions buildconfig/stubs/pygame/math.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ from typing import (
Union,
final,
overload,
Optional
Optional,
)

if sys.version_info >= (3, 9):
Expand Down Expand Up @@ -214,6 +214,11 @@ class Vector2(_GenericVector):
xy: Vector2
yx: Vector2
yy: Vector2
@overload # type: ignore
@classmethod
def from_polar(cls, value: Tuple[float, float], /) -> Vector2: ...
Comment on lines +222 to +224
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@overload # type: ignore
@classmethod
def from_polar(cls, value: Tuple[float, float], /) -> Vector2: ...
@overload
def from_polar(value: Tuple[float, float], /) -> Vector2: ... # type: ignore

It looks like the hack for typing fails for the class call. A bit of experimentation gives a different fix, in mypy.
This should also be applied to from_spherical.

P.S.: The new Point and SequenceLike types should be integrated into the PR if it is going to be merged.

@overload
def from_polar(self, value: Tuple[float, float], /) -> None: ...
@overload
def __init__(
self: _TVec,
Expand All @@ -229,7 +234,6 @@ class Vector2(_GenericVector):
def rotate_ip_rad(self, angle: float, /) -> None: ...
def cross(self: _TVec, other: Union[Sequence[float], _TVec], /) -> float: ...
def as_polar(self) -> Tuple[float, float]: ...
def from_polar(self, polar_value: Sequence[float], /) -> None: ...
@overload
def update(
self: _TVec,
Expand Down Expand Up @@ -278,6 +282,11 @@ class Vector3(_GenericVector):
zzx: Vector3
zzy: Vector3
zzz: Vector3
@overload # type: ignore
@classmethod
def from_spherical(cls, value: Tuple[float, float, float], /) -> Vector2: ...
@overload
def from_spherical(self, value: Tuple[float, float, float], /) -> None: ...
@overload
def __init__(
self: _TVec,
Expand Down Expand Up @@ -318,7 +327,6 @@ class Vector3(_GenericVector):
def rotate_z_rad_ip(self, angle: float, /) -> None: ...
def rotate_z_ip_rad(self, angle: float, /) -> None: ...
def as_spherical(self) -> Tuple[float, float, float]: ...
def from_spherical(self, spherical: Tuple[float, float, float], /) -> None: ...
@overload
def update(
self: _TVec,
Expand Down
18 changes: 11 additions & 7 deletions docs/reST/ref/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,13 @@ Multiple coordinates can be set using slices or swizzling

.. method:: from_polar

| :sl:`Sets x and y from a polar coordinates tuple.`
| :sg:`from_polar((r, phi), /) -> None`
| :sl:`Creates a Vector2(x, y) or sets x and y from a polar coordinates tuple.`
| :sg:`Vector2.from_polar((r, phi), /) -> Vector2`
| :sg:`Vector2().from_polar((r, phi), /) -> None`

Sets x and y from a tuple (r, phi) where r is the radial distance, and
phi is the azimuthal angle.
If used from the class creates a Vector2(x,y), else sets x and y.
The values of x and y are defined from a tuple ``(r, phi)`` where r
is the radial distance, and phi is the azimuthal angle.

.. ## Vector2.from_polar ##

Expand Down Expand Up @@ -986,10 +988,12 @@ Multiple coordinates can be set using slices or swizzling

.. method:: from_spherical

| :sl:`Sets x, y and z from a spherical coordinates 3-tuple.`
| :sg:`from_spherical((r, theta, phi), /) -> None`
| :sl:`Creates a Vector3(x, y, z) or sets x, y and z from a spherical coordinates 3-tuple.`
| :sg:`Vector3.from_spherical((r, theta, phi), /) -> Vector3`
| :sg:`Vector3().from_spherical((r, theta, phi), /) -> None`

Sets x, y and z from a tuple ``(r, theta, phi)`` where r is the radial
If used from the class creates a Vector3(x, y, z), else sets x, y, and z.
The values of x, y, and z are from a tuple ``(r, theta, phi)`` where r is the radial
distance, theta is the inclination angle and phi is the azimuthal angle.

.. ## Vector3.from_spherical ##
Expand Down
4 changes: 2 additions & 2 deletions src_c/doc/math_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#define DOC_MATH_VECTOR2_ROTATERADIP "rotate_rad_ip(angle, /) -> None\nrotates the vector by a given angle in radians in place."
#define DOC_MATH_VECTOR2_ANGLETO "angle_to(Vector2, /) -> float\ncalculates the angle to a given vector in degrees."
#define DOC_MATH_VECTOR2_ASPOLAR "as_polar() -> (r, phi)\nreturns a tuple with radial distance and azimuthal angle."
#define DOC_MATH_VECTOR2_FROMPOLAR "from_polar((r, phi), /) -> None\nSets x and y from a polar coordinates tuple."
#define DOC_MATH_VECTOR2_FROMPOLAR "Vector2.from_polar((r, phi), /) -> Vector2\nVector2().from_polar((r, phi), /) -> None\nCreates a Vector2(x, y) or sets x and y from a polar coordinates tuple."
#define DOC_MATH_VECTOR2_PROJECT "project(Vector2, /) -> Vector2\nprojects a vector onto another."
#define DOC_MATH_VECTOR2_COPY "copy() -> Vector2\nReturns a copy of itself."
#define DOC_MATH_VECTOR2_CLAMPMAGNITUDE "clamp_magnitude(max_length, /) -> Vector2\nclamp_magnitude(min_length, max_length, /) -> Vector2\nReturns a copy of a vector with the magnitude clamped between max_length and min_length."
Expand Down Expand Up @@ -77,7 +77,7 @@
#define DOC_MATH_VECTOR3_ROTATEZRADIP "rotate_z_rad_ip(angle, /) -> None\nrotates the vector around the z-axis by the angle in radians in place."
#define DOC_MATH_VECTOR3_ANGLETO "angle_to(Vector3, /) -> float\ncalculates the angle to a given vector in degrees."
#define DOC_MATH_VECTOR3_ASSPHERICAL "as_spherical() -> (r, theta, phi)\nreturns a tuple with radial distance, inclination and azimuthal angle."
#define DOC_MATH_VECTOR3_FROMSPHERICAL "from_spherical((r, theta, phi), /) -> None\nSets x, y and z from a spherical coordinates 3-tuple."
#define DOC_MATH_VECTOR3_FROMSPHERICAL "Vector3.from_spherical((r, theta, phi), /) -> Vector3\nVector3().from_spherical((r, theta, phi), /) -> None\nCreates a Vector3(x, y, z) or sets x, y and z from a spherical coordinates 3-tuple."
#define DOC_MATH_VECTOR3_PROJECT "project(Vector3, /) -> Vector3\nprojects a vector onto another."
#define DOC_MATH_VECTOR3_COPY "copy() -> Vector3\nReturns a copy of itself."
#define DOC_MATH_VECTOR3_CLAMPMAGNITUDE "clamp_magnitude(max_length, /) -> Vector3\nclamp_magnitude(min_length, max_length, /) -> Vector3\nReturns a copy of a vector with the magnitude clamped between max_length and min_length."
Expand Down
Loading