Skip to content

Commit 1d2e5c6

Browse files
authored
Add comments on when to use which TypeVar (#8212)
* Add comments on when to use which `TypeVar` From the chars of #8208
1 parent 99f8446 commit 1d2e5c6

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

xarray/core/types.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,21 @@ def copy(
154154
T_Array = TypeVar("T_Array", bound="AbstractArray")
155155
T_Index = TypeVar("T_Index", bound="Index")
156156

157+
# `T_Xarray` is a type variable that can be either "DataArray" or "Dataset". When used
158+
# in a function definition, all inputs and outputs annotated with `T_Xarray` must be of
159+
# the same concrete type, either "DataArray" or "Dataset". This is generally preferred
160+
# over `T_DataArrayOrSet`, given the type system can determine the exact type.
161+
T_Xarray = TypeVar("T_Xarray", "DataArray", "Dataset")
162+
163+
# `T_DataArrayOrSet` is a type variable that is bounded to either "DataArray" or
164+
# "Dataset". Use it for functions that might return either type, but where the exact
165+
# type cannot be determined statically using the type system.
157166
T_DataArrayOrSet = TypeVar("T_DataArrayOrSet", bound=Union["Dataset", "DataArray"])
158167

159-
# Maybe we rename this to T_Data or something less Fortran-y?
160-
T_Xarray = TypeVar("T_Xarray", "DataArray", "Dataset")
168+
# For working directly with `DataWithCoords`. It will only allow using methods defined
169+
# on `DataWithCoords`.
161170
T_DataWithCoords = TypeVar("T_DataWithCoords", bound="DataWithCoords")
171+
162172
T_Alignable = TypeVar("T_Alignable", bound="Alignable")
163173

164174
# Temporary placeholder for indicating an array api compliant type.

0 commit comments

Comments
 (0)