Skip to content

Commit e4266dc

Browse files
committed
Update docs/source/contributing/docs/types.rst
1 parent 211c9e6 commit e4266dc

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

docs/source/contributing/docs/types.rst

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,46 @@ in space. For example:
2222

2323
.. code-block:: python
2424
25-
def status2D(coord: Point2D) -> None:
25+
def print_point2D(coord: Point2DLike) -> None:
2626
x, y = coord
2727
print(f"Point at {x=},{y=}")
2828
2929
30-
def status3D(coord: Point3D) -> None:
30+
def print_point3D(coord: Point3DLike) -> None:
3131
x, y, z = coord
3232
print(f"Point at {x=},{y=},{z=}")
3333
3434
35-
def get_statuses(coords: Point2D_Array | Point3D_Array) -> None:
35+
def print_point_array(coords: Point2DLike_Array | Point3DLike_Array) -> None:
3636
for coord in coords:
3737
if len(coord) == 2:
38-
# it's a Point2D
39-
status2D(coord)
38+
# it's a Point2DLike
39+
print_point2D(coord)
4040
else:
41-
# it's a point3D
42-
status3D(coord)
43-
44-
It's important to realize that the status functions accepted both
45-
tuples/lists of the correct length, and ``NDArray``'s of the correct shape.
46-
If they only accepted ``NDArray``'s, we would use their ``Internal`` counterparts:
47-
:class:`~.typing.InternalPoint2D`, :class:`~.typing.InternalPoint3D`, :class:`~.typing.InternalPoint2D_Array` and :class:`~.typing.InternalPoint3D_Array`.
48-
49-
In general, the type aliases prefixed with ``Internal`` should never be used on
50-
user-facing classes and functions, but should be reserved for internal behavior.
41+
# it's a Point3DLike
42+
print_point3D(coord)
43+
44+
def shift_point_up(coord: Point3DLike) -> Point3D:
45+
result = np.asarray(coord)
46+
result += UP
47+
return result
48+
49+
Notice that the last function, ``shift_point_up()``, accepts a
50+
:class:`~.Point3DLike` as a parameter and returns a :class:`~.Point3D`. A
51+
:class:`~.Point3D` always represents a NumPy array consisting of 3 floats,
52+
whereas a :class:`~.Point3DLike` can represent anything resembling a 3D point:
53+
either a NumPy array or a tuple/list of 3 floats, hence the ``Like`` word. The
54+
same happens with :class:`~.Point2D`, :class:`~.Point2D_Array` and
55+
:class:`~.Point3D_Array`, and their ``Like`` counterparts
56+
:class:`~.Point2DLike`, :class:`~.Point2DLike_Array` and
57+
:class:`~.Point3DLike_Array`.
58+
59+
The rule for typing functions is: **make parameter types as broad as possible,
60+
and return types as specific as possible.** Therefore, for functions which are
61+
intended to be called by users, we should always, if possible, accept ``Like``
62+
types as parameters and return NumPy, non-``Like`` types. The last function,
63+
``shift_point_up()``, is an example of it. Internal functions which are *not*
64+
meant to be called by users may accept non-``Like`` parameters if necessary.
5165

5266
Vectors
5367
~~~~~~~

0 commit comments

Comments
 (0)