8
8
TYPE_CHECKING ,
9
9
Any ,
10
10
Callable ,
11
- Dict ,
12
11
Hashable ,
13
12
Iterable ,
14
13
Iterator ,
15
- List ,
16
14
Mapping ,
17
- Optional ,
18
- Tuple ,
19
15
TypeVar ,
20
- Union ,
21
16
overload ,
22
17
)
23
18
@@ -164,9 +159,7 @@ def __iter__(self: Any) -> Iterator[Any]:
164
159
raise TypeError ("iteration over a 0-d array" )
165
160
return self ._iter ()
166
161
167
- def get_axis_num (
168
- self , dim : Union [Hashable , Iterable [Hashable ]]
169
- ) -> Union [int , Tuple [int , ...]]:
162
+ def get_axis_num (self , dim : Hashable | Iterable [Hashable ]) -> int | tuple [int , ...]:
170
163
"""Return axis number(s) corresponding to dimension(s) in this array.
171
164
172
165
Parameters
@@ -244,7 +237,7 @@ def __getattr__(self, name: str) -> Any:
244
237
with suppress (KeyError ):
245
238
return source [name ]
246
239
raise AttributeError (
247
- "{ !r} object has no attribute {!r}". format ( type ( self ). __name__ , name )
240
+ f" { type ( self ). __name__ !r} object has no attribute { name !r} "
248
241
)
249
242
250
243
# This complicated two-method design boosts overall performance of simple operations
@@ -284,37 +277,37 @@ def __setattr__(self, name: str, value: Any) -> None:
284
277
"assignment (e.g., `ds['name'] = ...`) instead of assigning variables."
285
278
) from e
286
279
287
- def __dir__ (self ) -> List [str ]:
280
+ def __dir__ (self ) -> list [str ]:
288
281
"""Provide method name lookup and completion. Only provide 'public'
289
282
methods.
290
283
"""
291
- extra_attrs = set (
284
+ extra_attrs = {
292
285
item
293
286
for source in self ._attr_sources
294
287
for item in source
295
288
if isinstance (item , str )
296
- )
289
+ }
297
290
return sorted (set (dir (type (self ))) | extra_attrs )
298
291
299
- def _ipython_key_completions_ (self ) -> List [str ]:
292
+ def _ipython_key_completions_ (self ) -> list [str ]:
300
293
"""Provide method for the key-autocompletions in IPython.
301
294
See http://ipython.readthedocs.io/en/stable/config/integrating.html#tab-completion
302
295
For the details.
303
296
"""
304
- items = set (
297
+ items = {
305
298
item
306
299
for source in self ._item_sources
307
300
for item in source
308
301
if isinstance (item , str )
309
- )
302
+ }
310
303
return list (items )
311
304
312
305
313
306
def get_squeeze_dims (
314
307
xarray_obj ,
315
- dim : Union [ Hashable , Iterable [Hashable ], None ] = None ,
316
- axis : Union [ int , Iterable [int ], None ] = None ,
317
- ) -> List [Hashable ]:
308
+ dim : Hashable | Iterable [Hashable ] | None = None ,
309
+ axis : int | Iterable [int ] | None = None ,
310
+ ) -> list [Hashable ]:
318
311
"""Get a list of dimensions to squeeze out."""
319
312
if dim is not None and axis is not None :
320
313
raise ValueError ("cannot use both parameters `axis` and `dim`" )
@@ -346,15 +339,15 @@ def get_squeeze_dims(
346
339
class DataWithCoords (AttrAccessMixin ):
347
340
"""Shared base class for Dataset and DataArray."""
348
341
349
- _close : Optional [ Callable [[], None ]]
342
+ _close : Callable [[], None ] | None
350
343
351
344
__slots__ = ("_close" ,)
352
345
353
346
def squeeze (
354
347
self ,
355
- dim : Union [ Hashable , Iterable [Hashable ], None ] = None ,
348
+ dim : Hashable | Iterable [Hashable ] | None = None ,
356
349
drop : bool = False ,
357
- axis : Union [ int , Iterable [int ], None ] = None ,
350
+ axis : int | Iterable [int ] | None = None ,
358
351
):
359
352
"""Return a new object with squeezed data.
360
353
@@ -416,8 +409,8 @@ def get_index(self, key: Hashable) -> pd.Index:
416
409
return pd .Index (range (self .sizes [key ]), name = key )
417
410
418
411
def _calc_assign_results (
419
- self : C , kwargs : Mapping [Any , Union [ T , Callable [[C ], T ] ]]
420
- ) -> Dict [Hashable , T ]:
412
+ self : C , kwargs : Mapping [Any , T | Callable [[C ], T ]]
413
+ ) -> dict [Hashable , T ]:
421
414
return {k : v (self ) if callable (v ) else v for k , v in kwargs .items ()}
422
415
423
416
def assign_coords (self , coords = None , ** coords_kwargs ):
@@ -535,7 +528,7 @@ def assign_attrs(self, *args, **kwargs):
535
528
536
529
def pipe (
537
530
self ,
538
- func : Union [ Callable [..., T ], Tuple [Callable [..., T ], str ] ],
531
+ func : Callable [..., T ] | tuple [Callable [..., T ], str ],
539
532
* args ,
540
533
** kwargs ,
541
534
) -> T :
@@ -802,7 +795,7 @@ def groupby_bins(
802
795
},
803
796
)
804
797
805
- def weighted (self : T_DataWithCoords , weights : " DataArray" ) -> Weighted [T_Xarray ]:
798
+ def weighted (self : T_DataWithCoords , weights : DataArray ) -> Weighted [T_Xarray ]:
806
799
"""
807
800
Weighted operations.
808
801
@@ -825,7 +818,7 @@ def rolling(
825
818
self ,
826
819
dim : Mapping [Any , int ] = None ,
827
820
min_periods : int = None ,
828
- center : Union [ bool , Mapping [Any , bool ] ] = False ,
821
+ center : bool | Mapping [Any , bool ] = False ,
829
822
** window_kwargs : int ,
830
823
):
831
824
"""
@@ -940,7 +933,7 @@ def coarsen(
940
933
self ,
941
934
dim : Mapping [Any , int ] = None ,
942
935
boundary : str = "exact" ,
943
- side : Union [ str , Mapping [Any , str ] ] = "left" ,
936
+ side : str | Mapping [Any , str ] = "left" ,
944
937
coord_func : str = "mean" ,
945
938
** window_kwargs : int ,
946
939
):
@@ -1290,7 +1283,7 @@ def where(self, cond, other=dtypes.NA, drop: bool = False):
1290
1283
1291
1284
return ops .where_method (self , cond , other )
1292
1285
1293
- def set_close (self , close : Optional [ Callable [[], None ]] ) -> None :
1286
+ def set_close (self , close : Callable [[], None ] | None ) -> None :
1294
1287
"""Register the function that releases any resources linked to this object.
1295
1288
1296
1289
This method controls how xarray cleans up resources associated
@@ -1523,20 +1516,20 @@ def __getitem__(self, value):
1523
1516
1524
1517
@overload
1525
1518
def full_like (
1526
- other : " Dataset" ,
1519
+ other : Dataset ,
1527
1520
fill_value ,
1528
- dtype : Union [ DTypeLike , Mapping [Any , DTypeLike ] ] = None ,
1529
- ) -> " Dataset" :
1521
+ dtype : DTypeLike | Mapping [Any , DTypeLike ] = None ,
1522
+ ) -> Dataset :
1530
1523
...
1531
1524
1532
1525
1533
1526
@overload
1534
- def full_like (other : " DataArray" , fill_value , dtype : DTypeLike = None ) -> " DataArray" :
1527
+ def full_like (other : DataArray , fill_value , dtype : DTypeLike = None ) -> DataArray :
1535
1528
...
1536
1529
1537
1530
1538
1531
@overload
1539
- def full_like (other : " Variable" , fill_value , dtype : DTypeLike = None ) -> " Variable" :
1532
+ def full_like (other : Variable , fill_value , dtype : DTypeLike = None ) -> Variable :
1540
1533
...
1541
1534
1542
1535
@@ -1815,9 +1808,9 @@ def ones_like(other, dtype: DTypeLike = None):
1815
1808
1816
1809
def get_chunksizes (
1817
1810
variables : Iterable [Variable ],
1818
- ) -> Mapping [Any , Tuple [int , ...]]:
1811
+ ) -> Mapping [Any , tuple [int , ...]]:
1819
1812
1820
- chunks : Dict [Any , Tuple [int , ...]] = {}
1813
+ chunks : dict [Any , tuple [int , ...]] = {}
1821
1814
for v in variables :
1822
1815
if hasattr (v .data , "chunks" ):
1823
1816
for dim , c in v .chunksizes .items ():
0 commit comments