Skip to content

Commit 0bf403b

Browse files
committed
Type keep_attrs as bool = True
1 parent d08e608 commit 0bf403b

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed

xarray/core/variable.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,14 +1743,15 @@ def reduce(
17431743
Array with summarized data and the indicated dimension(s)
17441744
removed.
17451745
"""
1746-
if keep_attrs is None:
1747-
keep_attrs = _get_keep_attrs(default=False)
1746+
keep_attrs_ = (
1747+
_get_keep_attrs(default=False) if keep_attrs is None else keep_attrs
1748+
)
17481749

17491750
result = super().reduce(
17501751
func=func,
17511752
dim=dim,
17521753
axis=axis,
1753-
keep_attrs=keep_attrs,
1754+
keep_attrs=keep_attrs_,
17541755
keepdims=keepdims,
17551756
**kwargs,
17561757
)

xarray/namedarray/_aggregations.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def reduce(
1919
dim: Dims = None,
2020
*,
2121
axis: int | Sequence[int] | None = None,
22-
keep_attrs: bool | None = None,
22+
keep_attrs: bool = True,
2323
keepdims: bool = False,
2424
**kwargs: Any,
2525
) -> Self:
@@ -29,7 +29,7 @@ def count(
2929
self,
3030
dim: Dims = None,
3131
*,
32-
keep_attrs: bool | None = None,
32+
keep_attrs: bool = True,
3333
**kwargs: Any,
3434
) -> Self:
3535
"""
@@ -40,7 +40,7 @@ def count(
4040
dim : str, Iterable of Hashable, "..." or None, default: None
4141
Name of dimension[s] along which to apply ``count``. For e.g. ``dim="x"``
4242
or ``dim=["x", "y"]``. If "..." or None, will reduce over all dimensions.
43-
keep_attrs : bool or None, optional
43+
keep_attrs : bool, optional
4444
If True, ``attrs`` will be copied from the original
4545
object to the new one. If False, the new object will be
4646
returned without attributes.
@@ -90,7 +90,7 @@ def all(
9090
self,
9191
dim: Dims = None,
9292
*,
93-
keep_attrs: bool | None = None,
93+
keep_attrs: bool = True,
9494
**kwargs: Any,
9595
) -> Self:
9696
"""
@@ -101,7 +101,7 @@ def all(
101101
dim : str, Iterable of Hashable, "..." or None, default: None
102102
Name of dimension[s] along which to apply ``all``. For e.g. ``dim="x"``
103103
or ``dim=["x", "y"]``. If "..." or None, will reduce over all dimensions.
104-
keep_attrs : bool or None, optional
104+
keep_attrs : bool, optional
105105
If True, ``attrs`` will be copied from the original
106106
object to the new one. If False, the new object will be
107107
returned without attributes.
@@ -151,7 +151,7 @@ def any(
151151
self,
152152
dim: Dims = None,
153153
*,
154-
keep_attrs: bool | None = None,
154+
keep_attrs: bool = True,
155155
**kwargs: Any,
156156
) -> Self:
157157
"""
@@ -162,7 +162,7 @@ def any(
162162
dim : str, Iterable of Hashable, "..." or None, default: None
163163
Name of dimension[s] along which to apply ``any``. For e.g. ``dim="x"``
164164
or ``dim=["x", "y"]``. If "..." or None, will reduce over all dimensions.
165-
keep_attrs : bool or None, optional
165+
keep_attrs : bool, optional
166166
If True, ``attrs`` will be copied from the original
167167
object to the new one. If False, the new object will be
168168
returned without attributes.
@@ -213,7 +213,7 @@ def max(
213213
dim: Dims = None,
214214
*,
215215
skipna: bool | None = None,
216-
keep_attrs: bool | None = None,
216+
keep_attrs: bool = True,
217217
**kwargs: Any,
218218
) -> Self:
219219
"""
@@ -229,7 +229,7 @@ def max(
229229
skips missing values for float dtypes; other dtypes either do not
230230
have a sentinel missing value (int) or ``skipna=True`` has not been
231231
implemented (object, datetime64 or timedelta64).
232-
keep_attrs : bool or None, optional
232+
keep_attrs : bool, optional
233233
If True, ``attrs`` will be copied from the original
234234
object to the new one. If False, the new object will be
235235
returned without attributes.
@@ -287,7 +287,7 @@ def min(
287287
dim: Dims = None,
288288
*,
289289
skipna: bool | None = None,
290-
keep_attrs: bool | None = None,
290+
keep_attrs: bool = True,
291291
**kwargs: Any,
292292
) -> Self:
293293
"""
@@ -303,7 +303,7 @@ def min(
303303
skips missing values for float dtypes; other dtypes either do not
304304
have a sentinel missing value (int) or ``skipna=True`` has not been
305305
implemented (object, datetime64 or timedelta64).
306-
keep_attrs : bool or None, optional
306+
keep_attrs : bool, optional
307307
If True, ``attrs`` will be copied from the original
308308
object to the new one. If False, the new object will be
309309
returned without attributes.
@@ -361,7 +361,7 @@ def mean(
361361
dim: Dims = None,
362362
*,
363363
skipna: bool | None = None,
364-
keep_attrs: bool | None = None,
364+
keep_attrs: bool = True,
365365
**kwargs: Any,
366366
) -> Self:
367367
"""
@@ -377,7 +377,7 @@ def mean(
377377
skips missing values for float dtypes; other dtypes either do not
378378
have a sentinel missing value (int) or ``skipna=True`` has not been
379379
implemented (object, datetime64 or timedelta64).
380-
keep_attrs : bool or None, optional
380+
keep_attrs : bool, optional
381381
If True, ``attrs`` will be copied from the original
382382
object to the new one. If False, the new object will be
383383
returned without attributes.
@@ -440,7 +440,7 @@ def prod(
440440
*,
441441
skipna: bool | None = None,
442442
min_count: int | None = None,
443-
keep_attrs: bool | None = None,
443+
keep_attrs: bool = True,
444444
**kwargs: Any,
445445
) -> Self:
446446
"""
@@ -462,7 +462,7 @@ def prod(
462462
NA. Only used if skipna is set to True or defaults to True for the
463463
array's dtype. Changed in version 0.17.0: if specified on an integer
464464
array and skipna=True, the result will be a float array.
465-
keep_attrs : bool or None, optional
465+
keep_attrs : bool, optional
466466
If True, ``attrs`` will be copied from the original
467467
object to the new one. If False, the new object will be
468468
returned without attributes.
@@ -532,7 +532,7 @@ def sum(
532532
*,
533533
skipna: bool | None = None,
534534
min_count: int | None = None,
535-
keep_attrs: bool | None = None,
535+
keep_attrs: bool = True,
536536
**kwargs: Any,
537537
) -> Self:
538538
"""
@@ -554,7 +554,7 @@ def sum(
554554
NA. Only used if skipna is set to True or defaults to True for the
555555
array's dtype. Changed in version 0.17.0: if specified on an integer
556556
array and skipna=True, the result will be a float array.
557-
keep_attrs : bool or None, optional
557+
keep_attrs : bool, optional
558558
If True, ``attrs`` will be copied from the original
559559
object to the new one. If False, the new object will be
560560
returned without attributes.
@@ -624,7 +624,7 @@ def std(
624624
*,
625625
skipna: bool | None = None,
626626
ddof: int = 0,
627-
keep_attrs: bool | None = None,
627+
keep_attrs: bool = True,
628628
**kwargs: Any,
629629
) -> Self:
630630
"""
@@ -643,7 +643,7 @@ def std(
643643
ddof : int, default: 0
644644
“Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``,
645645
where ``N`` represents the number of elements.
646-
keep_attrs : bool or None, optional
646+
keep_attrs : bool, optional
647647
If True, ``attrs`` will be copied from the original
648648
object to the new one. If False, the new object will be
649649
returned without attributes.
@@ -713,7 +713,7 @@ def var(
713713
*,
714714
skipna: bool | None = None,
715715
ddof: int = 0,
716-
keep_attrs: bool | None = None,
716+
keep_attrs: bool = True,
717717
**kwargs: Any,
718718
) -> Self:
719719
"""
@@ -732,7 +732,7 @@ def var(
732732
ddof : int, default: 0
733733
“Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``,
734734
where ``N`` represents the number of elements.
735-
keep_attrs : bool or None, optional
735+
keep_attrs : bool, optional
736736
If True, ``attrs`` will be copied from the original
737737
object to the new one. If False, the new object will be
738738
returned without attributes.
@@ -801,7 +801,7 @@ def median(
801801
dim: Dims = None,
802802
*,
803803
skipna: bool | None = None,
804-
keep_attrs: bool | None = None,
804+
keep_attrs: bool = True,
805805
**kwargs: Any,
806806
) -> Self:
807807
"""
@@ -817,7 +817,7 @@ def median(
817817
skips missing values for float dtypes; other dtypes either do not
818818
have a sentinel missing value (int) or ``skipna=True`` has not been
819819
implemented (object, datetime64 or timedelta64).
820-
keep_attrs : bool or None, optional
820+
keep_attrs : bool, optional
821821
If True, ``attrs`` will be copied from the original
822822
object to the new one. If False, the new object will be
823823
returned without attributes.
@@ -879,7 +879,7 @@ def cumsum(
879879
dim: Dims = None,
880880
*,
881881
skipna: bool | None = None,
882-
keep_attrs: bool | None = None,
882+
keep_attrs: bool = True,
883883
**kwargs: Any,
884884
) -> Self:
885885
"""
@@ -895,7 +895,7 @@ def cumsum(
895895
skips missing values for float dtypes; other dtypes either do not
896896
have a sentinel missing value (int) or ``skipna=True`` has not been
897897
implemented (object, datetime64 or timedelta64).
898-
keep_attrs : bool or None, optional
898+
keep_attrs : bool, optional
899899
If True, ``attrs`` will be copied from the original
900900
object to the new one. If False, the new object will be
901901
returned without attributes.
@@ -957,7 +957,7 @@ def cumprod(
957957
dim: Dims = None,
958958
*,
959959
skipna: bool | None = None,
960-
keep_attrs: bool | None = None,
960+
keep_attrs: bool = True,
961961
**kwargs: Any,
962962
) -> Self:
963963
"""
@@ -973,7 +973,7 @@ def cumprod(
973973
skips missing values for float dtypes; other dtypes either do not
974974
have a sentinel missing value (int) or ``skipna=True`` has not been
975975
implemented (object, datetime64 or timedelta64).
976-
keep_attrs : bool or None, optional
976+
keep_attrs : bool, optional
977977
If True, ``attrs`` will be copied from the original
978978
object to the new one. If False, the new object will be
979979
returned without attributes.

xarray/util/generate_aggregations.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ def reduce(
6161
dim: Dims = None,
6262
*,
6363
axis: int | Sequence[int] | None = None,
64-
keep_attrs: bool | None = None,
64+
keep_attrs{keep_attrs_type},
6565
keepdims: bool = False,
6666
**kwargs: Any,
6767
) -> Self:
6868
raise NotImplementedError()"""
6969

70+
7071
GROUPBY_PREAMBLE = """
7172
7273
class {obj}{cls}Aggregations:
@@ -120,7 +121,7 @@ def {method}(
120121
self,
121122
dim: Dims = None,
122123
*,{extra_kwargs}
123-
keep_attrs: bool | None = None,
124+
keep_attrs{keep_attrs_type},
124125
**kwargs: Any,
125126
) -> Self:
126127
"""
@@ -188,7 +189,7 @@ def {method}(
188189
“Delta Degrees of Freedom”: the divisor used in the calculation is ``N - ddof``,
189190
where ``N`` represents the number of elements."""
190191

191-
_KEEP_ATTRS_DOCSTRING = """keep_attrs : bool or None, optional
192+
_KEEP_ATTRS_DOCSTRING = """keep_attrs : {keep_attrs_type}, optional
192193
If True, ``attrs`` will be copied from the original
193194
object to the new one. If False, the new object will be
194195
returned without attributes."""
@@ -240,13 +241,16 @@ def {method}(
240241
>>> {calculation}(skipna=True, ddof=1)""",
241242
)
242243

244+
from dataclasses import field
245+
243246

244247
@dataclass
245248
class DataStructure:
246249
name: str
247250
create_example: str
248251
example_var_name: str
249252
numeric_only: bool = False
253+
keep_attrs_type: str = field(default=": bool | None = None")
250254
see_also_modules: tuple[str] = tuple
251255

252256

@@ -295,7 +299,11 @@ def __init__(
295299
self.docref = docref
296300
self.docref_description = docref_description
297301
self.example_call_preamble = example_call_preamble
298-
self.preamble = definition_preamble.format(obj=datastructure.name, cls=cls)
302+
self.preamble = definition_preamble.format(
303+
obj=datastructure.name,
304+
cls=cls,
305+
keep_attrs_type=datastructure.keep_attrs_type,
306+
)
299307
self.notes = "" if notes is None else notes
300308

301309
def generate_methods(self):
@@ -304,7 +312,11 @@ def generate_methods(self):
304312
yield self.generate_method(method)
305313

306314
def generate_method(self, method):
307-
template_kwargs = dict(obj=self.datastructure.name, method=method.name)
315+
template_kwargs = dict(
316+
obj=self.datastructure.name,
317+
method=method.name,
318+
keep_attrs_type=self.datastructure.keep_attrs_type,
319+
)
308320

309321
if method.extra_kwargs:
310322
extra_kwargs = "\n " + "\n ".join(
@@ -321,7 +333,11 @@ def generate_method(self, method):
321333
for text in [
322334
self._dim_docstring.format(method=method.name, cls=self.cls),
323335
*(kwarg.docs for kwarg in method.extra_kwargs if kwarg.docs),
324-
_KEEP_ATTRS_DOCSTRING,
336+
_KEEP_ATTRS_DOCSTRING.format(
337+
keep_attrs_type="bool or None"
338+
if "None" in self.datastructure.keep_attrs_type
339+
else "bool"
340+
),
325341
_KWARGS_DOCSTRING.format(method=method.name),
326342
]:
327343
if text:
@@ -574,6 +590,7 @@ def generate_code(self, method):
574590
... )""",
575591
example_var_name="na",
576592
numeric_only=False, # TODO
593+
keep_attrs_type=": bool = True",
577594
see_also_modules=("Dataset", "DataArray"),
578595
)
579596

0 commit comments

Comments
 (0)